Passed
Push — main ( 22f6c0...394f25 )
by Daniel
02:25
created

GetRandomSongsResponder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 47
ccs 19
cts 19
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeXml() 0 11 2
A __construct() 0 3 1
A writeJson() 0 3 1
A isBinaryResponder() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usox\HyperSonic\FeatureSet\V1161\Responder;
6
7
use AaronDDM\XMLBuilder\XMLArray;
8
use Traversable;
9
use Usox\HyperSonic\Response\FormattedResponderInterface;
10
11
final class GetRandomSongsResponder implements FormattedResponderInterface
12
{
13
    /**
14
     * @param Traversable<array{
15
     *  id: string,
16
     *  parent: string,
17
     *  title: string,
18
     *  isDir: string,
19
     *  album: string,
20
     *  artist: string,
21
     *  track: int,
22
     *  year: int,
23
     *  coverArt: string,
24
     *  duration: int,
25
     *  size: int
26
     * }> $songs
27
     */
28 4
    public function __construct(
29
        private readonly Traversable $songs,
30
    ) {
31 4
    }
32
33 1
    public function writeXml(XMLArray $XMLArray): void
34
    {
35 1
        $XMLArray->startLoop(
36 1
            'randomSongs',
37 1
            [],
38 1
            function (XMLArray $XMLArray): void {
39 1
                foreach ($this->songs as $song) {
40 1
                    $XMLArray->add(
41 1
                        'song',
42 1
                        null,
43 1
                        $song
44 1
                    );
45
                }
46 1
            }
47 1
        );
48
    }
49
50 1
    public function writeJson(array &$root): void
51
    {
52 1
        $root['randomSongs'] = ['song' => iterator_to_array($this->songs)];
53
    }
54
55 1
    public function isBinaryResponder(): bool
56
    {
57 1
        return false;
58
    }
59
}
60