Passed
Pull Request — master (#11)
by Dominik
02:22
created

getObjectMapping()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer;
6
7
use Chubbyphp\Deserialization\DeserializerLogicException;
8
use Chubbyphp\Deserialization\Doctrine\Denormalizer\DenormalizerObjectMappingRegistry as DoctrineDenormalizerObjectMappingRegistry;
9
use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface;
10
use Doctrine\Common\Persistence\Proxy;
11
12
final class DenormalizerObjectMappingRegistry implements DenormalizerObjectMappingRegistryInterface
13
{
14
    /**
15
     * @var DenormalizationObjectMappingInterface[]
16
     */
17
    private $objectMappings;
18
19
    /**
20
     * @param array $objectMappings
21
     */
22 3
    public function __construct(array $objectMappings)
23
    {
24 3
        $this->objectMappings = [];
25 3
        foreach ($objectMappings as $objectMapping) {
26 2
            $this->addObjectMapping($objectMapping);
27
        }
28 3
    }
29
30
    /**
31
     * @param DenormalizationObjectMappingInterface $objectMapping
32
     */
33 2
    private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping)
34
    {
35 2
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
36 2
    }
37
38
    /**
39
     * @param string $class
40
     *
41
     * @return DenormalizationObjectMappingInterface
42
     *
43
     * @throws DeserializerLogicException
44
     */
45 3
    public function getObjectMapping(string $class): DenormalizationObjectMappingInterface
46
    {
47 3
        $reflectionClass = new \ReflectionClass($class);
48
49 3
        if (interface_exists('Doctrine\Common\Persistence\Proxy')
50 3
            && in_array(Proxy::class, $reflectionClass->getInterfaceNames(), true)
51
        ) {
52 1
            $parentClass = $reflectionClass->getParentClass()->name;
53 1
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
54 1
                sprintf(
55 1
                    'Use "%s" instead of "%s" for "%s"',
56 1
                    DoctrineDenormalizerObjectMappingRegistry::class,
57 1
                    self::class,
58 1
                    $parentClass
59
                ),
60 1
                E_USER_DEPRECATED
61
            );
62
63 1
            $class = $parentClass;
64
        }
65
66 3
        if (isset($this->objectMappings[$class])) {
67 2
            return $this->objectMappings[$class];
68
        }
69
70 1
        throw DeserializerLogicException::createMissingMapping($class);
71
    }
72
}
73