Completed
Push — master ( fc0e6a...80104d )
by Peter
01:54
created

ErrorDetector::detect()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 11
nc 4
nop 1
crap 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\AniDbBrowserBundle\Util;
12
13
use AnimeDb\Bundle\AniDbBrowserBundle\Exception\BannedException;
14
use AnimeDb\Bundle\AniDbBrowserBundle\Exception\NotFoundException;
15
use AnimeDb\Bundle\AniDbBrowserBundle\Exception\ErrorException;
16
17
class ErrorDetector
18
{
19
    /**
20
     * @param string $response
21
     *
22
     * @return string
23
     */
24 4
    public function detect($response)
25
    {
26 4
        if (stripos($response, '<error>') === false) {
27 1
            return $response;
28
        }
29
30 3
        $error = preg_replace('/^.*<error>([^<]+)<\/error>.*$/ims', '$1', $response);
31
32
        switch ($error) {
33 3
            case 'Banned':
34 1
                throw BannedException::banned();
35 2
            case 'Anime not found':
36 1
                throw NotFoundException::anime();
37
            default:
38 1
                throw ErrorException::error($error);
39
        }
40
    }
41
}
42