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

JmsDoctrineHandler::deserializeRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 4
crap 1.0156
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use JMS\Serializer\Context;
7
use JMS\Serializer\Metadata\ClassMetadata;
8
use JMS\Serializer\Metadata\PropertyMetadata;
9
use JMS\Serializer\VisitorInterface;
10
use ScayTrase\Api\Cruds\Adaptors\DoctrineOrm\EntityToIdNormalizer;
11
12
final class JmsDoctrineHandler
13
{
14
    const TYPE = 'DoctrineAssociation';
15
16
    /** @var  EntityToIdNormalizer */
17
    private $converter;
18
    /**
19
     * @var ManagerRegistry
20
     */
21
    private $registry;
22
23
    /**
24
     * JmsDoctrineHandler constructor.
25
     *
26
     * @param ManagerRegistry $registry
27
     */
28 8
    public function __construct(ManagerRegistry $registry)
29
    {
30 8
        $this->registry  = $registry;
31 8
        $this->converter = new EntityToIdNormalizer($this->registry);
32 8
    }
33
34 8
    public function serializeRelation(VisitorInterface $visitor, $relation, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36 8
        if ($relation instanceof \Traversable) {
37 8
            $relation = iterator_to_array($relation);
38
        }
39
40 8
        if (is_array($relation)) {
41 8
            return array_map([$this->converter, 'normalize'], $relation);
42
        }
43
44 3
        return $this->converter->normalize($relation);
45
    }
46
47 1
    public function deserializeRelation(VisitorInterface $visitor, $data, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49 1
        $metadatas = iterator_to_array($context->getMetadataStack());
50
        /** @var PropertyMetadata $property */
51 1
        $property = array_shift($metadatas);
52
        /** @var ClassMetadata $class */
53 1
        $class = array_shift($metadatas);
54
55 1
        $metadata = $this->registry->getManagerForClass($class->name)->getClassMetadata($class->name);
56
57 1
        return $this->converter->denormalize($data, $metadata->getAssociationTargetClass($property->name));
58
    }
59
}
60