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

ErrorDetector::detect()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 17
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 1
nop 1
crap 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