|
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\PlaybackHistory; |
|
9
|
|
|
use Uxmp\Core\Orm\Model\PlaybackHistoryInterface; |
|
10
|
|
|
use Uxmp\Core\Orm\Model\SongInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @extends EntityRepository<PlaybackHistory> |
|
14
|
|
|
* |
|
15
|
|
|
* @method PlaybackHistoryInterface[] findBy(mixed[] $criteria, null|array $order = null, null|int $limit = null, null|int $offset = null) |
|
16
|
|
|
*/ |
|
17
|
|
|
final class PlaybackHistoryRepository extends EntityRepository implements PlaybackHistoryRepositoryInterface |
|
18
|
|
|
{ |
|
19
|
1 |
|
public function prototype(): PlaybackHistoryInterface |
|
20
|
|
|
{ |
|
21
|
1 |
|
return new PlaybackHistory(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public function findBySong(SongInterface $song): iterable |
|
25
|
|
|
{ |
|
26
|
1 |
|
return $this->findBy(['song' => $song]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param int $number Amount of items to retrieve |
|
31
|
|
|
* |
|
32
|
|
|
* @return iterable<array{cnt: int, song_id: int}> |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function getMostPlayed(int $number = 10): iterable |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->getEntityManager() |
|
37
|
1 |
|
->createQueryBuilder() |
|
38
|
1 |
|
->select('COUNT(c) as cnt, c.song_id') |
|
39
|
1 |
|
->from(PlaybackHistory::class, 'c') |
|
40
|
1 |
|
->groupBy('c.song_id') |
|
41
|
1 |
|
->orderBy('cnt', 'DESC') |
|
42
|
1 |
|
->setMaxResults($number) |
|
43
|
1 |
|
->getQuery() |
|
44
|
1 |
|
->getResult(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function save(PlaybackHistoryInterface $playbackHistory): void |
|
48
|
|
|
{ |
|
49
|
1 |
|
$em = $this->getEntityManager(); |
|
50
|
|
|
|
|
51
|
1 |
|
$em->persist($playbackHistory); |
|
52
|
1 |
|
$em->flush(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function delete(PlaybackHistoryInterface $playbackHistory): void |
|
56
|
|
|
{ |
|
57
|
1 |
|
$em = $this->getEntityManager(); |
|
58
|
|
|
|
|
59
|
1 |
|
$em->remove($playbackHistory); |
|
60
|
1 |
|
$em->flush(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|