Passed
Push — main ( cb2c48...010f0d )
by Daniel
05:13
created

AlbumRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Generator;
9
use Uxmp\Core\Orm\Model\Album;
10
use Uxmp\Core\Orm\Model\AlbumInterface;
11
use Uxmp\Core\Orm\Model\CatalogInterface;
12
use Uxmp\Core\Orm\Model\Disc;
13
use Uxmp\Core\Orm\Model\Favorite;
14
use Uxmp\Core\Orm\Model\GenreInterface;
15
use Uxmp\Core\Orm\Model\GenreMap;
16
use Uxmp\Core\Orm\Model\GenreMapEnum;
0 ignored issues
show
Bug introduced by
The type Uxmp\Core\Orm\Model\GenreMapEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Uxmp\Core\Orm\Model\UserInterface;
18
19
/**
20
 * @extends EntityRepository<AlbumInterface>
21
 *
22
 * @method AlbumInterface[] findBy(mixed[] $criteria, null|array $order = null, null|int $limit = null, null|int $offset = null)
23
 */
24
final class AlbumRepository extends EntityRepository implements AlbumRepositoryInterface
25
{
26 1
    public function prototype(): AlbumInterface
27
    {
28 1
        return new Album();
29
    }
30
31 1
    public function save(AlbumInterface $album): void
32
    {
33 1
        $em = $this->getEntityManager();
34
35 1
        $em->persist($album);
36 1
        $em->flush();
37
    }
38
39 1
    public function findByMbId(string $mbid): ?AlbumInterface
40
    {
41 1
        return $this->findOneBy([
42
            'mbid' => $mbid,
43
        ]);
44
    }
45
46
    /**
47
     * Returns all albums having a certain genre
48
     *
49
     * @return Generator<AlbumInterface>
50
     */
51 1
    public function findByGenre(GenreInterface $genre): Generator
52
    {
53 1
        $queryBuilder = $this
54 1
            ->getEntityManager()
55 1
            ->createQueryBuilder();
56 1
        $subQueryBuilder = $this
57 1
            ->getEntityManager()
58 1
            ->createQueryBuilder();
59 1
        $expressionBuilder = $this
60 1
            ->getEntityManager()
61 1
            ->getExpressionBuilder();
62
63 1
        $andExpression = $expressionBuilder->andX();
64 1
        $andExpression->add($expressionBuilder->eq('genre_map.genre', '?1'));
65 1
        $andExpression->add($expressionBuilder->eq('genre_map.mapped_item_type', '?2'));
66
67
        $queryBuilder
68 1
            ->select('a')
69 1
            ->from(Album::class, 'a')
70 1
            ->where(
71 1
                $expressionBuilder->in(
72
                    'a.id',
73
                    $subQueryBuilder
74 1
                        ->select('genre_map.mapped_item_id')
75 1
                        ->from(GenreMap::class, 'genre_map')
76 1
                        ->where($andExpression)
77 1
                        ->getDQL()
78
                )
79
            )
80 1
            ->orderBy('a.title', 'ASC')
81 1
            ->setParameter(1, $genre)
82 1
            ->setParameter(2, GenreMapEnum::ALBUM)
83
        ;
84
85 1
        foreach ($queryBuilder->getQuery()->toIterable() as $item) {
86 1
            yield $item;
87
        }
88
    }
89
90 1
    public function delete(AlbumInterface $album): void
91
    {
92 1
        $em = $this->getEntityManager();
93
94 1
        $em->remove($album);
95 1
        $em->flush();
96
    }
97
98 1
    public function getFavorites(UserInterface $user): iterable
99
    {
100 1
        $qb = $this
101 1
            ->getEntityManager()
102 1
            ->createQueryBuilder();
103 1
        $qbSub = $this
104 1
            ->getEntityManager()
105 1
            ->createQueryBuilder();
106 1
        $expressionBuilder = $this
107 1
            ->getEntityManager()
108 1
            ->getExpressionBuilder();
109
110
        $qb
111 1
            ->select('a')
112 1
            ->from(Album::class, 'a')
113 1
            ->where(
114 1
                $expressionBuilder->in(
115
                    'a.id',
116
                    $qbSub
117 1
                        ->select('fav.item_id')
118 1
                        ->from(Favorite::class, 'fav')
119 1
                        ->where($expressionBuilder->eq('fav.user', '?1'))
120 1
                        ->getDQL()
121
                )
122
            )
123 1
            ->orderBy('a.title', 'ASC')
124 1
            ->setParameter(1, $user);
125
126 1
        return $qb->getQuery()->getResult();
127
    }
128
129 1
    public function findEmptyAlbums(CatalogInterface $catalog): iterable
130
    {
131 1
        $query = <<<SQL
132
            SELECT album
133
            FROM %s album
134
            LEFT JOIN %s disc 
135
            WITH disc.album_id = album.id
136
            WHERE album.catalog_id = %d
137
            GROUP BY album HAVING COUNT(disc.id) = 0
138
            SQL;
139
140 1
        return $this->getEntityManager()
141 1
            ->createQuery(sprintf(
142
                $query,
143
                Album::class,
144
                Disc::class,
145 1
                $catalog->getId(),
146
            ))
147 1
            ->getResult();
148
    }
149
}
150