ExceptionConverter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace jin2chen\ApiBundle\Response;
6
7
use jin2chen\ApiBundle\Response\ExceptionConverter\GenericConverter;
8
use Symfony\Component\DependencyInjection\ServiceLocator;
9
use Throwable;
10
11
use function array_unshift;
12
use function class_parents;
13
use function get_class;
14
15
/**
16
 * @psalm-import-type ExceptionResponseArray from ExceptionConverterInterface
17
 */
18
final class ExceptionConverter implements ExceptionConverterInterface
19
{
20
    private ServiceLocator $converters;
21
    /**
22
     * @var array<string, string>
23
     */
24
    private array $mappings;
25
26
    /**
27
     * ExceptionConverter constructor.
28
     *
29
     * @param ServiceLocator $converters
30
     * @param array<string, string> $mappings
31
     */
32 3
    public function __construct(ServiceLocator $converters, array $mappings = [])
33
    {
34 3
        $this->converters = $converters;
35 3
        $this->mappings = $mappings;
36 3
    }
37
38
    /**
39
     * @param Throwable $e
40
     *
41
     * @return ExceptionConverterInterface
42
     */
43 3
    private function map(Throwable $e): ExceptionConverterInterface
44
    {
45
        /** @var class-string[] $classNames */
46 3
        $classNames = array_values(class_parents($e, false));
47 3
        array_unshift($classNames, get_class($e));
48 3
        $class = GenericConverter::class;
49 3
        foreach ($classNames as $className) {
50 3
            if (isset($this->mappings[$className])) {
51 2
                $class = $this->mappings[$className];
52 2
                break;
53
            }
54
        }
55
56
        /** @noinspection PhpUnnecessaryLocalVariableInspection */
57
        /** @var ExceptionConverterInterface $service */
58 3
        $service = $this->converters->get($class);
59 3
        return $service;
60
    }
61
62
    /**
63
     * @return ExceptionResponseArray
0 ignored issues
show
Bug introduced by
The type jin2chen\ApiBundle\Response\ExceptionResponseArray was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
     */
65 3
    public function convert(Throwable $e): array
66
    {
67 3
        return $this->map($e)->convert($e);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->map($e)->convert($e) returns the type array which is incompatible with the documented return type jin2chen\ApiBundle\Response\ExceptionResponseArray.
Loading history...
68
    }
69
}
70