From d334cc1d4766382e4992c5345e048a9915882783 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Mon, 24 Feb 2014 00:32:56 +0100 Subject: [PATCH] Adding the adult filter plugin --- .../HttpClient/Plugin/AdultFilterPlugin.php | 40 +++++++++++++++++++ .../Plugin/AdultFilterPluginTest.php | 38 ++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 lib/Tmdb/HttpClient/Plugin/AdultFilterPlugin.php create mode 100644 test/Tmdb/Tests/HttpClient/Plugin/AdultFilterPluginTest.php diff --git a/lib/Tmdb/HttpClient/Plugin/AdultFilterPlugin.php b/lib/Tmdb/HttpClient/Plugin/AdultFilterPlugin.php new file mode 100644 index 00000000..166d022f --- /dev/null +++ b/lib/Tmdb/HttpClient/Plugin/AdultFilterPlugin.php @@ -0,0 +1,40 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\HttpClient\Plugin; + +use Guzzle\Common\Event; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class AdultFilterPlugin implements EventSubscriberInterface +{ + private $includeAdult; + + public function __construct($includeAdult = false) + { + $this->includeAdult = $includeAdult; + } + + public static function getSubscribedEvents() + { + return array('request.before_send' => 'onBeforeSend'); + } + + public function onBeforeSend(Event $event) + { + $url = $event['request']->getUrl(true); + + $url->getQuery()->set('include_adult', $this->includeAdult === true ? 'true' : 'false'); + + $event['request']->setUrl($url); + } +} diff --git a/test/Tmdb/Tests/HttpClient/Plugin/AdultFilterPluginTest.php b/test/Tmdb/Tests/HttpClient/Plugin/AdultFilterPluginTest.php new file mode 100644 index 00000000..ee177bfa --- /dev/null +++ b/test/Tmdb/Tests/HttpClient/Plugin/AdultFilterPluginTest.php @@ -0,0 +1,38 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\HttpClient\Plugin; + +use Guzzle\Common\Event; +use Guzzle\Http\Message\Request; +use Tmdb\HttpClient\Plugin\AdultFilterPlugin; +use Tmdb\Tests\TestCase; + +class AdultFilterPluginTest extends TestCase +{ + /** + * @test + */ + public function shouldAddToken() + { + $request = new Request('GET', '/'); + + $event = new Event(); + $event['request'] = $request; + + $plugin = new AdultFilterPlugin(false); + + $plugin->onBeforeSend($event); + + $this->assertEquals('/?include_adult=false', $event['request']->getUrl()); + } +} \ No newline at end of file