Completed
Push — master ( 207f95...fc0e6a )
by Peter
04:33
created

ErrorDetector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 77.78%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 25
ccs 7
cts 9
cp 0.7778
rs 10
c 3
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A detect() 0 17 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>/im', '$1', $response);
31
32
        switch ($error) {
33 3
            case 'Banned':
34
                throw BannedException::banned();
35 3
            case 'Anime not found':
36
                throw NotFoundException::anime();
37
            default:
38 3
                throw ErrorException::error($error);
39
        }
40
    }
41
}
42