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