1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BoneMvc\Module\Orc\Controller; |
6
|
|
|
|
7
|
|
|
use BoneMvc\Module\Orc\Collection\OrcCollection; |
8
|
|
|
use BoneMvc\Module\Orc\Form\OrcForm; |
9
|
|
|
use BoneMvc\Module\Orc\Service\OrcService; |
10
|
|
|
use League\Route\Http\Exception\NotFoundException; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
13
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
14
|
|
|
|
15
|
|
View Code Duplication |
class OrcApiController |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** @param OrcService $service */ |
18
|
|
|
private $service; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param OrcService $service |
22
|
|
|
*/ |
23
|
|
|
public function __construct(OrcService $service) |
24
|
|
|
{ |
25
|
|
|
$this->service = $service; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ServerRequestInterface $request |
30
|
|
|
* @param array $args |
31
|
|
|
* @return ResponseInterface |
32
|
|
|
* @throws NotFoundException |
33
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
34
|
|
|
*/ |
35
|
|
|
public function indexAction(ServerRequestInterface $request, array $args): ResponseInterface |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$params = $request->getQueryParams(); |
38
|
|
|
$limit = $params['limit']; |
39
|
|
|
$offset = $params['offset']; |
40
|
|
|
$db = $this->service->getRepository(); |
41
|
|
|
$orcs = new OrcCollection($db->findBy([], null, $limit, $offset)); |
42
|
|
|
$total = $db->getTotalOrcCount(); |
43
|
|
|
$count = count($orcs); |
44
|
|
|
if ($count < 1) { |
45
|
|
|
throw new NotFoundException(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$payload['_embedded'] = $orcs->toArray(); |
|
|
|
|
49
|
|
|
$payload['count'] = $count; |
50
|
|
|
$payload['total'] = $total; |
51
|
|
|
|
52
|
|
|
return new JsonResponse($payload); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ServerRequestInterface $request |
57
|
|
|
* @param array $args |
58
|
|
|
* @return ResponseInterface |
59
|
|
|
* @throws \Doctrine\ORM\ORMException |
60
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
61
|
|
|
*/ |
62
|
|
|
public function createAction(ServerRequestInterface $request, array $args): ResponseInterface |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody(); |
65
|
|
|
$form = new OrcForm('create'); |
66
|
|
|
$form->populate($post); |
67
|
|
|
|
68
|
|
|
if ($form->isValid()) { |
69
|
|
|
$data = $form->getValues(); |
70
|
|
|
$orc = $this->service->createFromArray($data); |
71
|
|
|
$this->service->saveOrc($orc); |
72
|
|
|
|
73
|
|
|
return new JsonResponse($orc->toArray()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new JsonResponse([ |
77
|
|
|
'error' => $form->getErrorMessages(), |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ServerRequestInterface $request |
83
|
|
|
* @param array $args |
84
|
|
|
* @return ResponseInterface |
85
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException |
86
|
|
|
*/ |
87
|
|
|
public function viewAction(ServerRequestInterface $request, array $args): ResponseInterface |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$orc = $this->service->getRepository()->find($args['id']); |
90
|
|
|
|
91
|
|
|
return new JsonResponse($orc->toArray()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ServerRequestInterface $request |
96
|
|
|
* @param array $args |
97
|
|
|
* @return ResponseInterface |
98
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException |
99
|
|
|
* @throws \Doctrine\ORM\ORMException |
100
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
101
|
|
|
*/ |
102
|
|
|
public function updateAction(ServerRequestInterface $request, array $args): ResponseInterface |
103
|
|
|
{ |
104
|
|
|
$db = $this->service->getRepository(); |
105
|
|
|
$orc = $db->find($args['id']); |
106
|
|
|
|
107
|
|
|
$post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody(); |
108
|
|
|
$form = new OrcForm('update'); |
109
|
|
|
$form->populate($post); |
110
|
|
|
|
111
|
|
|
if ($form->isValid()) { |
112
|
|
|
$data = $form->getValues(); |
113
|
|
|
$orc = $this->service->updateFromArray($orc, $data); |
114
|
|
|
$this->service->saveOrc($orc); |
115
|
|
|
|
116
|
|
|
return new JsonResponse($orc->toArray()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return new JsonResponse([ |
120
|
|
|
'error' => $form->getErrorMessages(), |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ServerRequestInterface $request |
126
|
|
|
* @param array $args |
127
|
|
|
* @return ResponseInterface |
128
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException |
129
|
|
|
* @throws \Doctrine\ORM\ORMException |
130
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
131
|
|
|
*/ |
132
|
|
|
public function deleteAction(ServerRequestInterface $request, array $args): ResponseInterface |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$db = $this->service->getRepository(); |
135
|
|
|
$orc = $db->find($args['id']); |
136
|
|
|
$this->service->deleteOrc($orc); |
137
|
|
|
|
138
|
|
|
return new JsonResponse(['deleted' => true]); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.