Skip to content

Commit

Permalink
Adding the adult filter plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 23, 2014
1 parent a08391a commit d334cc1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/Tmdb/HttpClient/Plugin/AdultFilterPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @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);
}
}
38 changes: 38 additions & 0 deletions test/Tmdb/Tests/HttpClient/Plugin/AdultFilterPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @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());
}
}

0 comments on commit d334cc1

Please sign in to comment.