Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
CRAP
91.30% covered (success)
91.30%
21 / 23
QuoteRepository
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
14.13
91.30% covered (success)
91.30%
21 / 23
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 findByCriteria
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
7 / 7
 isSameAuthor
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getAuthorSlug
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 fetchAll
0.00% covered (danger)
0.00%
0 / 1
6.22
81.82% covered (warning)
81.82%
9 / 11
<?php
declare(strict_types=1);
namespace App\Quote\Repository;
use App\Collection\DataSourceCollection;
use App\Quote\DataSource\DataSourceInterface;
use App\Quote\Model\QuoteInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\String\UnicodeString;
final class QuoteRepository implements QuoteRepositoryInterface
{
    private DataSourceCollection $dataSourceCollection;
    private SluggerInterface $slugger;
    public function __construct(
        DataSourceCollection $dataSourceCollection,
        SluggerInterface $slugger
    ) {
        $this->dataSourceCollection = $dataSourceCollection;
        $this->slugger = $slugger;
    }
    public function findByCriteria(array $criteria): ?array
    {
        $quotes = [];
        foreach ($this->fetchAll() as $quote) {
            if ($this->isSameAuthor($criteria, $quote)) {
                $quotes[] = $quote;
            }
        }
        if (isset($criteria['limit'])) {
            return array_slice($quotes, 0, $criteria['limit']);
        }
        return $quotes;
    }
    private function isSameAuthor(array $criteria, QuoteInterface $quote): bool
    {
        return $this->getAuthorSlug($quote->getAuthor()) === $this->getAuthorSlug($criteria['author']);
    }
    private function getAuthorSlug(string $author): string
    {
        return (new UnicodeString($this->slugger->slug($author)->toString()))->trim()->lower()->toString();
    }
    public function fetchAll(): array
    {
        $quotesCollection = [];
        foreach ($this->dataSourceCollection->all() as $dataSource) {
            if (!$dataSource instanceof DataSourceInterface) {
                continue;
            }
            if (empty($dataSource->getQuotes()->items())) {
                continue;
            }
            foreach ($dataSource->getQuotes()->items() as $quote) {
                if (!$quote instanceof QuoteInterface) {
                    continue;
                }
                $quotesCollection[] = $quote;
            }
        }
        return $quotesCollection;
    }
}