Test Failed
Push — master ( f562c0...ed7d3a )
by Dominik
02:11
created

CourseDeleteController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiSkeleton\Controller\Course;
6
7
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
8
use Chubbyphp\Model\RepositoryInterface;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Psr\Http\Message\ResponseInterface as Response;
11
use Chubbyphp\ApiSkeleton\Error\ErrorManager;
12
use Chubbyphp\ApiSkeleton\Model\Course;
13
14 View Code Duplication
final class CourseDeleteController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var ErrorManager
18
     */
19
    private $errorManager;
20
21
    /**
22
     * @var RepositoryInterface
23
     */
24
    private $repository;
25
26
    /**
27
     * @var ResponseManagerInterface
28
     */
29
    private $responseManager;
30
31
    /**
32
     * @param ErrorManager             $errorManager
33
     * @param RepositoryInterface      $repository
34
     * @param ResponseManagerInterface $responseManager
35
     */
36
    public function __construct(
37
        ErrorManager $errorManager,
38
        RepositoryInterface $repository,
39
        ResponseManagerInterface $responseManager
40
    ) {
41
        $this->errorManager = $errorManager;
42
        $this->repository = $repository;
43
        $this->responseManager = $responseManager;
44
    }
45
46
    /**
47
     * @param Request $request
48
     *
49
     * @return Response
50
     */
51
    public function __invoke(Request $request): Response
52
    {
53
        $id = $request->getAttribute('id');
54
55
        /** @var Course $course */
56
        $course = $this->repository->find($id);
57
58
        if (null === $course) {
59
            return $this->responseManager->createResponse(
60
                $request,
61
                404,
62
                $this->errorManager->createByMissingModel('course', ['id' => $id])
63
            );
64
        }
65
66
        $this->repository->remove($course);
67
68
        return $this->responseManager->createResponse($request, 200);
69
    }
70
}
71