Passed
Push — master ( acfdea...483b01 )
by Peter
02:17
created

ErrorDetector::detect()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 1
nop 1
dl 0
loc 28
ccs 15
cts 16
cp 0.9375
crap 7.0119
rs 6.7272
c 0
b 0
f 0
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\AnimeNewsNetworkBrowserBundle\Service;
12
13
use AnimeDb\Bundle\AnimeNewsNetworkBrowserBundle\Exception\NotFoundException;
14
use AnimeDb\Bundle\AnimeNewsNetworkBrowserBundle\Exception\ErrorException;
15
use Psr\Http\Message\ResponseInterface;
16
17
class ErrorDetector
18
{
19
    /**
20
     * @param ResponseInterface $response
21
     *
22
     * @return string
23
     */
24 12
    public function detect(ResponseInterface $response)
25
    {
26 12
        if ($response->getStatusCode() == 404) {
27 1
            throw NotFoundException::page();
28
        }
29
30 11
        $content = $response->getBody()->getContents();
31
32
        // no warnings
33 11
        if (strpos($content, '<ann><warning>') === false) {
34 1
            return $content;
35
        }
36
37 10
        if (preg_match('/<warning>no result for (title|manga|anime)=([^<]*)<\/warning>/i', $content, $match)) {
38 9
            switch ($match[1]) {
39 9
                case 'title':
40 3
                    throw NotFoundException::title($match[2]);
41
                    break;
42 6
                case 'manga':
43 3
                    throw NotFoundException::manga($match[2]);
44
                    break;
45 3
                case 'anime':
46 3
                    throw NotFoundException::anime($match[2]);
47
                    break;
48
            }
49
        }
50
51 1
        throw ErrorException::error(preg_replace('/.*<warning>(.*)<\/warning>.*/im', '$1', $content));
52
    }
53
}
54