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

CourseCreateController::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 12
Ratio 46.15 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 12
loc 26
rs 8.8571
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 CourseCreateController
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
        /** @var Course $course */
81
        $course = $this->requestManager->getDataFromRequestBody($request, Course::class);
82
83
        if (null === $course) {
84
            return $this->responseManager->createResponse($request, 415);
85
        }
86
87 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...
88
            return $this->responseManager->createResponse(
89
                $request,
90
                422,
91
                $this->errorManager->createByValidationErrors(
92
                    $errors,
93
                    $this->requestManager->getAcceptLanguage($request, $this->defaultLanguage),
94
                    Error::SCOPE_BODY,
95
                    'course'
96
                )
97
            );
98
        }
99
100
        $this->repository->persist($course);
101
102
        return $this->responseManager->createResponse($request, 201, $course);
103
    }
104
}
105