From ae88a75bc6134537c043c70f577d12ddb3e73771 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Mon, 24 Feb 2014 00:40:28 +0100 Subject: [PATCH] Adding the LanguageFilterPlugin --- .../Plugin/LanguageFilterPlugin.php | 40 +++++++++++++++++++ .../HttpClient/Plugin/LanguageFilterTest.php | 38 ++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.php create mode 100644 test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.php diff --git a/lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.php b/lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.php new file mode 100644 index 00000000..fd29835c --- /dev/null +++ b/lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.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 LanguageFilterPlugin implements EventSubscriberInterface +{ + private $language; + + public function __construct($language = 'en') + { + $this->language = $language; + } + + public static function getSubscribedEvents() + { + return array('request.before_send' => 'onBeforeSend'); + } + + public function onBeforeSend(Event $event) + { + $url = $event['request']->getUrl(true); + + $url->getQuery()->set('language', $this->language); + + $event['request']->setUrl($url); + } +} diff --git a/test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.php b/test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.php new file mode 100644 index 00000000..55b04f03 --- /dev/null +++ b/test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.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\LanguageFilterPlugin; +use Tmdb\Tests\TestCase; + +class LanguageFilterTest extends TestCase +{ + /** + * @test + */ + public function shouldAddToken() + { + $request = new Request('GET', '/'); + + $event = new Event(); + $event['request'] = $request; + + $plugin = new LanguageFilterPlugin(); + + $plugin->onBeforeSend($event); + + $this->assertEquals('/?language=en', $event['request']->getUrl()); + } +} \ No newline at end of file