src/Controller/Api/ApiController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. class ApiController extends AbstractController
  9. {
  10.   /**
  11.    * @var integer HTTP status code - 200 (OK) by default
  12.    */
  13.   protected $statusCode 200;
  14.   /** @var EntityManagerInterface $em */
  15.   protected $em;
  16.   /** @var ParameterBagInterface $parameterBag */
  17.   protected $parameterBag;
  18.   /**
  19.    * ApiController constructor.
  20.    * @param ParameterBagInterface $parameterBag
  21.    */
  22.   public function __construct(EntityManagerInterface $emParameterBagInterface $parameterBag)
  23.   {
  24.     $this->em $em;
  25.     $this->parameterBag $parameterBag;
  26.   }
  27.   /**
  28.    * Gets the value of statusCode.
  29.    *
  30.    * @return integer
  31.    */
  32.   public function getStatusCode()
  33.   {
  34.     return $this->statusCode;
  35.   }
  36.   /**
  37.    * Sets the value of statusCode.
  38.    *
  39.    * @param integer $statusCode the status code
  40.    *
  41.    * @return self
  42.    */
  43.   protected function setStatusCode($statusCode)
  44.   {
  45.     $this->statusCode $statusCode;
  46.     return $this;
  47.   }
  48.   /**
  49.    * Returns a JSON response
  50.    *
  51.    * @param array $data
  52.    * @param array $headers
  53.    *
  54.    * @return JsonResponse
  55.    */
  56.   public function response($data$headers = [])
  57.   {
  58.     return new JsonResponse($data$this->getStatusCode(), $headers);
  59.   }
  60.   /**
  61.    * Sets an error message and returns a JSON response
  62.    *
  63.    * @param string $errors
  64.    * @param $headers
  65.    * @return JsonResponse
  66.    */
  67.   public function respondWithErrors($errors$headers = [])
  68.   {
  69.     $data = [
  70.       'status' => $this->getStatusCode(),
  71.       'errors' => $errors,
  72.     ];
  73.     return new JsonResponse($data$this->getStatusCode(), $headers);
  74.   }
  75.   /**
  76.    * Sets an error message and returns a JSON response
  77.    *
  78.    * @param string $success
  79.    * @param $headers
  80.    * @return JsonResponse
  81.    */
  82.   public function respondWithSuccess($success$headers = [])
  83.   {
  84.     $data = [
  85.       'status' => $this->getStatusCode(),
  86.       'success' => $success,
  87.     ];
  88.     return new JsonResponse($data$this->getStatusCode(), $headers);
  89.   }
  90.   /**
  91.    * Returns a 401 Unauthorized http response
  92.    *
  93.    * @param string $message
  94.    *
  95.    * @return JsonResponse
  96.    */
  97.   public function respondUnauthorized($message 'Not authorized!')
  98.   {
  99.     return $this->setStatusCode(401)->respondWithErrors($message);
  100.   }
  101.   /**
  102.    * Returns a 422 Unprocessable Entity
  103.    *
  104.    * @param string $message
  105.    *
  106.    * @return JsonResponse
  107.    */
  108.   public function respondValidationError($message 'Validation errors')
  109.   {
  110.     return $this->setStatusCode(422)->respondWithErrors($message);
  111.   }
  112.   /**
  113.    * Returns a 404 Not Found
  114.    *
  115.    * @param string $message
  116.    *
  117.    * @return JsonResponse
  118.    */
  119.   public function respondNotFound($message 'Not found!')
  120.   {
  121.     return $this->setStatusCode(404)->respondWithErrors($message);
  122.   }
  123.   /**
  124.    * Returns a 201 Created
  125.    *
  126.    * @param array $data
  127.    *
  128.    * @return JsonResponse
  129.    */
  130.   public function respondCreated($data = [])
  131.   {
  132.     return $this->setStatusCode(201)->response($data);
  133.   }
  134.   // this method allows us to accept JSON payloads in POST requests
  135.   // since Symfony 4 doesn’t handle that automatically:
  136.   protected function transformJsonBody(\Symfony\Component\HttpFoundation\Request $request)
  137.   {
  138.     $data json_decode($request->getContent(), true);
  139.     if ($data === null) {
  140.       return $request;
  141.     }
  142.     $request->request->replace($data);
  143.     return $request;
  144.   }
  145. }