src/Utils/ResponseTrait.php line 103

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use JMS\Serializer\SerializationContext;
  4. use JMS\Serializer\Serializer;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. trait ResponseTrait
  9. {
  10.     /** @var Serializer */
  11.     private $serializer;
  12.     protected $context;
  13.     public function __construct(ContainerInterface $container)
  14.     {
  15.         $this->serializer $container->get('jms_serializer');
  16.     }
  17.     public function setContext($groups)
  18.     {
  19.         $this->context = new SerializationContext();
  20.         $this->context->setGroups($groups);
  21.     }
  22.     private function getResponse($json$httpCode)
  23.     {
  24.         return Response::create(
  25.             $json,
  26.             $httpCode,
  27.             [
  28.                 'Content-Type' => 'application/json',
  29.             ]
  30.         );
  31.     }
  32.     private function failedResult($message null$code null$httpCode null$addFields = [])
  33.     {
  34.         $responseData = [
  35.             'success' => false,
  36.             'error' => $code ?: Response::HTTP_NOT_FOUND,
  37.             'error_description' => $message,
  38.         ];
  39.         if(sizeof($addFields))
  40.             foreach($addFields as $k => $v)
  41.                 $responseData[$k] = $v;
  42.         $json $this->serializer->serialize(
  43.             $responseData,
  44.             'json'
  45.         );
  46.         return $this->getResponse($json$httpCode);
  47.     }
  48.     private function successResult($message null$code null$data null$httpCode null)
  49.     {
  50.         $json $this->serializer->serialize(
  51.             [
  52.                 'success' => true,
  53.                 'code' => $code,
  54.                 'message' => $message,
  55.                 'data' => $data,
  56.             ],
  57.             'json',
  58.             $this->context
  59.         );
  60.         return $this->getResponse($json$httpCode);
  61.     }
  62.     public function statusNotFound($error null$code null)
  63.     {
  64.         return $this->failedResult($error$codeResponse::HTTP_NOT_FOUND);
  65.     }
  66.     public function statusAccessDenied(
  67.         $message 'You have no permission for this action.',
  68.         $code JsonResponse::HTTP_FORBIDDEN
  69.     ) {
  70.         return $this->failedResult($message$codeResponse::HTTP_FORBIDDEN);
  71.     }
  72.     public function statusNotAuthorized($message 'You are not authorized')
  73.     {
  74.         return $this->failedResult($messageResponse::HTTP_UNAUTHORIZEDResponse::HTTP_UNAUTHORIZED);
  75.     }
  76.     public function statusOk($message null$code null$data null)
  77.     {
  78.         return $this->successResult($message$code$dataResponse::HTTP_OK);
  79.     }
  80.     public function statusCreated(
  81.         $message null,
  82.         $code null,
  83.         $data null
  84.     ) {
  85.         return $this->successResult($message$code$dataResponse::HTTP_CREATED);
  86.     }
  87.     public function statusDeleted($message null$code null)
  88.     {
  89.         return $this->successResult($message$codenullResponse::HTTP_OK);
  90.     }
  91.     public function statusUnprocessedEntity($message null$code null)
  92.     {
  93.         return $this->failedResult($message$codeResponse::HTTP_UNPROCESSABLE_ENTITY);
  94.     }
  95.     public function statusBadRequest($message null$code null$addFields = [])
  96.     {
  97.         return $this->failedResult($message$codeResponse::HTTP_BAD_REQUEST$addFields);
  98.     }
  99.     public function statusConflict($message null$code null$addFields = [])
  100.     {
  101.         return $this->failedResult($message$codeResponse::HTTP_CONFLICT$addFields);
  102.     }
  103. }