InitializedObjectConstructor::construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.4624

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 3
cts 11
cp 0.2727
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 5
crap 6.4624
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\Construction\ObjectConstructorInterface;
6
use JMS\Serializer\DeserializationContext;
7
use JMS\Serializer\Metadata\ClassMetadata;
8
use JMS\Serializer\VisitorInterface;
9
10
/**
11
 * Object constructor that allows deserialization into already constructed
12
 * objects passed through the deserialization context
13
 */
14
final class InitializedObjectConstructor implements ObjectConstructorInterface
15
{
16
    private $fallbackConstructor;
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param ObjectConstructorInterface $fallbackConstructor Fallback object constructor
22
     */
23 13
    public function __construct(ObjectConstructorInterface $fallbackConstructor)
24
    {
25 13
        $this->fallbackConstructor = $fallbackConstructor;
26 13
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function construct(VisitorInterface $visitor,
32
                              ClassMetadata $metadata,
33
                              $data,
34
                              array $type,
35
                              DeserializationContext $context
36
    ) {
37 2
        if ($context->attributes->containsKey('target') && $context->getDepth() === 1) {
38 2
            return $context->attributes->get('target')->get();
39
        }
40
41
        return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
42
    }
43
}
44