Test Failed
Push — master ( 385400...b007ca )
by Michael
02:12
created

RelationshipProcessor::createRelationship()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 13
nc 4
nop 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor;
5
6
use Mikemirten\Component\JsonApi\Mapper\Definition\Annotation\Relationship as RelationshipAnnotation;
7
use Mikemirten\Component\JsonApi\Mapper\Definition\Definition;
8
use Mikemirten\Component\JsonApi\Mapper\Definition\Relationship;
9
10
/**
11
 * Processor of relationships
12
 *
13
 * @package Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor
14
 */
15
class RelationshipProcessor extends AbstractProcessor
16
{
17
    /**
18
     * Process properties of class
19
     *
20
     * @param \ReflectionClass $reflection
21
     * @param Definition       $definition
22
     */
23
    public function process(\ReflectionClass $reflection, Definition $definition): void
24
    {
25
        foreach ($reflection->getProperties() as $property)
26
        {
27
            $this->processProperty($property, $definition);
28
        }
29
    }
30
31
    /**
32
     * Process property of class
33
     *
34
     * @param \ReflectionProperty $property
35
     * @param Definition          $definition
36
     */
37
    protected function processProperty(\ReflectionProperty $property, Definition $definition)
38
    {
39
        $annotations = $this->reader->getPropertyAnnotations($property);
40
41
        foreach ($annotations as $annotation)
42
        {
43
            if ($annotation instanceof RelationshipAnnotation) {
44
                $relationship = $this->createRelationship($annotation, $property);
45
46
                $definition->addRelationship($relationship);
47
            }
48
        }
49
    }
50
51
    /**
52
     * Process relationship
53
     *
54
     * @param  RelationshipAnnotation $annotation
55
     * @param  \ReflectionProperty    $property
56
     * @return Relationship
57
     */
58
    protected function createRelationship(RelationshipAnnotation $annotation, \ReflectionProperty $property): Relationship
59
    {
60
        $name = ($annotation->name === null)
61
            ? $property->getName()
62
            : $annotation->name;
63
64
        $type = $this->resolveType($annotation);
65
66
        $getter = ($annotation->getter === null)
67
            ? $this->resolveGetter($property)
68
            : $annotation->getter;
69
70
        $relationship = new Relationship($name, $type, $getter);
71
        $relationship->setPropertyName($property->getName());
72
73
        $this->handleLinks($annotation, $relationship);
74
        $this->handleDataControl($annotation, $relationship);
75
76
        return $relationship;
77
    }
78
79
    /**
80
     * Handle links
81
     *
82
     * @param RelationshipAnnotation $annotation
83
     * @param Relationship           $relationship
84
     */
85
    protected function handleLinks(RelationshipAnnotation $annotation, Relationship $relationship)
86
    {
87
        foreach ($annotation->links as $linkAnnotation)
88
        {
89
            $link = $this->createLink($linkAnnotation);
90
91
            $relationship->addLink($link);
92
        }
93
    }
94
95
    /**
96
     * Handle control of data-section
97
     *
98
     * @param RelationshipAnnotation $annotation
99
     * @param Relationship           $relationship
100
     */
101
    protected function handleDataControl(RelationshipAnnotation $annotation, Relationship $relationship)
102
    {
103
        $relationship->setIncludeData($annotation->dataAllowed);
104
        $relationship->setDataLimit($annotation->dataLimit);
105
    }
106
107
    /**
108
     * Resolve type of relationship
109
     *
110
     * @param  RelationshipAnnotation $annotation
111
     * @return int
112
     */
113
    protected function resolveType(RelationshipAnnotation $annotation): int
114
    {
115
        if ($annotation->type === RelationshipAnnotation::TYPE_ONE) {
116
            return Relationship::TYPE_X_TO_ONE;
117
        }
118
119
        if ($annotation->type === RelationshipAnnotation::TYPE_MANY) {
120
            return Relationship::TYPE_X_TO_MANY;
121
        }
122
123
        throw new \LogicException(sprintf('Invalid type of relation "%s" defined.', $annotation->type));
124
    }
125
}