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

FavoriteRemoveApplication::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 1
cc 2
nc 2
nop 3
crap 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
 * Remove a item from the users favorites
17
 */
18
final class FavoriteRemoveApplication extends AbstractFavoriteApplication
19
{
20 2
    public function __construct(
21
        SongRepositoryInterface $songRepository,
22
        private FavoriteManagerInterface $favoriteManager,
23
    ) {
24 2
        parent::__construct(
25 2
            $songRepository
26
        );
27 2
    }
28
29 2
    protected function run(
30
        ServerRequestInterface $request,
31
        ResponseInterface $response,
32
        array $args
33
    ): ResponseInterface {
34 2
        $obj = $this->getItem($request, $args);
35
36 2
        if ($obj === null) {
37 1
            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->remove($obj, $user->getId())
47
            ]
48
        );
49
    }
50
}
51