<?php
/**
* Created by PhpStorm.
* User: matijajanc
* Date: 23/05/2018
* Time: 09:26
*/
namespace App\Adapter\Api\EventListener;
use App\Adapter\Api\Exception\ApiException;
use App\Adapter\Api\Service\Response\ResponseManager;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class ApiExceptionListener
{
/**
* @var ResponseManager
*/
private $response;
/**
* ApiExceptionListener constructor.
* @param ResponseManager $response
*/
public function __construct(ResponseManager $response)
{
$this->response = $response;
}
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if (!$exception instanceof ApiException) {
return;
}
$message = json_decode($exception->getMessage(), true);
$event->setResponse(
$this->response->error(
$message[0] ?? '',
$message[1] ?? '',
$message[2] ?? '',
$exception->getCode()
)
);
}
}