Completed
Pull Request — master (#2101)
by Christian
15:10 queued 05:49
created

ExceptionValueMap::resolveThrowable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
 * @final since 2.8
21
 */
22
class ExceptionValueMap
23
{
24
    /**
25
     * Map of values mapped to exception class
26
     * key => exception class
27
     * value => value associated with exception.
28
     *
29
     * @var array
30
     */
31
    private $map;
32
33
    /**
34 15
     * @param array $map
35
     */
36 15
    public function __construct(array $map)
37 15
    {
38
        $this->map = $map;
39
    }
40
41
    /**
42
     * Resolves the value corresponding to an exception object.
43
     *
44
     * @param \Exception $exception
45
     *
46 12
     * @return mixed|false Value found or false is not found
47
     */
48 12
    public function resolveException(\Throwable $exception)
49
    {
50
        return $this->doResolveClass(get_class($exception));
51
    }
52
53
    /**
54
     * Resolves the value corresponding to an exception class.
55
     *
56
     * @param string $class
57
     *
58 12
     * @return mixed|false if not found
59
     */
60 12
    private function doResolveClass($class)
61 9
    {
62 1
        foreach ($this->map as $mapClass => $value) {
63
            if (!$value) {
64
                continue;
65 9
            }
66 6
67
            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...
68 10
                return $value;
69
            }
70 10
        }
71
72
        return false;
73
    }
74
}
75