GetArtistsMethod::__invoke()   B
last analyzed

Complexity

Conditions 7
Paths 28

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 29
c 1
b 0
f 0
nc 28
nop 3
dl 0
loc 53
ccs 33
cts 33
cp 1
crap 7
rs 8.5226

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usox\HyperSonic\FeatureSet\V1161\Method;
6
7
use Usox\HyperSonic\FeatureSet\V1161\Contract\ArtistListDataProviderInterface;
8
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface;
9
use Usox\HyperSonic\Response\ResponderInterface;
10
11
/**
12
 * Retrieves and transforms data for the list of artists
13
 *
14
 * This class covers the `getArtists.view` method
15
 *
16
 * @see http://www.subsonic.org/pages/api.jsp#getArtists
17
 */
18
final class GetArtistsMethod implements V1161MethodInterface
19
{
20 2
    public function __construct(
21
        private readonly ResponderFactoryInterface $responderFactory,
22
    ) {
23 2
    }
24
25
    /**
26
     * @param array<string, scalar> $queryParams
27
     * @param array<string, scalar> $args
28
     */
29 1
    public function __invoke(
30
        ArtistListDataProviderInterface $artistListDataProvider,
31
        array $queryParams,
32
        array $args,
33
    ): ResponderInterface {
34 1
        $data = [];
35 1
        $currentIndex = null;
36 1
        $indexItem = [];
37
38 1
        $musicFolderId = $queryParams['musicFolderId'] ?? null;
39 1
        if ($musicFolderId !== null) {
40 1
            $musicFolderId = (string) $musicFolderId;
41
        }
42
43 1
        foreach ($artistListDataProvider->getArtists($musicFolderId) as $artist) {
44 1
            $artistIndex = mb_strtoupper(substr($artist['name'], 0, 1));
45
46 1
            if ($artistIndex !== $currentIndex) {
47 1
                if ($indexItem !== []) {
48 1
                    $data[] = $indexItem;
49
                }
50
51 1
                $currentIndex = $artistIndex;
52
53 1
                $indexItem = [
54 1
                    'name' => $currentIndex,
55 1
                    'artist' => [],
56 1
                ];
57
            }
58
59 1
            $item = [
60 1
                'id' => $artist['id'],
61 1
                'name' => $artist['name'],
62 1
                'coverArt' => $artist['coverArtId'],
63 1
                'artistImageUrl' => $artist['artistImageUrl'],
64 1
                'albumCount' => $artist['albumCount'],
65 1
            ];
66
67 1
            if ($artist['starred'] !== null) {
68 1
                $item['starred'] = $artist['starred']->format(DATE_ATOM);
69
            }
70
71 1
            $indexItem['artist'][] = $item;
72
        }
73
74 1
        if ($indexItem !== []) {
75 1
            $data[] = $indexItem;
76
        }
77
78 1
        return $this->responderFactory->createArtistsResponder(
79 1
            [
80 1
                'ignoredArticles' => implode(' ', $artistListDataProvider->getIgnoredArticles()),
81 1
                'index' => $data,
82 1
            ]
83 1
        );
84
    }
85
}
86