Passed
Push — main ( 9f54e3...00f6b4 )
by Daniel
04:22
created

SongRepository::findByMbId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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 JetBrains\PhpStorm\Pure;
9
use Uxmp\Core\Orm\Model\ArtistInterface;
10
use Uxmp\Core\Orm\Model\CatalogInterface;
11
use Uxmp\Core\Orm\Model\Favorite;
12
use Uxmp\Core\Orm\Model\FavoriteItemTypeEnum;
13
use Uxmp\Core\Orm\Model\PlaybackHistory;
14
use Uxmp\Core\Orm\Model\Song;
15
use Uxmp\Core\Orm\Model\SongInterface;
16
use Uxmp\Core\Orm\Model\UserInterface;
17
18
/**
19
 * @extends EntityRepository<Song>
20
 *
21
 * @method null|SongInterface find(mixed $id)
22
 */
23
final class SongRepository extends EntityRepository implements SongRepositoryInterface
24
{
25 1
    #[Pure]
26
    public function prototype(): SongInterface
27
    {
28 1
        return new Song();
29
    }
30
31 1
    public function save(SongInterface $song): void
32
    {
33 1
        $this->getEntityManager()->persist($song);
34 1
        $this->getEntityManager()->flush();
35
    }
36
37 1
    public function delete(SongInterface $song): void
38
    {
39 1
        $this->getEntityManager()->remove($song);
40 1
        $this->getEntityManager()->flush();
41
    }
42
43 1
    public function findByMbId(string $mbid): ?SongInterface
44
    {
45 1
        return $this->findOneBy([
46
            'mbid' => $mbid,
47
        ]);
48
    }
49
50
    /**
51
     * Retrieve a complete list of favorite songs for a user
52
     *
53
     * @return iterable<SongInterface>
54
     */
55 1
    public function findFavorites(
56
        UserInterface $user,
57
        ?CatalogInterface $catalog = null
58
    ): iterable {
59 1
        $parameter = [
60
            'user' => $user,
61
            'type' => FavoriteItemTypeEnum::SONG,
62
        ];
63
64 1
        $qb = $this->createQueryBuilder('a');
65 1
        $qbSub = $this
66 1
            ->getEntityManager()
67 1
            ->createQueryBuilder();
68 1
        $expressionBuilder = $this->getEntityManager()->getExpressionBuilder();
69
70 1
        $andExpression = $expressionBuilder->andX();
71 1
        $andExpression->add($expressionBuilder->eq('fav.user', ':user'));
72 1
        $andExpression->add($expressionBuilder->eq('fav.type', ':type'));
73 1
        if ($catalog !== null) {
74 1
            $andExpression->add($expressionBuilder->eq('a.catalog', ':catalog'));
75
76 1
            $parameter['catalog'] = $catalog;
77
        }
78
79
        $qb
80 1
            ->where(
81 1
                $expressionBuilder->in(
82
                    'a.id',
83
                    $qbSub
84 1
                        ->select('fav.item_id')
85 1
                        ->from(Favorite::class, 'fav')
86 1
                        ->where($andExpression)
87 1
                        ->getDQL()
88
                )
89
            )
90 1
            ->setParameters($parameter)
91
        ;
92
93 1
        return $qb->getQuery()->getResult();
94
    }
95
96
    /**
97
     * Returns most played songs of a certain artis
98
     *
99
     * @return iterable<SongInterface>
100
     */
101
    public function getTopSongsByArtist(
102
        ArtistInterface $artist
103
    ): iterable {
104
        $query = <<<SQL
105
            SELECT song
106
            FROM %s song
107
            LEFT JOIN %s pbh
108
            WITH pbh.song_id = song.id
109
            WHERE song.artist = :artist
110
            GROUP BY song HAVING(pbh.id) > 0
111
            ORDER BY COUNT(pbh.id)
112
            SQL;
113
114
        return $this->getEntityManager()
115
            ->createQuery(sprintf(
116
                $query,
117
                Song::class,
118
                PlaybackHistory::class,
119
            ))
120
            ->setParameters(['artist' => $artist])
121
            ->setMaxResults(10)
122
            ->toIterable();
123
    }
124
}
125