@@ 14-70 (lines=57) @@ | ||
11 | use Chubbyphp\ApiSkeleton\Error\ErrorManager; |
|
12 | use Chubbyphp\ApiSkeleton\Model\Course; |
|
13 | ||
14 | final class CourseDeleteController |
|
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 | $this->repository->remove($course); |
|
67 | ||
68 | return $this->responseManager->createResponse($request, 200); |
|
69 | } |
|
70 | } |
|
71 |
@@ 14-68 (lines=55) @@ | ||
11 | use Chubbyphp\ApiSkeleton\Error\ErrorManager; |
|
12 | use Chubbyphp\ApiSkeleton\Model\Course; |
|
13 | ||
14 | 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 |