ErrorDetector   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A detect() 0 18 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\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