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

CourseUpdateController::__invoke()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 39
Code Lines 22

Duplication

Lines 12
Ratio 30.77 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 1
dl 12
loc 39
rs 8.5806
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\RequestManagerInterface;
8
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
9
use Chubbyphp\Model\RepositoryInterface;
10
use Chubbyphp\Validation\ValidatorInterface;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
use Psr\Http\Message\ResponseInterface as Response;
13
use Chubbyphp\ApiSkeleton\Error\Error;
14
use Chubbyphp\ApiSkeleton\Error\ErrorManager;
15
use Chubbyphp\ApiSkeleton\Model\Course;
16
17
final class CourseUpdateController
18
{
19
    /**
20
     * @var string
21
     */
22
    private $defaultLanguage;
23
24
    /**
25
     * @var ErrorManager
26
     */
27
    private $errorManager;
28
29
    /**
30
     * @var RepositoryInterface
31
     */
32
    private $repository;
33
34
    /**
35
     * @var RequestManagerInterface
36
     */
37
    private $requestManager;
38
39
    /**
40
     * @var ResponseManagerInterface
41
     */
42
    private $responseManager;
43
44
    /**
45
     * @var ValidatorInterface
46
     */
47
    private $validator;
48
49
    /**
50
     * @param string                   $defaultLanguage
51
     * @param ErrorManager             $errorManager
52
     * @param RepositoryInterface      $repository
53
     * @param RequestManagerInterface  $requestManager
54
     * @param ResponseManagerInterface $responseManager
55
     * @param ValidatorInterface       $validator
56
     */
57 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
58
        string $defaultLanguage,
59
        ErrorManager $errorManager,
60
        RepositoryInterface $repository,
61
        RequestManagerInterface $requestManager,
62
        ResponseManagerInterface $responseManager,
63
        ValidatorInterface $validator
64
    ) {
65
        $this->defaultLanguage = $defaultLanguage;
66
        $this->errorManager = $errorManager;
67
        $this->repository = $repository;
68
        $this->requestManager = $requestManager;
69
        $this->responseManager = $responseManager;
70
        $this->validator = $validator;
71
    }
72
73
    /**
74
     * @param Request $request
75
     *
76
     * @return Response
77
     */
78
    public function __invoke(Request $request): Response
79
    {
80
        $id = $request->getAttribute('id');
81
82
        /** @var Course $course */
83
        $course = $this->repository->find($id);
84
85
        if (null === $course) {
86
            return $this->responseManager->createResponse(
87
                $request,
88
                404,
89
                $this->errorManager->createByMissingModel('course', ['id' => $id])
90
            );
91
        }
92
93
        /** @var Course $course */
94
        $course = $this->requestManager->getDataFromRequestBody($request, $course);
95
96
        if (null === $course) {
97
            return $this->responseManager->createResponse($request, 415);
98
        }
99
100 View Code Duplication
        if ([] !== $errors = $this->validator->validateObject($course)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
101
            return $this->responseManager->createResponse(
102
                $request,
103
                422,
104
                $this->errorManager->createByValidationErrors(
105
                    $errors,
106
                    $this->requestManager->getAcceptLanguage($request, $this->defaultLanguage),
107
                    Error::SCOPE_BODY,
108
                    'course'
109
                )
110
            );
111
        }
112
113
        $this->repository->persist($course);
114
115
        return $this->responseManager->createResponse($request, 200, $course);
116
    }
117
}
118