Test Failed
Push — main ( 3531fe...1eb5c2 )
by Daniel
04:35
created

SearchApplication::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 22
rs 9.8333
c 1
b 0
f 0
cc 1
nc 1
nop 3
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\Orm\Model\AlbumInterface;
13
use Uxmp\Core\Orm\Model\ArtistInterface;
14
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
15
use Uxmp\Core\Orm\Repository\ArtistRepositoryInterface;
16
17
/**
18
 * Search for anything
19
 */
20
final class SearchApplication extends AbstractApiApplication
21
{
22
    public function __construct(
23
        private readonly AlbumRepositoryInterface $albumRepository,
24
        private readonly ArtistRepositoryInterface $artistRepository,
25
        private readonly ResultItemFactoryInterface $resultItemFactory,
26
    ) {
27
    }
28
29
    protected function run(
30
        ServerRequestInterface $request,
31
        JsonEnabledResponseInterface $response,
32
        array $args
33
    ): ResponseInterface {
34
        $searchTerm = trim($request->getParsedBody()['searchTerm']);
35
36
        $albums = $this->albumRepository->search($searchTerm);
37
        $artists = $this->artistRepository->search($searchTerm);
38
39
        return $response->withJson([
40
            'items' => [
41
                'albums' => array_values(
42
                    array_map(
43
                        fn (AlbumInterface $album) => $this->resultItemFactory->createAlbumListItem($album),
44
                        $albums
45
                    )
46
                ),
47
                'artists' => array_values(
48
                    array_map(
49
                        fn (ArtistInterface $artist) => $this->resultItemFactory->createArtistListItem($artist),
50
                        $artists
51
                    )
52
                ),
53
            ],
54
        ]);
55
    }
56
}
57