Created
July 18, 2013 09:19
-
-
Save tristanbes/6027931 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\HttpKernel\Event\GetResponseForExceptionEvent; | |
use Tristanbes\ElophantBundle\Manager\StatsManager; | |
/** | |
* Class GuzzleExceptionListener | |
*/ | |
class GuzzleExceptionListener | |
{ | |
private $statsManager; | |
private $fail = false; | |
/** | |
* Constructor | |
* | |
* @param StatsManager $manager The stats Manager | |
*/ | |
public function __construct(StatsManager $manager) | |
{ | |
$this->statsManager = $manager; | |
} | |
public function onKernelException(GetResponseForExceptionEvent $event) | |
{ | |
$exception = $event->getException(); | |
if ($exception instanceof BadResponseException) { | |
$this->fail = true; | |
$response = new JsonResponse(array('success' => false)); | |
$event->setResponse($response); | |
} | |
} | |
public function onKernelTerminate() | |
{ | |
if (false === $this->fail) { | |
return; | |
} | |
$this->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" /> | |
<tag name="kernel.event_listener" event="kernel.terminate" method="onKernelTerminate" /> | |
<argument type="service" id="tristanbes_elophant.stats.manager" /> | |
</service> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment