Passed
Push — main ( 3d6de0...a0d988 )
by Daniel
03:41
created

FavoriteAddApplication   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 10
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A run() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\User;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Teapot\StatusCode;
10
use Uxmp\Core\Component\Favorite\FavoriteManagerInterface;
11
use Uxmp\Core\Component\Session\SessionValidatorMiddleware;
12
use Uxmp\Core\Orm\Model\UserInterface;
13
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
14
15
/**
16
 * Mark a song as favorite
17
 */
18
final class FavoriteAddApplication extends AbstractFavoriteApplication
19
{
20 3
    public function __construct(
21
        SongRepositoryInterface $songRepository,
22
        private FavoriteManagerInterface $favoriteManager,
23
    ) {
24 3
        parent::__construct(
25 3
            $songRepository
26
        );
27 3
    }
28
29 3
    protected function run(
30
        ServerRequestInterface $request,
31
        ResponseInterface $response,
32
        array $args
33
    ): ResponseInterface {
34 3
        $obj = $this->getItem($request, $args);
35
36 3
        if ($obj === null) {
37 2
            return $response->withStatus(StatusCode::NOT_FOUND);
38
        }
39
40
        /** @var UserInterface $user */
41 1
        $user = $request->getAttribute(SessionValidatorMiddleware::USER);
42
43 1
        return $this->asJson(
44 1
            $response,
45
            [
46 1
                'result' => $this->favoriteManager->add($obj, $user->getId())
47
            ]
48
        );
49
    }
50
}
51