Passed
Push — master ( 19a485...913337 )
by Michael
02:43
created

createSingleIdentifierRelationship()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Handler;
5
6
use Mikemirten\Component\JsonApi\Document\IdentifierCollectionRelationship;
7
use Mikemirten\Component\JsonApi\Document\ResourceIdentifierObject;
8
use Mikemirten\Component\JsonApi\Document\ResourceObject;
9
use Mikemirten\Component\JsonApi\Document\SingleIdentifierRelationship;
10
use Mikemirten\Component\JsonApi\Mapper\Definition\Relationship as RelationshipDefinition;
11
use Mikemirten\Component\JsonApi\Mapper\Handler\LinkHandler\LinkHandlerInterface;
12
use Mikemirten\Component\JsonApi\Mapper\MappingContext;
13
14
/**
15
 * Handler of relationships
16
 *
17
 * @package Mikemirten\Component\JsonApi\Mapper\Handler
18
 */
19
class RelationshipHandler implements HandlerInterface
20
{
21
    /**
22
     * Links' handler
23
     *
24
     * @var LinkHandlerInterface
25
     */
26
    protected $linkHandler;
27
28
    /**
29
     * RelationshipHandler constructor.
30
     *
31
     * @param LinkHandlerInterface $linkHandler
32
     */
33 2
    public function __construct(LinkHandlerInterface $linkHandler)
34
    {
35 2
        $this->linkHandler = $linkHandler;
36 2
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function toResource($object, ResourceObject $resource, MappingContext $context)
42
    {
43 2
        $definitions = $context->getDefinition()->getRelationships();
44
45 2
        foreach ($definitions as $definition)
46
        {
47 2
            $relationship = $definition->isCollection()
48 1
                ? $this->createIdentifierCollectionRelationship($object, $definition, $context)
49 2
                : $this->createSingleIdentifierRelationship($object, $definition, $context);
50
51 2
            $resource->setRelationship($definition->getName(), $relationship);
52
        }
53 2
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function fromResource($object, ResourceObject $resource, MappingContext $context)
59
    {
60
        // Do nothing
61
    }
62
63
    /**
64
     * Create relationship contains a single resource-identifier
65
     *
66
     * @param  mixed                  $object
67
     * @param  RelationshipDefinition $definition
68
     * @return SingleIdentifierRelationship
69
     */
70 1
    protected function createSingleIdentifierRelationship($object, RelationshipDefinition $definition, MappingContext $context): SingleIdentifierRelationship
71
    {
72 1
        $relatedObject = $object->{$definition->getGetter()}();
73
74 1
        $identifier   = $this->resolveIdentifier($relatedObject, $definition, $context);
75 1
        $resourceType = $this->resolveType($relatedObject, $definition, $context);
76
77 1
        $resource     = new ResourceIdentifierObject($identifier, $resourceType);
78 1
        $relationship = new SingleIdentifierRelationship($resource);
79
80 1
        $this->linkHandler->handleLinks($object, $definition, $relationship);
81
82 1
        return $relationship;
83
    }
84
85
    /**
86
     * Create relationship contains a collection of resource-identifiers
87
     *
88
     * @param  mixed                  $object
89
     * @param  RelationshipDefinition $definition
90
     * @return IdentifierCollectionRelationship
91
     */
92 1
    protected function createIdentifierCollectionRelationship($object, RelationshipDefinition $definition, MappingContext $context): IdentifierCollectionRelationship
93
    {
94 1
        $relationship = new IdentifierCollectionRelationship();
95 1
        $collection   = $object->{$definition->getGetter()}();
96
97 1
        foreach ($collection as $relatedObject)
98
        {
99 1
            $identifier   = $this->resolveIdentifier($relatedObject, $definition, $context);
100 1
            $resourceType = $this->resolveType($relatedObject, $definition, $context);
101 1
            $resource     = new ResourceIdentifierObject($identifier, $resourceType);
102
103 1
            $relationship->addIdentifier($resource);
104
        }
105
106 1
        $this->linkHandler->handleLinks($object, $definition, $relationship);
107
108 1
        return $relationship;
109
    }
110
111
    /**
112
     * Resolve ID of resource
113
     *
114
     * @param  mixed                  $object
115
     * @param  RelationshipDefinition $definition
116
     * @param  MappingContext         $context
117
     * @return string
118
     */
119 2
    protected function resolveIdentifier($object, RelationshipDefinition $definition, MappingContext $context): string
120
    {
121 2
        if ($definition->hasIdentifierGetter()) {
122 2
            $method = $definition->getIdentifierGetter();
123
124 2
            return (string) $object->$method();
125
        }
126
127
        return $context->getIdentifierHandler()->getIdentifier($object, $context);
128
    }
129
130
    /**
131
     * Resolve type of resource
132
     *
133
     * @param  mixed                  $object
134
     * @param  RelationshipDefinition $definition
135
     * @param  MappingContext         $context
136
     * @return string
137
     */
138 2
    protected function resolveType($object, RelationshipDefinition $definition, MappingContext $context): string
139
    {
140 2
        if ($definition->hasResourceType()) {
141 2
            return $definition->getResourceType();
142
        }
143
144
        return $context->getTypeHandler()->getType($object, $context);
145
    }
146
}