ErrorDetector::detect()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 1
nop 1
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 1
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 8
    public function detect(ResponseInterface $response)
25
    {
26 8
        if ($response->getStatusCode() == 404) {
27 1
            throw NotFoundException::page();
28
        }
29
30 7
        $content = $response->getBody()->getContents();
31
32
        // no warnings
33 7
        if (strpos($content, '<ann><warning>') === false) {
34 1
            return $content;
35
        }
36
37 6
        if (preg_match('/<warning>no result for (?<source>[a-z]+)=(?<id>[^<]*)<\/warning>/i', $content, $match)) {
38 5
            throw NotFoundException::source($match['source'], $match['id']);
39
        }
40
41 1
        throw ErrorException::error(preg_replace('/.*<warning>(.*)<\/warning>.*/im', '$1', $content));
42
    }
43
}
44