Completed
Pull Request — master (#1380)
by Guilh
05:41
created

ExceptionValueMap::doResolveClass()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 8.8571
cc 5
eloc 7
nc 4
nop 1
crap 5
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Util;
13
14
/**
15
 * Stores map of values mapped to exception class
16
 * Resolves value by exception.
17
 *
18
 * @author Mikhail Shamin <[email protected]>
19
 */
20
class ExceptionValueMap
21
{
22
    /**
23
     * Map of values mapped to exception class
24
     * key => exception class
25
     * value => value associated with exception.
26
     *
27
     * @var array
28
     */
29
    private $map;
30
31
    /**
32
     * @param array $map
33
     */
34 12
    public function __construct(array $map)
35
    {
36 12
        $this->map = $map;
37 12
    }
38
39
    /**
40
     * Resolves the value corresponding to an exception object.
41
     *
42
     * @param \Exception $exception
43
     *
44
     * @return mixed|false Value found or false is not found
45
     */
46 9
    public function resolveException(\Exception $exception)
47
    {
48 9
        return $this->doResolveClass(get_class($exception));
49
    }
50
51
    /**
52
     * Resolves the value corresponding to an exception class.
53
     *
54
     * @param string $class
55
     *
56
     * @return mixed|false if not found
57
     */
58 9
    private function doResolveClass($class)
59
    {
60 9
        foreach ($this->map as $mapClass => $value) {
61 7
            if (!$value) {
62 1
                continue;
63
            }
64
65 7
            if ($class === $mapClass || is_subclass_of($class, $mapClass)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $mapClass can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
66 6
                return $value;
67
            }
68 7
        }
69
70 7
        return false;
71
    }
72
}
73