Completed
Push — master ( 4bbc05...072c74 )
by Peter
01:28
created

ErrorDetector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 0
loc 56
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D detect() 0 31 9
A isNotAkira() 0 4 2
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
        if (
35 6
            strpos($content, 'url=http://www.world-art.ru/not_connect.html') !== false ||
36 6
            strpos($content, 'NETGEAR ProSecure') !== false
37
        ) {
38 2
            throw BannedException::banned();
39
        }
40
41
        // return anime Akira page if anime not found
42
        // example http://www.world-art.ru/animation/animation.php?id=10000000
43 4
        if (strpos($path, '/animation/animation.php') !== false) {
44
            // check ID in options
45 4
            if (isset($options['query']['id']) && $this->isNotAkira($options['query']['id'], $content)) {
46 1
                throw NotFoundException::anime($options['query']['id']);
47
            }
48
49
            // check ID in path
50 3
            if (preg_match('/\?id=(\d+)/', $path, $match) && $this->isNotAkira($match[1], $content)) {
51 1
                throw NotFoundException::anime($match[1]);
52
            }
53
        }
54
55 2
        return $content;
56
    }
57
58
    /**
59
     * Is not a Akira
60
     *
61
     * @see http://www.world-art.ru/animation/animation.php?id=1
62
     *
63
     * @param int    $id
64
     * @param string $content
65
     *
66
     * @return bool
67
     */
68 4
    private function isNotAkira($id, $content)
69
    {
70 4
        return $id != 1 && strpos($content, '/animation/img/1000/1/1.jpg') !== false;
71
    }
72
}
73