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( |
|
|
|
|
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)) { |
|
|
|
|
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
|
|
|
|
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.