Skip to content

Commit

Permalink
create detector for detect errors in response
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jul 27, 2017
1 parent e4384ad commit a585948
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 32 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ Get info for anime [Cowboy Bebop](https://myanimelist.net/anime/1/Cowboy_Bebop):
$content = $browser->get('/anime/1');
```

Catch exceptions

```php
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\BannedException;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\NotFoundException;

try {
$content = $browser->get('/anime/1');
} catch (BannedException $e) {
// you are banned
} catch (NotFoundException $e) {
// page not found
} catch (\Exception $e) {
// other exceptions
}
```

You can customize request options. See [Guzzle Documentation](http://docs.guzzlephp.org/en/stable/request-options.html).

License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration(new Configuration(), $configs);

$container->getDefinition('anime_db.my_anime_list.browser')
->replaceArgument(1, $config['host'])
->replaceArgument(2, $config['client'])
->replaceArgument(2, $config['host'])
->replaceArgument(3, $config['client'])
;
}
}
22 changes: 22 additions & 0 deletions src/Exception/BannedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* AnimeDb package.
*
* @author Peter Gribanov <[email protected]>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
*/

namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception;

class BannedException extends ErrorException
{
/**
* @return BannedException
*/
public static function banned()
{
return new self('Access has been restricted for this account.');
}
}
15 changes: 15 additions & 0 deletions src/Exception/ErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* AnimeDb package.
*
* @author Peter Gribanov <[email protected]>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
*/

namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception;

class ErrorException extends \RuntimeException
{
}
22 changes: 22 additions & 0 deletions src/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* AnimeDb package.
*
* @author Peter Gribanov <[email protected]>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
*/

namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception;

class NotFoundException extends ErrorException
{
/**
* @return NotFoundException
*/
public static function page()
{
return new self('Page not found.');
}
}
10 changes: 9 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
services:
anime_db.my_anime_list.browser:
class: AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\Browser
arguments: [ '@anime_db.my_anime_list.browser.client', ~, ~ ]
arguments:
- '@anime_db.my_anime_list.browser.client'
- '@anime_db.my_anime_list.browser.error_detector'
- ~
- ~

anime_db.my_anime_list.browser.error_detector:
class: AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\ErrorDetector
public: false

anime_db.my_anime_list.browser.client:
class: GuzzleHttp\Client
Expand Down
23 changes: 14 additions & 9 deletions src/Service/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@

namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Client as HttpClient;

class Browser
{
/**
* @var Client
* @var HttpClient
*/
private $client;

/**
* @var ErrorDetector
*/
private $detector;

/**
* @var string
*/
Expand All @@ -30,13 +35,15 @@ class Browser
private $app_client;

/**
* @param Client $client
* @param string $host
* @param string $app_client
* @param HttpClient $client
* @param ErrorDetector $detector
* @param string $host
* @param string $app_client
*/
public function __construct(Client $client, $host, $app_client)
public function __construct(HttpClient $client, ErrorDetector $detector, $host, $app_client)
{
$this->client = $client;
$this->detector = $detector;
$this->host = $host;
$this->app_client = $app_client;
}
Expand All @@ -58,8 +65,6 @@ public function get($path, array $options = [])

$response = $this->client->request('GET', $this->host.$path, $options);

$content = $response->getBody()->getContents();

return $content;
return $this->detector->detect($response);
}
}
38 changes: 38 additions & 0 deletions src/Service/ErrorDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* AnimeDb package.
*
* @author Peter Gribanov <[email protected]>
* @copyright Copyright (c) 2011, Peter Gribanov
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
*/

namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Service;

use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\BannedException;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\NotFoundException;
use Psr\Http\Message\ResponseInterface;

class ErrorDetector
{
/**
* @param ResponseInterface $response
*
* @return string
*/
public function detect(ResponseInterface $response)
{
if ($response->getStatusCode() == 404) {
throw NotFoundException::page();
}

$content = $response->getBody()->getContents();

if (strpos($content, 'Access has been restricted for this account.') !== false) {
throw BannedException::banned();
}

return $content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ public function testLoad(array $config, $host, $client)
$browser
->expects($this->at(0))
->method('replaceArgument')
->with(1, $host)
->with(2, $host)
->will($this->returnSelf())
;
$browser
->expects($this->at(1))
->method('replaceArgument')
->with(2, $client)
->with(3, $client)
->will($this->returnSelf())
;

Expand Down
31 changes: 13 additions & 18 deletions tests/Service/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Tests\Service;

use AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\Browser;
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\ErrorDetector;
use GuzzleHttp\Client as HttpClient;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\ResponseInterface;

class BrowserTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -33,14 +33,14 @@ class BrowserTest extends \PHPUnit_Framework_TestCase
private $client;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|StreamInterface
* @var \PHPUnit_Framework_MockObject_MockObject|ResponseInterface
*/
private $stream;
private $response;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|MessageInterface
* @var \PHPUnit_Framework_MockObject_MockObject|ErrorDetector
*/
private $message;
private $detector;

/**
* @var Browser
Expand All @@ -50,10 +50,10 @@ class BrowserTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->client = $this->getMock(HttpClient::class);
$this->stream = $this->getMock(StreamInterface::class);
$this->message = $this->getMock(MessageInterface::class);
$this->response = $this->getMock(ResponseInterface::class);
$this->detector = $this->getMock(ErrorDetector::class);

$this->browser = new Browser($this->client, $this->host, $this->app_client);
$this->browser = new Browser($this->client, $this->detector, $this->host, $this->app_client);
}

/**
Expand Down Expand Up @@ -89,23 +89,18 @@ public function testGet($app_client)

$content = 'Hello, world!';

$this->stream
$this->detector
->expects($this->once())
->method('getContents')
->method('detect')
->with($this->response)
->will($this->returnValue($content))
;

$this->message
->expects($this->once())
->method('getBody')
->will($this->returnValue($this->stream))
;

$this->client
->expects($this->once())
->method('request')
->with('GET', $this->host.$path, $options)
->will($this->returnValue($this->message))
->will($this->returnValue($this->response))
;

$this->assertEquals($content, $this->browser->get($path, $params));
Expand Down
Loading

0 comments on commit a585948

Please sign in to comment.