PlaybackHistoryRepository::delete()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
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 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