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

JmsDoctrineHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 69.57%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 48
ccs 16
cts 23
cp 0.6957
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serializeRelation() 0 12 3
A deserializeRelation() 0 12 1
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