Passed
Pull Request — master (#11)
by Pavel
08:05
created

RelationsHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Adapters\JMS;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use JMS\Serializer\Context;
8
use JMS\Serializer\JsonDeserializationVisitor;
9
use JMS\Serializer\JsonSerializationVisitor;
10
11
final class RelationsHandler
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $manager;
17
18
    /**
19
     * RelationsHandler constructor.
20
     *
21
     * @param EntityManagerInterface $manager
22
     */
23
    public function __construct(EntityManagerInterface $manager)
24
    {
25
        $this->manager = $manager;
26
    }
27
28
    public function serializeRelation(JsonSerializationVisitor $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...
29
    {
30
        if ($relation instanceof \Traversable) {
31
            $relation = iterator_to_array($relation);
32
        }
33
34
        if (is_array($relation)) {
35
            return array_map([$this, 'getSingleEntityRelation'], $relation);
36
        }
37
38
        return $this->getSingleEntityRelation($relation);
39
    }
40
41
    public function deserializeRelation(JsonDeserializationVisitor $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 $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...
42
    {
43
        $className = isset($type['params'][0]['name']) ? $type['params'][0]['name'] : null;
44
45
        if (!class_exists($className)) {
46
            throw new \InvalidArgumentException('Class name should be explicitly set for deserialization');
47
        }
48
49
        /** @var ClassMetadata $metadata */
50
        $metadata = $this->manager->getClassMetadata($className);
51
52
        if (!is_array($relation)) {
53
            return $this->manager->getReference($className, $relation);
54
        }
55
56
        $single = false;
57
        if ($metadata->isIdentifierComposite) {
58
            $single = true;
59
            foreach ($metadata->getIdentifierFieldNames() as $idName) {
60
                $single = $single && array_key_exists($idName, $relation);
61
            }
62
        }
63
64
        if ($single) {
65
            return $this->manager->getReference($className, $relation);
66
        }
67
68
        $objects = [];
69
        foreach ($relation as $idSet) {
70
            $objects[] = $this->manager->getReference($className, $idSet);
71
        }
72
73
        return $objects;
74
    }
75
76
    /**
77
     * @param $relation
78
     *
79
     * @return array|mixed
80
     */
81
    private function getSingleEntityRelation($relation)
82
    {
83
        $metadata = $this->manager->getClassMetadata(get_class($relation));
84
85
        $ids = $metadata->getIdentifierValues($relation);
86
        if (!$metadata->isIdentifierComposite) {
0 ignored issues
show
Bug introduced by
Accessing isIdentifierComposite on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
87
            $ids = array_shift($ids);
88
        }
89
90
        return $ids;
91
    }
92
}
93