<commit_before><?php

namespace Jimdo\Reports\Reportbook;

use Jimdo\Reports\Web\ApplicationConfig as ApplicationConfig;
use Jimdo\Reports\Serializer as Serializer;

class ReportMongoRepository implements ReportRepository
{
    /** @var Serializer */
    public $serializer;

    /** @var MongoDB\Client */
    private $client;

    /** @var MongoDB\Database */
    private $reportbook;

    /** @var MongoDB\Collection */
    private $reports;

    /** @var ApplicationConfig */
    private $applicationConfig;

    /**
     * @param Serializer $serializer
     * @param Client $client
     */
    public function __construct(\MongoDB\Client $client, Serializer $serializer, ApplicationConfig $applicationConfig)
    {
        $this->applicationConfig = $applicationConfig;
        $this->serializer = $serializer;
        $this->client = $client;
        $this->reportbook = $this->client->selectDatabase($this->applicationConfig->mongoDatabase);
        $this->reports = $this->reportbook->reports;
    }

    /**
     * @param TraineeId $traineeId
     * @param string $content
     * @param string $date
     * @param string $calendarWeek
     * @return Report
     */
    public function create(TraineeId $traineeId, string $content, string $date, string $calendarWeek): Report
    {
        $report = new Report($traineeId, $content, $date, $calendarWeek, uniqid());

        $this->save($report);

        return $report;
    }

    /**
     * @param Report $report
     */
    public function save(Report $report)
    {
        if ($this->findById($report->id()) !== null) {
            $this->delete($report);
            $this->reports->insertOne($this->serializer->serializeReport($report));
        } else {
            $this->reports->insertOne($this->serializer->serializeReport($report));
        }
    }

    /**
     * @return Report[]
     */
    public function findAll(): array
    {
        $foundReports = [];

        foreach ($this->reports->find() as $report) {
            $foundReports [] = $this->serializer->unserializeReport($report->getArrayCopy());
        }

        return $foundReports;
    }

    /**
     * @param string $traineeId
     * @return Report[]
     */
    public function findByTraineeId(string $traineeId): array
    {
        $foundReports = $this->findAll();
        $reports = [];

        foreach ($foundReports as $report) {
            if ($report->traineeId() === $traineeId) {
                $reports[] = $report;
            }
        }
        return $reports;
    }

    /**
     * @param Report $report
     */
    public function delete(Report $report)
    {
        $this->reports->deleteOne(['id' => $report->id()]);
    }

    /**
     * @param string $status
     * @return Report[]
     */
    public function findByStatus(string $status): array
    {
        $foundReports = $this->findAll();
        $reports = [];

        foreach ($foundReports as $report) {
            if ($report->status() === $status) {
                $reports[] = $report;
            }
        }
        return $reports;
    }

    /**
     * @param string $text
     * @return Report[]
     */
    public function findReportsByString(string $text): array
    {
        $foundReports = $this->findAll();

        if ($text === '') {
            return $foundReports;
        }

        $reports = [];

        if (is_numeric($text)) {
            foreach ($foundReports as $report) {
                if (strpos($report->calendarWeek(), $text) !== false) {
                    $reports[] = $report;
                }
            }
        } else {
            foreach ($foundReports as $report) {
                if (strpos($report->content(), $text) !== false) {
                    $reports[] = $report;
                }
            }
        }
        return $reports;
    }

    /**
     * @param string $id
     * @return Report
     */
    public function findById(string $id)
    {
        foreach ($this->findAll() as $report) {
            if ($report->id() === $id) {
                return $report;
            }
        }
    }
}
<commit_msg>Make the search function more efficient<commit_after><?php

namespace Jimdo\Reports\Reportbook;

use Jimdo\Reports\Web\ApplicationConfig as ApplicationConfig;
use Jimdo\Reports\Serializer as Serializer;

class ReportMongoRepository implements ReportRepository
{
    /** @var Serializer */
    public $serializer;

    /** @var MongoDB\Client */
    private $client;

    /** @var MongoDB\Database */
    private $reportbook;

    /** @var MongoDB\Collection */
    private $reports;

    /** @var ApplicationConfig */
    private $applicationConfig;

    /**
     * @param Serializer $serializer
     * @param Client $client
     */
    public function __construct(\MongoDB\Client $client, Serializer $serializer, ApplicationConfig $applicationConfig)
    {
        $this->applicationConfig = $applicationConfig;
        $this->serializer = $serializer;
        $this->client = $client;
        $this->reportbook = $this->client->selectDatabase($this->applicationConfig->mongoDatabase);
        $this->reports = $this->reportbook->reports;
    }

    /**
     * @param TraineeId $traineeId
     * @param string $content
     * @param string $date
     * @param string $calendarWeek
     * @return Report
     */
    public function create(TraineeId $traineeId, string $content, string $date, string $calendarWeek): Report
    {
        $report = new Report($traineeId, $content, $date, $calendarWeek, uniqid());

        $this->save($report);

        return $report;
    }

    /**
     * @param Report $report
     */
    public function save(Report $report)
    {
        if ($this->findById($report->id()) !== null) {
            $this->delete($report);
            $this->reports->insertOne($this->serializer->serializeReport($report));
        } else {
            $this->reports->insertOne($this->serializer->serializeReport($report));
        }
    }

    /**
     * @return Report[]
     */
    public function findAll(): array
    {
        $foundReports = [];

        foreach ($this->reports->find() as $report) {
            $foundReports [] = $this->serializer->unserializeReport($report->getArrayCopy());
        }

        return $foundReports;
    }

    /**
     * @param string $traineeId
     * @return Report[]
     */
    public function findByTraineeId(string $traineeId): array
    {
        $foundReports = $this->findAll();
        $reports = [];

        foreach ($foundReports as $report) {
            if ($report->traineeId() === $traineeId) {
                $reports[] = $report;
            }
        }
        return $reports;
    }

    /**
     * @param Report $report
     */
    public function delete(Report $report)
    {
        $this->reports->deleteOne(['id' => $report->id()]);
    }

    /**
     * @param string $status
     * @return Report[]
     */
    public function findByStatus(string $status): array
    {
        $foundReports = $this->findAll();
        $reports = [];

        foreach ($foundReports as $report) {
            if ($report->status() === $status) {
                $reports[] = $report;
            }
        }
        return $reports;
    }

    /**
     * @param string $text
     * @return Report[]
     */
    public function findReportsByString(string $text): array
    {
        $foundReports = $this->findAll();

        if ($text === '') {
            return $foundReports;
        }

        $reports = [];

        if (is_numeric($text)) {
             foreach ($this->reports->find(['calendarWeek' => $text]) as $report) {
                 $reports [] = $this->serializer->unserializeReport($report->getArrayCopy());
             }
        } else {
            foreach ($this->reports->find(['content' => $text]) as $report) {
                $reports [] = $this->serializer->unserializeReport($report->getArrayCopy());
            }
        }
        return $reports;
    }

    /**
     * @param string $id
     * @return Report
     */
    public function findById(string $id)
    {
        foreach ($this->findAll() as $report) {
            if ($report->id() === $id) {
                return $report;
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.14812112367748997, 'fraction_numerical': 0.0, 'mean_word_length': 1.9743218806509946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5204093', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2273, 'n_words': 479}