Completed
Push — master ( c00bbf...06e52a )
by Pavel
17:08 queued 22s
created

InitializedObjectConstructor::construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 7
cp 0.4286
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 5
crap 4.679
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\VisitorInterface;
6
use JMS\Serializer\Metadata\ClassMetadata;
7
use JMS\Serializer\DeserializationContext;
8
use JMS\Serializer\Construction\ObjectConstructorInterface;
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, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
32
    {
33 2
        if ($context->attributes->containsKey('target') && $context->getDepth() === 1) {
34 2
            return $context->attributes->get('target')->get();
35
        }
36
37
        return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
38
    }
39
}
40