ExceptionObjectAccess   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSetterMapping() 0 3 1
A __construct() 0 4 1
A getGetterMapping() 0 23 3
1
<?php
2
3
4
namespace Apie\CorePlugin\ObjectAccess;
5
6
use Apie\CorePlugin\ObjectAccess\Getters\CodeGetter;
7
use Apie\CorePlugin\ObjectAccess\Getters\ErrorsGetter;
8
use Apie\CorePlugin\ObjectAccess\Getters\MessageGetter;
9
use Apie\ObjectAccessNormalizer\Exceptions\ValidationException;
10
use Apie\ObjectAccessNormalizer\ObjectAccess\ObjectAccess;
11
use ReflectionClass;
12
13
class ExceptionObjectAccess extends ObjectAccess
14
{
15
    /**
16
     * @var bool
17
     */
18
    private $debug;
19
20
    public function __construct(bool $debug)
21
    {
22
        $this->debug = $debug;
23
        parent::__construct(true, true);
24
    }
25
26
    /**
27
     * Excepions are always immutable.
28
     *
29
     * @param ReflectionClass $reflectionClass
30
     * @return array
31
     */
32
    protected function getSetterMapping(ReflectionClass $reflectionClass): array
33
    {
34
        return [];
35
    }
36
37
    protected function getGetterMapping(ReflectionClass $reflectionClass): array
38
    {
39
        $mapping = parent::getGetterMapping($reflectionClass);
40
        $mapping['message'] = [new MessageGetter()];
41
        $mapping['code'] = [new CodeGetter()];
42
        unset($mapping['file']);
43
        unset($mapping['line']);
44
        unset($mapping['previous']);
45
        if ($this->debug) {
46
            $mapping['trace'] = $mapping['traceAsString'];
47
        } else {
48
            unset($mapping['trace']);
49
        }
50
        unset($mapping['traceAsString']);
51
        if ($reflectionClass->name === ValidationException::class) {
52
            $mapping['errors'] = [new ErrorsGetter()];
53
            unset($mapping['exceptions']);
54
            unset($mapping['i18n']);
55
            unset($mapping['errorBag']);
56
            unset($mapping['headers']);
57
            unset($mapping['statusCode']);
58
        }
59
        return $mapping;
60
    }
61
}
62