Passed
Push — main ( 45ffa3...f692a6 )
by Daniel
02:23
created

GetStarred2Responder::writeJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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 GetStarred2Responder implements FormattedResponderInterface
12
{
13
    /**
14
     * @param Traversable<array{
15
     *  id: string,
16
     *  name: string,
17
     *  album: string,
18
     *  artist: string,
19
     *  coverArt: string,
20
     *  albumId: string,
21
     *  artistId: string,
22
     *  duration: int,
23
     *  created: string,
24
     *  starred: string,
25
     *  size: int
26
     * }> $songs
27
     * @param Traversable<array{
28
     *  id: string,
29
     *  name: string,
30
     *  artist: string,
31
     *  artistId: string,
32
     *  songCount: int,
33
     *  coverArt: string,
34
     *  duration: int,
35
     *  created: string,
36
     *  starred: string,
37
     *  year: int
38
     * }> $albums
39
     */
40 4
    public function __construct(
41
        private readonly Traversable $songs,
42
        private readonly Traversable $albums
43
    ) {
44
    }
45
46 1
    public function writeXml(XMLArray $XMLArray): void
47
    {
48 1
        $XMLArray->startLoop(
49
            'starred2',
50 1
            [],
51 1
            function (XMLArray $XMLArray): void {
52 1
                foreach ($this->albums as $album) {
53 1
                    $XMLArray->add(
54
                        'album',
55
                        null,
56
                        $album
57
                    );
58
                }
59
60 1
                foreach ($this->songs as $song) {
61 1
                    $XMLArray->add(
62
                        'song',
63
                        null,
64
                        $song
65
                    );
66
                }
67
            }
68
        );
69
    }
70
71 1
    public function writeJson(array &$root): void
72
    {
73 1
        $root['starred2'] = [
74 1
            'album' => iterator_to_array($this->albums),
75 1
            'song' => iterator_to_array($this->songs),
76
        ];
77
    }
78
79 1
    public function isBinaryResponder(): bool
80
    {
81 1
        return false;
82
    }
83
}
84