<?php
/**
* Created by PhpStorm.
* User: rostandnj
* Date: 22/7/19
* Time: 5:22 PM
*/
namespace App\EventSubscriber;
use App\Mercure\CookieGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class ResponseSuscriber implements EventSubscriberInterface
{
private $container;
/**
* ResponseSuscriber constructor.
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelResponse(ResponseEvent $event)
{
$response = $event->getResponse();
$data =$response->getContent();
$currentReq = $this->container->get("request_stack")->getCurrentRequest();
if($currentReq->getRequestUri() !== "/" )
{
if($response->getStatusCode()===201)
{
$xp=$currentReq->headers->get('X-PK');
if($xp==null) {
$response->setStatusCode(403);
$response->setContent(json_encode(["message"=>"please add X-PK in your request header","code"=>401]));
}
else
{
$xpk = base64_decode($xp);
$r = $this->container->get( "hash_service")->virgilEncrypt(json_encode($data),$xpk);
if($r["code"]==201)
{
$json = json_encode($r,1);
// var_dump($r);
$response->setContent($json);
}
else
{
$json = json_encode(["message"=>$r["message"]],1);
$response->setStatusCode(401);
$response->setContent($json);
}
}
}
else
{
if($response->getStatusCode()==404)
{
$response->setContent(json_encode(["message"=>"route not found","code"=>404]));
}
}
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => 'onKernelResponse',
];
}
}