Passed
Push — main ( 97bfd7...43e821 )
by Daniel
04:18
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 Uxmp\Core\Orm\Model\Album;
9
use Uxmp\Core\Orm\Model\AlbumInterface;
10
use Uxmp\Core\Orm\Model\CatalogInterface;
11
use Uxmp\Core\Orm\Model\Disc;
12
use Uxmp\Core\Orm\Model\Favorite;
13
use Uxmp\Core\Orm\Model\Song;
14
use Uxmp\Core\Orm\Model\UserInterface;
15
16
/**
17
 * @extends EntityRepository<AlbumInterface>
18
 *
19
 * @method AlbumInterface[] findBy(mixed[] $criteria, null|array $order = null, null|int $limit = null, null|int $offset = null)
20
 */
21
final class AlbumRepository extends EntityRepository implements AlbumRepositoryInterface
22
{
23 1
    public function prototype(): AlbumInterface
24
    {
25 1
        return new Album();
26
    }
27
28 1
    public function save(AlbumInterface $album): void
29
    {
30 1
        $em = $this->getEntityManager();
31
32 1
        $em->persist($album);
33 1
        $em->flush();
34 1
    }
35
36 1
    public function findByMbId(string $mbid): ?AlbumInterface
37
    {
38 1
        return $this->findOneBy([
39 1
            'mbid' => $mbid
40
        ]);
41
    }
42
43 1
    public function delete(AlbumInterface $album): void
44
    {
45 1
        $em = $this->getEntityManager();
46
47 1
        $em->remove($album);
48 1
        $em->flush();
49 1
    }
50
51
    public function getFavorites(UserInterface $user): iterable
52
    {
53
        $qb = $this
54
            ->getEntityManager()
55
            ->createQueryBuilder();
56
        $qbSub = $this
57
            ->getEntityManager()
58
            ->createQueryBuilder();
59
        $expr = $this->getEntityManager()->getExpressionBuilder();
60
61
        $qb
62
            ->select('a')
63
            ->from(Album::class, 'a')
64
            ->where(
65
                $expr->in(
66
                    'a.id',
67
                    $qbSub
68
                        ->select('fav.item_id')
69
                        ->from(Favorite::class, 'fav')
70
                        ->where($expr->eq('fav.user', '?1'))
71
                        ->getDQL()
72
                )
73
            )
74
            ->orderBy('a.title', 'ASC')
75
            ->setParameter(1, $user);
76
77
        return $qb->getQuery()->getResult();
78
    }
79
80 1
    public function findEmptyAlbums(CatalogInterface $catalog): iterable
81
    {
82 1
        $query = <<<SQL
83
        SELECT album
84
        FROM %s album
85
        LEFT JOIN %s disc 
86
        WITH disc.album_id = album.id
87
        WHERE album.catalog_id = %d
88
        GROUP BY album HAVING COUNT(disc.id) = 0
89
        SQL;
90
91 1
        return $this->getEntityManager()
92 1
            ->createQuery(sprintf(
93 1
                $query,
94 1
                Album::class,
95 1
                Disc::class,
96 1
                $catalog->getId(),
97
            ))
98 1
            ->getResult();
99
    }
100
}
101