ExceptionValueMap::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
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
 * @internal
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
    private $map;
30
31
    /**
32
     * @param array<string,bool>|array<string,int> $map
33
     */
34 21
    public function __construct(array $map)
35
    {
36 21
        $this->map = $map;
37 21
    }
38
39
    /**
40
     * @return bool|int|null null if not found
41
     */
42 18
    public function resolveFromClassName(string $className)
43
    {
44 18
        foreach ($this->map as $mapClass => $value) {
45 18
            if (!$value) {
46 1
                continue;
47
            }
48
49 18
            if ($className === $mapClass || is_subclass_of($className, $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...
50 14
                return $value;
51
            }
52
        }
53
54 4
        return null;
55
    }
56
}
57