Passed
Push — main ( 4a31bb...ed4306 )
by Daniel
04:24
created

GenreRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Query\Expr\Join;
9
use Generator;
10
use JetBrains\PhpStorm\Pure;
11
use Uxmp\Core\Orm\Model\Genre;
12
use Uxmp\Core\Orm\Model\GenreInterface;
13
use Uxmp\Core\Orm\Model\GenreMap;
14
use Uxmp\Core\Orm\Model\GenreMapEnum;
15
16
/**
17
 * @extends EntityRepository<GenreInterface>
18
 *
19
 * @method null|GenreInterface findOneBy(mixed[] $criteria)
20
 */
21
final class GenreRepository extends EntityRepository implements GenreRepositoryInterface
22
{
23 1
    #[Pure]
24
    public function prototype(): GenreInterface
25
    {
26 1
        return new Genre();
27
    }
28
29 1
    public function save(GenreInterface $genre): void
30
    {
31 1
        $this->getEntityManager()->persist($genre);
32 1
        $this->getEntityManager()->flush();
33
    }
34
35 1
    public function delete(GenreInterface $genre): void
36
    {
37 1
        $this->getEntityManager()->remove($genre);
38 1
        $this->getEntityManager()->flush();
39
    }
40
41
    /**
42
     * Retrieve a list of all genres and the number of albums/songs associated
43
     *
44
     * @return Generator<array{
45
     *  value: string,
46
     *  albumCount: int,
47
     *  songCount: int
48
     * }>
49
     */
50 1
    public function getGenreStatistics(): Generator
51
    {
52 1
        $query = $this->getEntityManager()
53 1
            ->createQueryBuilder()
54 1
            ->select(
55
                'a.title',
56
                'count(b.id) as albumCount',
57
                'count(c.id) as songCount'
58
            )
59 1
            ->from(Genre::class, 'a')
60 1
            ->leftJoin(
61
                GenreMap::class,
62
                'b',
63
                Join::WITH,
64
                'b.genre_id = a.id AND b.mapped_item_type = :album_index_name'
65
            )
66 1
            ->leftJoin(
67
                GenreMap::class,
68
                'c',
69
                Join::WITH,
70
                'c.genre_id = a.id AND c.mapped_item_type = :song_index_name'
71
            )
72 1
            ->setParameters([
73 1
                'album_index_name' => GenreMapEnum::ALBUM->value,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ']' on line 73 at column 57
Loading history...
74 1
                'song_index_name' => GenreMapEnum::SONG->value,
75
            ])
76 1
            ->groupBy('a.title')
77 1
            ->orderBy('a.title', 'ASC')
78 1
            ->getQuery();
79
80 1
        foreach ($query->toIterable() as $data) {
81
            yield [
82 1
                'value' => $data['title'],
83 1
                'albumCount' => (int) $data['albumCount'],
84 1
                'songCount' => (int) $data['songCount'],
85
            ];
86
        }
87
    }
88
}
89