src/EventSubscriber/ResponseSuscriber.php line 33

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: rostandnj
  5.  * Date: 22/7/19
  6.  * Time: 5:22 PM
  7.  */
  8. namespace App\EventSubscriber;
  9. use App\Mercure\CookieGenerator;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  16. class ResponseSuscriber implements  EventSubscriberInterface
  17. {
  18.     private $container;
  19.     /**
  20.      * ResponseSuscriber constructor.
  21.      */
  22.     public function __construct(ContainerInterface $container)
  23.     {
  24.         $this->container $container;
  25.     }
  26.     public function onKernelResponse(ResponseEvent $event)
  27.     {
  28.         $response $event->getResponse();
  29.         $data =$response->getContent();
  30.         $currentReq $this->container->get("request_stack")->getCurrentRequest();
  31.         if($currentReq->getRequestUri() !== "/" )
  32.         {
  33.                 if($response->getStatusCode()===201)
  34.                 {
  35.                     $xp=$currentReq->headers->get('X-PK');
  36.                     if($xp==null) {
  37.                         $response->setStatusCode(403);
  38.                         $response->setContent(json_encode(["message"=>"please add X-PK in your request header","code"=>401]));
  39.                     }
  40.                     else
  41.                     {
  42.                         $xpk base64_decode($xp);
  43.                         $r $this->container->get"hash_service")->virgilEncrypt(json_encode($data),$xpk);
  44.                         if($r["code"]==201)
  45.                         {
  46.                             $json json_encode($r,1);
  47.                            // var_dump($r);
  48.                            $response->setContent($json);
  49.                         }
  50.                         else
  51.                         {
  52.                             $json json_encode(["message"=>$r["message"]],1);
  53.                             $response->setStatusCode(401);
  54.                             $response->setContent($json);
  55.                         }
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     if($response->getStatusCode()==404)
  61.                     {
  62.                         $response->setContent(json_encode(["message"=>"route not found","code"=>404]));
  63.                     }
  64.                 }
  65.         }
  66.     }
  67.     public static function getSubscribedEvents()
  68.     {
  69.         return [
  70.             KernelEvents::RESPONSE => 'onKernelResponse',
  71.         ];
  72.     }
  73. }