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
|
|
|
use Chubbyphp\ApiSkeleton\Search\CourseSearch; |
17
|
|
|
|
18
|
|
|
final class CourseSearchController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $defaultLanguage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ErrorManager |
27
|
|
|
*/ |
28
|
|
|
private $errorManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $repository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RequestManagerInterface |
37
|
|
|
*/ |
38
|
|
|
private $requestManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ResponseManagerInterface |
42
|
|
|
*/ |
43
|
|
|
private $responseManager; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ValidatorInterface |
47
|
|
|
*/ |
48
|
|
|
private $validator; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $defaultLanguage |
52
|
|
|
* @param ErrorManager $errorManager |
53
|
|
|
* @param RepositoryInterface $repository |
54
|
|
|
* @param RequestManagerInterface $requestManager |
55
|
|
|
* @param ResponseManagerInterface $responseManager |
56
|
|
|
* @param ValidatorInterface $validator |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
59
|
|
|
string $defaultLanguage, |
60
|
|
|
ErrorManager $errorManager, |
61
|
|
|
RepositoryInterface $repository, |
62
|
|
|
RequestManagerInterface $requestManager, |
63
|
|
|
ResponseManagerInterface $responseManager, |
64
|
|
|
ValidatorInterface $validator |
65
|
|
|
) { |
66
|
|
|
$this->defaultLanguage = $defaultLanguage; |
67
|
|
|
$this->errorManager = $errorManager; |
68
|
|
|
$this->repository = $repository; |
69
|
|
|
$this->requestManager = $requestManager; |
70
|
|
|
$this->responseManager = $responseManager; |
71
|
|
|
$this->validator = $validator; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Request $request |
76
|
|
|
* |
77
|
|
|
* @return Response |
78
|
|
|
*/ |
79
|
|
|
public function __invoke(Request $request): Response |
80
|
|
|
{ |
81
|
|
|
/** @var CourseSearch $courseSearch */ |
82
|
|
|
$courseSearch = $this->requestManager->getDataFromRequestQuery($request, CourseSearch::class); |
83
|
|
|
|
84
|
|
View Code Duplication |
if ([] !== $errors = $this->validator->validateObject($courseSearch)) { |
|
|
|
|
85
|
|
|
return $this->responseManager->createResponse( |
86
|
|
|
$request, |
87
|
|
|
400, |
88
|
|
|
$this->errorManager->createByValidationErrors( |
89
|
|
|
$errors, |
90
|
|
|
$this->requestManager->getAcceptLanguage($request, $this->defaultLanguage), |
91
|
|
|
Error::SCOPE_BODY, |
92
|
|
|
Course::class |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** @var CourseSearch $courseSearch */ |
98
|
|
|
$courseSearch = $this->repository->search($courseSearch); |
|
|
|
|
99
|
|
|
|
100
|
|
|
return $this->responseManager->createResponse($request, 200, $courseSearch); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.