Completed
Push — master ( 2e0cd6...c623e8 )
by Pavel
12:31
created

JmsDoctrineHandler::deserializeRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 4
crap 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use JMS\Serializer\Context;
8
use JMS\Serializer\VisitorInterface;
9
10
final class JmsDoctrineHandler
11
{
12
    const TYPE = 'DoctrineAssociation';
13
14
    /** @var  ManagerRegistry */
15
    private $registry;
16
17
    /**
18
     * JmsDoctrineHandler constructor.
19
     *
20
     * @param ManagerRegistry $registry
21
     */
22 2
    public function __construct(ManagerRegistry $registry)
23
    {
24 2
        $this->registry = $registry;
25 2
    }
26
27 2
    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...
28
    {
29 2
        if ($relation instanceof \Traversable) {
30 2
            $relation = iterator_to_array($relation);
31 2
        }
32
33 2
        if (is_array($relation)) {
34 2
            return array_map([$this, 'convertEntityToIds'], $relation);
35
        }
36
37 1
        return $this->convertEntityToIds($relation);
38
    }
39
40
    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 $data 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...
41
    {
42
        // fixme
43
        throw new \BadMethodCallException('Not supported at the moment');
44
    }
45
46 1 View Code Duplication
    private function convertEntityToIds($entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 1
        $class    = get_class($entity);
49 1
        $metadata = $this->registry->getManagerForClass($class)->getClassMetadata($class);
50
51 1
        $ids = $metadata->getIdentifierValues($entity);
52
53 1
        if (!$metadata instanceof ClassMetadata || $metadata->isIdentifierComposite) {
54
            return $ids;
55
        }
56
57 1
        return array_shift($ids);
58
    }
59
}
60