<?php
/**
* @license SILK SOFTWARE HOUSE SP Z O O
*/
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class AppErrorSubscriber.
*/
class AppErrorSubscriber implements EventSubscriberInterface
{
/**
* Device ping path.
*
* @var string[]
*/
private $encryptedPaths;
/**
* RequestDecryptingSubscriber constructor.
*
* @param string[] $encryptedPaths Array of encrypted paths.
*/
public function __construct(array $encryptedPaths)
{
$this->encryptedPaths = $encryptedPaths;
}
/**
* Get subscribed events.
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => ['onKernelResponse'],
];
}
/**
* @param ResponseEvent $event
*/
public function onKernelResponse(ResponseEvent $event)
{
if (!$event->isMasterRequest() || !in_array($event->getRequest()->getPathInfo(), $this->encryptedPaths)) {
return;
}
$response = $event->getResponse();
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
return;
}
$responseBody = json_decode($response->getContent(), true);
$newResponse = [
'result' => false,
'message' => Response::$statusTexts[$response->getStatusCode()] ?? '',
];
if (400 === $response->getStatusCode()) {
$newResponse['errors'] = $responseBody;
}
$response->setContent(json_encode($newResponse));
$event->setResponse($response);
}
}