Passed
Push — master ( a58594...89ad36 )
by Peter
02:41
created

ErrorDetector::wrap()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 2
nop 1
crap 4
1
<?php
2
3
/**
4
 * AnimeDb package.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Service;
12
13
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\BannedException;
14
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\ErrorException;
15
use AnimeDb\Bundle\MyAnimeListBrowserBundle\Exception\NotFoundException;
16
use GuzzleHttp\Exception\ClientException;
17
use Psr\Http\Message\ResponseInterface;
18
19
class ErrorDetector
20
{
21
    /**
22
     * @param \Exception $exception
23
     *
24
     * @return ErrorException|NotFoundException
25
     */
26 4
    public function wrap(\Exception $exception)
27
    {
28 4
        if ($exception instanceof ClientException &&
29 4
            $exception->getResponse() instanceof ResponseInterface &&
30 4
            $exception->getResponse()->getStatusCode() == 404
31
        ) {
32 1
            return NotFoundException::page();
33
        }
34
35 3
        return ErrorException::wrap($exception);
36
    }
37
38
    /**
39
     * @param ResponseInterface $response
40
     *
41
     * @return string
42
     */
43 3
    public function detect(ResponseInterface $response)
44
    {
45 3
        if ($response->getStatusCode() == 404) {
46 1
            throw NotFoundException::page();
47
        }
48
49 2
        $content = $response->getBody()->getContents();
50
51 2
        if (strpos($content, 'Access has been restricted for this account.') !== false) {
52 1
            throw BannedException::banned();
53
        }
54
55 1
        return $content;
56
    }
57
}
58