Passed
Push — main ( 79b143...ab85f9 )
by Daniel
03:36
created

RandomSongsRetriever::retrieve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 10
cc 1
nc 1
nop 5
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Song;
6
7
use Uxmp\Core\Orm\Model\CatalogInterface;
8
use Uxmp\Core\Orm\Model\GenreInterface;
9
use Uxmp\Core\Orm\Model\SongInterface;
10
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
11
12
/**
13
 * Provides functionality to retrieve songs in a random order
14
 */
15
final class RandomSongsRetriever implements RandomSongsRetrieverInterface
16
{
17 1
    public function __construct(
18
        private readonly SongRepositoryInterface $songRepository,
19
    ) {
20 1
    }
21
22
    /**
23
     * @return array<SongInterface>
24
     */
25 1
    public function retrieve(
26
        int $limit,
27
        ?CatalogInterface $catalog = null,
28
        ?GenreInterface $genre = null,
29
        ?int $fromYear = null,
30
        ?int $toYear = null,
31
    ): array {
32 1
        $list = $this->songRepository->getRandomBy(
33 1
            $catalog,
34 1
            $genre,
35 1
            $fromYear,
36 1
            $toYear
37 1
        );
38
39
        // @todo inefficient, but doctrine doesn't support RAND order natively
40 1
        $list = iterator_to_array($list);
41
42 1
        shuffle($list);
43
44 1
        return array_slice($list, 0, $limit);
45
    }
46
}
47