Completed
Push — master ( 31318f...42adad )
by Peter
01:24
created

ErrorDetector::detect()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 5.3846
cc 8
eloc 11
nc 4
nop 3
crap 8
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\WorldArtBrowserBundle\Service;
12
13
use AnimeDb\Bundle\WorldArtBrowserBundle\Exception\BannedException;
14
use AnimeDb\Bundle\WorldArtBrowserBundle\Exception\NotFoundException;
15
use Psr\Http\Message\ResponseInterface;
16
17
class ErrorDetector
18
{
19
    /**
20
     * @param ResponseInterface $response
21
     * @param string            $path
22
     * @param array             $options
23
     *
24
     * @return string
25
     */
26 7
    public function detect(ResponseInterface $response, $path, array $options = [])
27
    {
28 7
        if ($response->getStatusCode() == 404) {
29 1
            throw NotFoundException::page();
30
        }
31
32 6
        $content = $response->getBody()->getContents();
33
34 6
        if ($this->isBanned($content)) {
35 2
            throw BannedException::banned();
36
        }
37
38
        // return anime Akira page if anime not found
39
        // example http://www.world-art.ru/animation/animation.php?id=10000000
40 4
        if (strpos($path, '/animation/animation.php') !== false &&
41
            (
42
                // check ID in options
43 4
                (isset($options['query']['id']) && $this->isNotAkira($id = $options['query']['id'], $content)) ||
44
                // check ID in path
45 4
                (preg_match('/\?id=(\d+)/', $path, $match) && $this->isNotAkira($id = $match[1], $content))
46
            )
47
        ) {
48 2
            throw NotFoundException::anime($id);
49
        }
50
51 2
        return $content;
52
    }
53
54
    /**
55
     * @param string $content
56
     *
57
     * @return bool
58
     */
59 6
    private function isBanned($content)
60
    {
61
        return
62 6
            strpos($content, 'url=http://www.world-art.ru/not_connect.html') !== false ||
63 6
            strpos($content, 'NETGEAR ProSecure') !== false
64
        ;
65
    }
66
67
    /**
68
     * Is not a Akira.
69
     *
70
     * @see http://www.world-art.ru/animation/animation.php?id=1
71
     *
72
     * @param int    $id
73
     * @param string $content
74
     *
75
     * @return bool
76
     */
77 4
    private function isNotAkira($id, $content)
78
    {
79 4
        return $id != 1 && strpos($content, '/animation/img/1000/1/1.jpg') !== false;
80
    }
81
}
82