public static function create($data, string $opType = null): self
    {
        //Check type
        if (!$data instanceof Document && !$data instanceof AbstractScript) {
            throw new \InvalidArgumentException('The data needs to be a Document or a Script.');
        }

        if (null === $opType && $data->hasOpType()) {
            $opType = $data->getOpType();
        }

        //Check that scripts can only be used for updates
        if ($data instanceof AbstractScript) {
            if (null === $opType) {
                $opType = self::OP_TYPE_UPDATE;
            } elseif (self::OP_TYPE_UPDATE !== $opType) {
                throw new \InvalidArgumentException('Scripts can only be used with the update operation type.');
            }
        }

        switch ($opType) {
            case self::OP_TYPE_DELETE:
                $action = new DeleteDocument($data);
                break;

            case self::OP_TYPE_CREATE:
                $action = new CreateDocument($data);
                break;

            case self::OP_TYPE_UPDATE:
                $action = new UpdateDocument($data);
                break;

            case self::OP_TYPE_INDEX:
            default:
                $action = new IndexDocument($data);
                break;
        }

        return $action;
    }