Passed
Push — main ( cade59...7a0180 )
by Daniel
03:32
created

SearchApplication   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 19
dl 0
loc 45
ccs 26
cts 26
cp 1
rs 10
c 2
b 0
f 0

2 Methods

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