DiscRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 28
c 0
b 0
f 0
dl 0
loc 56
ccs 27
cts 27
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prototype() 0 3 1
A save() 0 4 1
A delete() 0 4 1
A findUniqueDisc() 0 7 1
A findEmptyDiscs() 0 24 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\CatalogInterface;
10
use Uxmp\Core\Orm\Model\Disc;
11
use Uxmp\Core\Orm\Model\DiscInterface;
12
use Uxmp\Core\Orm\Model\Song;
13
14
/**
15
 * @extends EntityRepository<Disc>
16
 */
17
final class DiscRepository extends EntityRepository implements DiscRepositoryInterface
18
{
19 1
    public function prototype(): DiscInterface
20
    {
21 1
        return new Disc();
22
    }
23
24 1
    public function save(DiscInterface $disc): void
25
    {
26 1
        $this->getEntityManager()->persist($disc);
27 1
        $this->getEntityManager()->flush();
28
    }
29
30 1
    public function delete(DiscInterface $disc): void
31
    {
32 1
        $this->getEntityManager()->remove($disc);
33 1
        $this->getEntityManager()->flush();
34
    }
35
36
    /**
37
     * Find a unique disc by its mbid and the disc number within a release group
38
     */
39 1
    public function findUniqueDisc(
40
        string $musicBrainzDiscId,
41
        int $discNumber
42
    ): ?DiscInterface {
43 1
        return $this->findOneBy([
44 1
            'mbid' => $musicBrainzDiscId,
45 1
            'number' => $discNumber,
46 1
        ]);
47
    }
48
49 1
    public function findEmptyDiscs(CatalogInterface $catalog): array
50
    {
51 1
        $query = <<<SQL
52
            SELECT disc
53
            FROM %s disc
54
            LEFT JOIN %s song 
55
            WITH song.disc_id = disc.id
56
            LEFT JOIN %s album
57
            WITH album.id = disc.album_id
58
            WHERE album.catalog = :catalog
59
            GROUP BY disc HAVING COUNT(song.id) = 0
60 1
            SQL;
61
62 1
        return $this->getEntityManager()
63 1
            ->createQuery(
64 1
                sprintf(
65 1
                    $query,
66 1
                    Disc::class,
67 1
                    Song::class,
68 1
                    Album::class,
69 1
                )
70 1
            )
71 1
            ->setParameters(['catalog' => $catalog])
72 1
            ->getResult();
73
    }
74
}
75