| @@ 24-99 (lines=76) @@ | ||
| 21 | /** |
|
| 22 | * Class PatchResource. |
|
| 23 | */ |
|
| 24 | class PatchResource |
|
| 25 | { |
|
| 26 | use ResponseTrait; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @var \NilPortugues\Api\JsonApi\Server\Errors\ErrorBag |
|
| 30 | */ |
|
| 31 | protected $errorBag; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @var JsonApiSerializer |
|
| 35 | */ |
|
| 36 | protected $serializer; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @param JsonApiSerializer $serializer |
|
| 40 | */ |
|
| 41 | public function __construct(JsonApiSerializer $serializer) |
|
| 42 | { |
|
| 43 | $this->serializer = $serializer; |
|
| 44 | $this->errorBag = new ErrorBag(); |
|
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * @param string $id |
|
| 49 | * @param array $data |
|
| 50 | * @param string $className |
|
| 51 | * @param callable $findOneCallable |
|
| 52 | * @param callable $update |
|
| 53 | * |
|
| 54 | * @return \Symfony\Component\HttpFoundation\Response |
|
| 55 | */ |
|
| 56 | public function get($id, array $data, $className, callable $findOneCallable, callable $update) |
|
| 57 | { |
|
| 58 | try { |
|
| 59 | DataObject::assertPatch($data, $this->serializer, $className, $this->errorBag); |
|
| 60 | $model = $findOneCallable(); |
|
| 61 | ||
| 62 | if (empty($model)) { |
|
| 63 | $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); |
|
| 64 | ||
| 65 | return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); |
|
| 66 | } |
|
| 67 | ||
| 68 | $values = DataObject::getAttributes($data, $this->serializer); |
|
| 69 | $update($model, $values, $this->errorBag); |
|
| 70 | ||
| 71 | $response = $this->resourceUpdated($this->serializer->serialize($model)); |
|
| 72 | } catch (Exception $e) { |
|
| 73 | $response = $this->getErrorResponse($e); |
|
| 74 | } |
|
| 75 | ||
| 76 | return $response; |
|
| 77 | } |
|
| 78 | ||
| 79 | /** |
|
| 80 | * @param $e |
|
| 81 | * |
|
| 82 | * @return \Symfony\Component\HttpFoundation\Response |
|
| 83 | */ |
|
| 84 | protected function getErrorResponse(Exception $e) |
|
| 85 | { |
|
| 86 | switch (get_class($e)) { |
|
| 87 | case DataException::class: |
|
| 88 | $response = $this->unprocessableEntity($this->errorBag); |
|
| 89 | break; |
|
| 90 | ||
| 91 | default: |
|
| 92 | $response = $this->errorResponse( |
|
| 93 | new ErrorBag([new Error('Bad Request', 'Request could not be served.')]) |
|
| 94 | ); |
|
| 95 | } |
|
| 96 | ||
| 97 | return $response; |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||
| @@ 24-99 (lines=76) @@ | ||
| 21 | /** |
|
| 22 | * Class PutResource. |
|
| 23 | */ |
|
| 24 | class PutResource |
|
| 25 | { |
|
| 26 | use ResponseTrait; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @var \NilPortugues\Api\JsonApi\Server\Errors\ErrorBag |
|
| 30 | */ |
|
| 31 | protected $errorBag; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @var JsonApiSerializer |
|
| 35 | */ |
|
| 36 | protected $serializer; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @param JsonApiSerializer $serializer |
|
| 40 | */ |
|
| 41 | public function __construct(JsonApiSerializer $serializer) |
|
| 42 | { |
|
| 43 | $this->serializer = $serializer; |
|
| 44 | $this->errorBag = new ErrorBag(); |
|
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * @param $id |
|
| 49 | * @param array $data |
|
| 50 | * @param $className |
|
| 51 | * @param callable $findOneCallable |
|
| 52 | * @param callable $update |
|
| 53 | * |
|
| 54 | * @return \Symfony\Component\HttpFoundation\Response |
|
| 55 | */ |
|
| 56 | public function get($id, array $data, $className, callable $findOneCallable, callable $update) |
|
| 57 | { |
|
| 58 | try { |
|
| 59 | DataObject::assertPut($data, $this->serializer, $className, $this->errorBag); |
|
| 60 | ||
| 61 | $model = $findOneCallable(); |
|
| 62 | ||
| 63 | if (empty($model)) { |
|
| 64 | $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); |
|
| 65 | ||
| 66 | return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); |
|
| 67 | } |
|
| 68 | $values = DataObject::getAttributes($data, $this->serializer); |
|
| 69 | $update($model, $values, $this->errorBag); |
|
| 70 | ||
| 71 | $response = $this->resourceUpdated($this->serializer->serialize($model)); |
|
| 72 | } catch (Exception $e) { |
|
| 73 | $response = $this->getErrorResponse($e); |
|
| 74 | } |
|
| 75 | ||
| 76 | return $response; |
|
| 77 | } |
|
| 78 | ||
| 79 | /** |
|
| 80 | * @param Exception $e |
|
| 81 | * |
|
| 82 | * @return \Symfony\Component\HttpFoundation\Response |
|
| 83 | */ |
|
| 84 | protected function getErrorResponse(Exception $e) |
|
| 85 | { |
|
| 86 | switch (get_class($e)) { |
|
| 87 | case DataException::class: |
|
| 88 | $response = $this->unprocessableEntity($this->errorBag); |
|
| 89 | break; |
|
| 90 | ||
| 91 | default: |
|
| 92 | $response = $this->errorResponse( |
|
| 93 | new ErrorBag([new Error('Bad Request', 'Request could not be served.')]) |
|
| 94 | ); |
|
| 95 | } |
|
| 96 | ||
| 97 | return $response; |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||