| @@ 7-39 (lines=33) @@ | ||
| 4 | use Pz\Doctrine\Rest\RestRepository; |
|
| 5 | use Pz\Doctrine\Rest\RestResponseInterface; |
|
| 6 | ||
| 7 | trait DeleteAction |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * Doctrine repository from where get data. |
|
| 11 | * |
|
| 12 | * @return RestRepository |
|
| 13 | */ |
|
| 14 | abstract protected function repository(); |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @return RestResponseInterface |
|
| 18 | */ |
|
| 19 | abstract protected function response(); |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @param DeleteRequestInterface $request |
|
| 23 | * |
|
| 24 | * @return array |
|
| 25 | */ |
|
| 26 | public function delete(DeleteRequestInterface $request) |
|
| 27 | { |
|
| 28 | if (null === ($entity = $this->repository()->find($request->getId()))) { |
|
| 29 | return $this->response()->notFound($request); |
|
| 30 | } |
|
| 31 | ||
| 32 | $request->authorize($entity); |
|
| 33 | ||
| 34 | $this->repository()->em()->remove($entity); |
|
| 35 | $this->repository()->em()->flush(); |
|
| 36 | ||
| 37 | return $this->response()->delete($request, $entity); |
|
| 38 | } |
|
| 39 | } |
|
| 40 | ||
| @@ 8-50 (lines=43) @@ | ||
| 5 | use Pz\Doctrine\Rest\RestResponseInterface; |
|
| 6 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
| 7 | ||
| 8 | trait UpdateAction |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * Doctrine repository from where get data. |
|
| 12 | * |
|
| 13 | * @return RestRepository |
|
| 14 | */ |
|
| 15 | abstract protected function repository(); |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @return RestResponseInterface |
|
| 19 | */ |
|
| 20 | abstract protected function response(); |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @param UpdateRequestInterface $request |
|
| 24 | * @param $entity |
|
| 25 | * |
|
| 26 | * @return object |
|
| 27 | */ |
|
| 28 | abstract protected function updateEntity($request, $entity); |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @param UpdateRequestInterface $request |
|
| 32 | * |
|
| 33 | * @return array |
|
| 34 | * @throws \Doctrine\ORM\EntityNotFoundException |
|
| 35 | */ |
|
| 36 | public function update(UpdateRequestInterface $request) |
|
| 37 | { |
|
| 38 | if (null === ($entity = $this->repository()->find($request->getId()))) { |
|
| 39 | return $this->response()->notFound($request); |
|
| 40 | } |
|
| 41 | ||
| 42 | $request->authorize($entity); |
|
| 43 | ||
| 44 | $this->updateEntity($request, $entity); |
|
| 45 | ||
| 46 | $this->repository()->em()->flush(); |
|
| 47 | ||
| 48 | return $this->response()->update($request, $entity); |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||