Last active
December 19, 2015 22:29
-
-
Save tristanbes/6028105 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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