Passed
Push — main ( cade59...7a0180 )
by Daniel
03:32
created

SongRepository::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 11
cts 11
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 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 1
            'mbid' => $mbid,
47 1
        ]);
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 1
            'user' => $user,
61 1
            'type' => FavoriteItemTypeEnum::SONG,
62 1
        ];
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 1
        $qb
80 1
            ->where(
81 1
                $expressionBuilder->in(
82 1
                    'a.id',
83 1
                    $qbSub
84 1
                        ->select('fav.item_id')
85 1
                        ->from(Favorite::class, 'fav')
86 1
                        ->where($andExpression)
87 1
                        ->getDQL()
88 1
                )
89 1
            )
90 1
            ->setParameters($parameter)
91 1
        ;
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 1
    public function getTopSongsByArtist(
102
        ArtistInterface $artist
103
    ): iterable {
104 1
        $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(COUNT(pbh.id)) > 0
111
            ORDER BY COUNT(pbh.id) DESC
112 1
            SQL;
113
114 1
        return $this->getEntityManager()
115 1
            ->createQuery(
116 1
                sprintf(
117 1
                    $query,
118 1
                    Song::class,
119 1
                    PlaybackHistory::class,
120 1
                )
121 1
            )
122 1
            ->setParameters(['artist' => $artist])
123 1
            ->setMaxResults(10)
124 1
            ->toIterable();
125
    }
126
127 1
    public function search(string $searchTerm): array
128
    {
129 1
        $qb = $this->getEntityManager()->createQueryBuilder();
130
131 1
        $qb
132 1
            ->select('a')
133 1
            ->from(Song::class, 'a')
134 1
            ->where(
135 1
                $qb->expr()->like('a.title', ':searchTerm')
136 1
            )
137 1
            ->setParameter('searchTerm', sprintf('%%%s%%', $searchTerm))
138 1
        ;
139
140 1
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
141
    }
142
}
143