Skip to content

Instantly share code, notes, and snippets.

@tristanbes
Last active December 19, 2015 22:29
Show Gist options
  • Save tristanbes/6028105 to your computer and use it in GitHub Desktop.
Save tristanbes/6028105 to your computer and use it in GitHub Desktop.
<?php
namespace Tristanbes\ElophantBundle\EventListener;
use Guzzle\Http\Exception\BadResponseException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Tristanbes\ElophantBundle\Manager\StatsManager;
/**
* Class GuzzleExceptionListener
*/
class GuzzleExceptionListener
{
private $statsManager;
private $dispatcher;
private $fail = false;
/**
* Constructor
*
* @param StatsManager $manager The stats Manager
* @param
*/
public function __construct(StatsManager $manager, EventDispatcher $dispatcher)
{
$this->statsManager = $manager;
$this->dispatcher = $dispatcher;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$statsManager = $this->statsManager;
if ($exception instanceof BadResponseException) {
$this->dispatcher->addListener('kernel.terminate', function (Event $event) use ($statsManager) {
$statsManager->addFail();
});
}
}
}
<service id="tristanbes_elophant.guzzle_exception_eventlistener" class="%tristanbes_elophant.guzzle.exception.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
<argument type="service" id="tristanbes_elophant.stats.manager" />
<argument type="service" id="event_dispatcher" />
</service>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment