SearchApplication::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 0
c 2
b 0
f 0
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 6
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Search;
6
7
use JsonSerializable;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Uxmp\Core\Api\AbstractApiApplication;
11
use Uxmp\Core\Api\Lib\Message\JsonEnabledResponseInterface;
12
use Uxmp\Core\Api\Lib\ResultItemFactoryInterface;
13
use Uxmp\Core\Api\Lib\SchemaValidatorInterface;
14
use Uxmp\Core\Lib\StringHelperInterface;
15
use Uxmp\Core\Orm\Model\AlbumInterface;
16
use Uxmp\Core\Orm\Model\ArtistInterface;
17
use Uxmp\Core\Orm\Model\SongInterface;
18
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
19
use Uxmp\Core\Orm\Repository\ArtistRepositoryInterface;
20
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
21
22
/**
23
 * Search for albums, artists and songs
24
 */
25
final class SearchApplication extends AbstractApiApplication
26
{
27
    /**
28
     * @param SchemaValidatorInterface<array{
29
     *  searchTerm: string
30
     * }> $schemaValidator
31
     */
32 1
    public function __construct(
33
        private readonly AlbumRepositoryInterface $albumRepository,
34
        private readonly ArtistRepositoryInterface $artistRepository,
35
        private readonly SongRepositoryInterface $songRepository,
36
        private readonly ResultItemFactoryInterface $resultItemFactory,
37
        private readonly SchemaValidatorInterface $schemaValidator,
38
        private readonly StringHelperInterface $stringHelper,
39
    ) {
40 1
    }
41
42 1
    protected function run(
43
        ServerRequestInterface $request,
44
        JsonEnabledResponseInterface $response,
45
        array $args
46
    ): ResponseInterface {
47 1
        $body = $this->schemaValidator->getValidatedBody(
48 1
            $request,
49 1
            'Search.json',
50 1
        );
51 1
        $searchTerm = $this->stringHelper->filterForSearch($body['searchTerm']);
52
53 1
        return $response->withJson([
54 1
            'items' => [
55 1
                'albums' => array_values(
56 1
                    array_map(
57 1
                        fn (AlbumInterface $album): JsonSerializable => $this->resultItemFactory->createAlbumListItem($album),
58 1
                        $this->albumRepository->search($searchTerm)
59 1
                    )
60 1
                ),
61 1
                'artists' => array_values(
62 1
                    array_map(
63 1
                        fn (ArtistInterface $artist): JsonSerializable => $this->resultItemFactory->createArtistListItem($artist),
64 1
                        $this->artistRepository->search($searchTerm)
65 1
                    )
66 1
                ),
67 1
                'songs' => array_values(
68 1
                    array_map(
69 1
                        fn (SongInterface $song): JsonSerializable => $this->resultItemFactory->createSongListItem($song, $song->getAlbum()),
70 1
                        $this->songRepository->search($searchTerm)
71 1
                    )
72 1
                ),
73 1
            ],
74 1
        ]);
75
    }
76
}
77