1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace BoneMvc\Module\Dragon\Controller; |
4
|
|
|
|
5
|
|
|
use BoneMvc\Module\Dragon\Collection\DragonCollection; |
6
|
|
|
use BoneMvc\Module\Dragon\Form\DragonForm; |
7
|
|
|
use BoneMvc\Module\Dragon\Service\DragonService; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
11
|
|
|
|
12
|
|
|
class DragonApiController |
13
|
|
|
{ |
14
|
|
|
/** @var DragonService $service */ |
15
|
|
|
private $service; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* DragonController constructor. |
19
|
|
|
* @param DragonService $service |
20
|
|
|
*/ |
21
|
|
|
public function __construct(DragonService $service) |
22
|
|
|
{ |
23
|
|
|
$this->service = $service; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Controller. |
28
|
|
|
* |
29
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
30
|
|
|
* |
31
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
32
|
|
|
*/ |
33
|
|
|
public function indexAction(ServerRequestInterface $request, array $args) : ResponseInterface |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$db = $this->service->getRepository(); |
36
|
|
|
$dragons = new DragonCollection($db->findAll()); |
37
|
|
|
|
38
|
|
|
return new JsonResponse($dragons->toArray()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ServerRequestInterface $request |
43
|
|
|
* @param array $args |
44
|
|
|
* @return ResponseInterface |
45
|
|
|
* @throws \Doctrine\ORM\ORMException |
46
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
47
|
|
|
*/ |
48
|
|
|
public function createAction(ServerRequestInterface $request, array $args) : ResponseInterface |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody(); |
51
|
|
|
$form = new DragonForm('create'); |
52
|
|
|
$form->populate($post); |
53
|
|
|
|
54
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
55
|
|
|
$data = $form->getValues(); |
56
|
|
|
$dragon = $this->service->createFromArray($data); |
57
|
|
|
$this->service->saveDragon($dragon); |
58
|
|
|
|
59
|
|
|
return new JsonResponse($dragon->toArray()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new JsonResponse([ |
63
|
|
|
'error' => [ |
64
|
|
|
'error_messages' => $form->render(), |
65
|
|
|
], |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ServerRequestInterface $request |
71
|
|
|
* @param array $args |
72
|
|
|
* @return ResponseInterface |
73
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException |
74
|
|
|
*/ |
75
|
|
|
public function viewAction(ServerRequestInterface $request, array $args) : ResponseInterface |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$dragon = $this->service->getRepository()->find($args['id']); |
78
|
|
|
|
79
|
|
|
return new JsonResponse($dragon->toArray()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ServerRequestInterface $request |
84
|
|
|
* @param array $args |
85
|
|
|
* @return ResponseInterface |
86
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException |
87
|
|
|
* @throws \Doctrine\ORM\ORMException |
88
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
89
|
|
|
*/ |
90
|
|
|
public function updateAction(ServerRequestInterface $request, array $args) : ResponseInterface |
91
|
|
|
{ |
92
|
|
|
$db = $this->service->getRepository(); |
93
|
|
|
$dragon = $db->find($args['id']); |
94
|
|
|
|
95
|
|
|
$post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody(); |
96
|
|
|
$form = new DragonForm('update'); |
97
|
|
|
$form->populate($post); |
98
|
|
|
|
99
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
100
|
|
|
$data = $form->getValues(); |
101
|
|
|
$dragon = $this->service->updateFromArray($dragon, $data); |
102
|
|
|
$this->service->saveDragon($dragon); |
103
|
|
|
|
104
|
|
|
return new JsonResponse($dragon->toArray()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return new JsonResponse([ |
108
|
|
|
'error' => [ |
109
|
|
|
'error_messages' => $form->render(), |
110
|
|
|
], |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ServerRequestInterface $request |
116
|
|
|
* @param array $args |
117
|
|
|
* @return ResponseInterface |
118
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException |
119
|
|
|
* @throws \Doctrine\ORM\ORMException |
120
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
121
|
|
|
*/ |
122
|
|
|
public function deleteAction(ServerRequestInterface $request, array $args) : ResponseInterface |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$db = $this->service->getRepository(); |
125
|
|
|
$dragon = $db->find($args['id']); |
126
|
|
|
$this->service->deleteDragon($dragon); |
127
|
|
|
return new JsonResponse(['deleted' => true]); |
128
|
|
|
} |
129
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.