Passed
Branch 1.1 (4974bd)
by Peter
01:58
created

ErrorDetector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C detect() 0 32 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\SmotretAnimeBrowserBundle\Service;
12
13
use AnimeDb\Bundle\SmotretAnimeBrowserBundle\Exception\ErrorException;
14
use AnimeDb\Bundle\SmotretAnimeBrowserBundle\Exception\NotFoundException;
15
use Psr\Http\Message\ResponseInterface;
16
17
class ErrorDetector
18
{
19
    /**
20
     * @param ResponseInterface $response
21
     *
22
     * @return array
23
     */
24 7
    public function detect(ResponseInterface $response)
25
    {
26 7
        if ($response->getStatusCode() == 404) {
27 1
            throw NotFoundException::page();
28
        }
29
30 6
        $content = $response->getBody()->getContents();
31
32
        // http://smotret-anime.ru/api/episodes/100000000000
33 6
        if ($content == '') {
34 1
            throw NotFoundException::page();
35
        }
36
37 5
        $data = json_decode($content, true);
38
39 5
        if (json_last_error() !== JSON_ERROR_NONE) {
40 1
            throw ErrorException::invalidResponse(json_last_error_msg(), json_last_error());
0 ignored issues
show
Bug introduced by
The function json_last_error_msg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            throw ErrorException::invalidResponse(/** @scrutinizer ignore-call */ json_last_error_msg(), json_last_error());
Loading history...
41
        }
42
43 4
        if (isset($data['error'])) {
44 3
            $code = isset($data['error']['code']) ? $data['error']['code'] : 0;
45 3
            if ($code == 404) {
46 1
                throw NotFoundException::page();
47
            } else {
48 2
                throw ErrorException::failed(
49 2
                    isset($data['error']['message']) ? $data['error']['message'] : '',
50
                    $code
51 2
                );
52
            }
53
        }
54
55 1
        return (array) $data;
56
    }
57
}
58