CourseDeleteController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 55
loc 55
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A __invoke() 17 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiSkeleton\Controller\Course;
6
7
use Chubbyphp\ApiHttp\Manager\RequestManagerInterface;
8
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
9
use Chubbyphp\Model\RepositoryInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Psr\Http\Message\ResponseInterface as Response;
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 RepositoryInterface
18
     */
19
    private $repository;
20
21
    /**
22
     * @var RequestManagerInterface
23
     */
24
    private $requestManager;
25
26
    /**
27
     * @var ResponseManagerInterface
28
     */
29
    private $responseManager;
30
31
    /**
32
     * @param RepositoryInterface      $repository
33
     * @param RequestManagerInterface  $requestManager
34
     * @param ResponseManagerInterface $responseManager
35
     */
36
    public function __construct(
37
        RepositoryInterface $repository,
38
        RequestManagerInterface $requestManager,
39
        ResponseManagerInterface $responseManager
40
    ) {
41
        $this->repository = $repository;
42
        $this->requestManager = $requestManager;
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
        if (null === $accept = $this->requestManager->getAccept($request)) {
54
            return $this->responseManager->createAcceptNotSupportedResponse($request);
55
        }
56
57
        $id = $request->getAttribute('id');
58
59
        /** @var Course $course */
60
        if (null === $course = $this->repository->find($id)) {
61
            return $this->responseManager->createResourceNotFoundResponse($request, $accept, 'course', ['id' => $id]);
62
        }
63
64
        $this->repository->remove($course);
65
66
        return $this->responseManager->createResponse($request, 200, $accept);
67
    }
68
}
69