statusCode; } /** * @param int $statusCode [http code] */ protected function setStatusCode(int $statusCode): self { $this->statusCode = $statusCode; return $this; } /** * @return string */ protected function getType(): string { return $this->type; } /** * @param string $type * * @return self */ protected function setType(string $type): self { $this->type = $type; return $this; } /** * @return string */ protected function getKey(): string { return $this->key; } /** * @param string $key * * @return string */ protected function setKey(string $key): self { $this->key = $key; return $this; } /** * @return string */ protected function getMethod(): string { return $this->method; } /** * @param string $method * * @return self */ protected function setMethod(string $method): self { $this->method = $method; return $this; } /** * @return string|int|array|object */ protected function getData() { return $this->data; } /** * @param string|int|array|object $data * * @return self */ protected function setData($data): self { $this->data = $data; return $this; } /** * {@inheritDoc} */ public function statusUnauthorized(): self { return $this->setStatusCode(Response::HTTP_UNAUTHORIZED); } /** * {@inheritDoc} */ public function statusUnprocessableEntity(): self { return $this->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY); } /** * {@inheritDoc} */ public function statusInternalServerError(): self { return $this->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); } /** * {@inheritDoc} */ public function statusNotFound(): self { return $this->setStatusCode(Response::HTTP_NOT_FOUND); } /** * {@inheritDoc} */ public function statusForbidden(): self { return $this->setStatusCode(Response::HTTP_FORBIDDEN); } /** * {@inheritDoc} */ public function statusOK(): self { return $this->setStatusCode(Response::HTTP_OK); } /** * {@inheritDoc} */ public function statusCreated(): self { return $this->setStatusCode(Response::HTTP_CREATED); } /** * {@inheritDoc} */ public function createBody(array $data): self { isset($data['type']) ? $this->setType($data['type']) : null; isset($data['key']) ? $this->setKey($data['key']) : null; isset($data['method']) ? $this->setMethod($data['method']) : null; isset($data['data']) ? $this->setData($data['data']) : null; return $this; } /** * {@inheritDoc} */ public function createBodyObject(CommonResponseView $view): self { $this->setType($view->getType()); $this->setKey($view->getKey()); $this->setMethod($view->getMethod()); $this->setData($view->getData()); return $this; } /** * {@inheritDoc} */ public function respond(array $headers = [], string $type = 'json') { $responseContent = [ 'type' => $this->getType(), 'key' => $this->getKey(), 'method' => $this->getMethod(), 'data' => $this->getData(), ]; switch ($type) { case 'json': $response = new JsonResponse($responseContent, $this->getStatusCode(), $headers); break; case 'binary': $response = new BinaryFileResponse($responseContent['data']); break; default: $responseContent['data'] = "Unhandled response type of $type ! Please verify your response configuration."; $response = new JsonResponse($responseContent, $this->getStatusCode(), $headers); } return $response; } /** * {@inheritDoc} */ public function respondSendFile(array $headers = [], string $type = 'json') { switch ($type) { case 'json': $response = new JsonResponse('', $this->getStatusCode(), $headers); break; case 'binary': $response = new BinaryFileResponse(''); break; default: $responseContent['data'] = "Unhandled response type of $type ! Please verify your response configuration."; $response = new JsonResponse($responseContent, $this->getStatusCode(), $headers); } return $response; } /** * {@inheritDoc} */ public function checkResponse(array $response) { if (isset($response['data'])) { $result = $response['data']; if (!isset($response['type']) || 'success' !== $response['type']) { $result = new Exception("An error occured : $result."); } } else { $result = new Exception('Response doesn\'t satisfies requirements !'); } return $result; } /** * {@inheritDoc} */ public function checkProviderResponse(array $response) { if (isset($response['data'])) { if (isset($response['type'])) { if ('success' === $response['type']) { return $response['data']; } else { throw new InternalServerErrorMaestroException($response['data']); } } else { throw new UnprocessableEntityMaestroException("type"); } } else { throw new UnprocessableEntityMaestroException("data"); } } }