-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
369 additions
and
181 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\ShikimoriBrowserBundle\Exception; | ||
|
||
class ErrorException extends \RuntimeException | ||
{ | ||
/** | ||
* @param string $massage | ||
* @param int $code | ||
* | ||
* @return ErrorException | ||
*/ | ||
public static function failed($massage, $code) | ||
{ | ||
if ($massage) { | ||
return new self(sprintf('Server returned error "%s".', $massage), $code); | ||
} else { | ||
return new self('Server returned an error.', $code); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $massage | ||
* @param int $code | ||
* | ||
* @return ErrorException | ||
*/ | ||
public static function invalidResponse($massage, $code) | ||
{ | ||
return new self(sprintf('Failed to parse response due to "%s" error.', $massage), $code); | ||
} | ||
} |
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
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\ShikimoriBrowserBundle\Exception; | ||
|
||
class NotFoundException extends ErrorException | ||
{ | ||
/** | ||
* @return NotFoundException | ||
*/ | ||
public static function page() | ||
{ | ||
return new self('Page not found.'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
services: | ||
anime_db.shikimori.browser: | ||
class: AnimeDb\Bundle\ShikimoriBrowserBundle\Service\Browser | ||
arguments: [ '@anime_db.shikimori.browser.client', ~, ~, ~ ] | ||
arguments: [ '@anime_db.shikimori.browser.client', '@anime_db.shikimori.browser.error_detector', ~, ~, ~ ] | ||
|
||
anime_db.shikimori.browser.client: | ||
class: GuzzleHttp\Client | ||
public: false | ||
|
||
anime_db.shikimori.browser.error_detector: | ||
class: AnimeDb\Bundle\ShikimoriBrowserBundle\Service\ErrorDetector | ||
public: false |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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\ShikimoriBrowserBundle\Service; | ||
|
||
use AnimeDb\Bundle\ShikimoriBrowserBundle\Exception\ErrorException; | ||
use AnimeDb\Bundle\ShikimoriBrowserBundle\Exception\NotFoundException; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class ErrorDetector | ||
{ | ||
/** | ||
* @param ResponseInterface $response | ||
* | ||
* @return array | ||
*/ | ||
public function detect(ResponseInterface $response) | ||
{ | ||
if ($response->getStatusCode() == 404) { | ||
throw NotFoundException::page(); | ||
} | ||
|
||
$content = $response->getBody()->getContents(); | ||
|
||
if ($content == '') { | ||
return []; | ||
} | ||
|
||
$data = json_decode($content, true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw ErrorException::invalidResponse(json_last_error_msg(), json_last_error()); | ||
} | ||
|
||
if (!empty($data['code'])) { | ||
throw ErrorException::failed( | ||
isset($data['message']) ? $data['message'] : '', | ||
$data['code'] | ||
); | ||
} | ||
|
||
return (array) $data; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.