Completed
Push — develop ( dcfc04...3d10a6 )
by Neomerx
04:33
created

Schema::createRelationshipRepresentationFromData()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 5.0795

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 29
cts 34
cp 0.8529
rs 8.4961
c 0
b 0
f 0
cc 5
nc 5
nop 3
crap 5.0795

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Limoncello\Flute\Schema;
2
3
/**
4
 * Copyright 2015-2018 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Application\ModelInterface;
20
use Limoncello\Contracts\Data\ModelSchemaInfoInterface;
21
use Limoncello\Contracts\Data\RelationshipTypes;
22
use Limoncello\Flute\Contracts\Models\PaginatedDataInterface;
23
use Limoncello\Flute\Contracts\Schema\JsonSchemasInterface;
24
use Limoncello\Flute\Contracts\Schema\SchemaInterface;
25
use Limoncello\Flute\Contracts\Validation\JsonApiQueryParserInterface;
26
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
27
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface;
28
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
29
use Neomerx\JsonApi\Schema\BaseSchema;
30
use Neomerx\JsonApi\Schema\Identifier;
31
32
/**
33
 * @package Limoncello\Flute
34
 */
35
abstract class Schema extends BaseSchema implements SchemaInterface
36
{
37
    /**
38
     * @var ModelSchemaInfoInterface
39
     */
40
    private $modelSchemas;
41
42
    /**
43
     * @var JsonSchemasInterface
44
     */
45
    private $jsonSchemas;
46
47
    /**
48
     * @param FactoryInterface         $factory
49
     * @param JsonSchemasInterface     $jsonSchemas
50
     * @param ModelSchemaInfoInterface $modelSchemas
51
     */
52 9
    public function __construct(
53
        FactoryInterface $factory,
54
        JsonSchemasInterface $jsonSchemas,
55
        ModelSchemaInfoInterface $modelSchemas
56
    ) {
57 9
        assert(empty(static::TYPE) === false);
58
59 9
        parent::__construct($factory);
60
61 9
        $this->modelSchemas = $modelSchemas;
62 9
        $this->jsonSchemas  = $jsonSchemas;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function getType(): string
69
    {
70 1
        return static::TYPE;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public static function getAttributeMapping(string $jsonName): string
77
    {
78
        return static::getMappings()[static::SCHEMA_ATTRIBUTES][$jsonName];
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 2
    public static function getRelationshipMapping(string $jsonName): string
85
    {
86 2
        return static::getMappings()[static::SCHEMA_RELATIONSHIPS][$jsonName];
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 6 View Code Duplication
    public static function hasAttributeMapping(string $jsonName): bool
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...
93
    {
94 6
        $mappings = static::getMappings();
95
96
        return
97 6
            array_key_exists(static::SCHEMA_ATTRIBUTES, $mappings) === true &&
98 6
            array_key_exists($jsonName, $mappings[static::SCHEMA_ATTRIBUTES]) === true;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 4 View Code Duplication
    public static function hasRelationshipMapping(string $jsonName): bool
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...
105
    {
106 4
        $mappings = static::getMappings();
107
108
        return
109 4
            array_key_exists(static::SCHEMA_RELATIONSHIPS, $mappings) === true &&
110 4
            array_key_exists($jsonName, $mappings[static::SCHEMA_RELATIONSHIPS]) === true;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getAttributes($model): iterable
117
    {
118
        $attributes = [];
119
        $mappings   = static::getMappings();
120
        if (array_key_exists(static::SCHEMA_ATTRIBUTES, $mappings) === true) {
121
            $attrMappings = $mappings[static::SCHEMA_ATTRIBUTES];
122
123
            // `id` is a `special` attribute and cannot be included in JSON API resource
124
            unset($attrMappings[static::RESOURCE_ID]);
125
126
            foreach ($attrMappings as $jsonAttrName => $modelAttrName) {
127
                $attributes[$jsonAttrName] = $model->{$modelAttrName} ?? null;
128
            }
129
        }
130
131
        return $attributes;
132
    }
133
134
    /**
135
     * @inheritdoc
136
     *
137
     * @SuppressWarnings(PHPMD.ElseExpression)
138
     */
139 2
    public function getRelationships($model): iterable
140
    {
141 2
        assert($model instanceof ModelInterface);
142
143 2
        $relationships = [];
144
145 2
        $mappings = static::getMappings();
146 2
        if (array_key_exists(static::SCHEMA_RELATIONSHIPS, $mappings) === true) {
147 2
            foreach ($mappings[static::SCHEMA_RELATIONSHIPS] as $jsonRelName => $modelRelName) {
148
                // if model has relationship data then use it
149 2
                if ($this->hasProperty($model, $modelRelName) === true) {
150 1
                    $relationships[$jsonRelName] = $this->createRelationshipRepresentationFromData(
151 1
                        $model,
152 1
                        $modelRelName,
153 1
                        $jsonRelName
154
                    );
155 1
                    continue;
156
                }
157
158
                // if relationship is `belongs-to` and has that ID we can add relationship as identifier
159 2
                $modelClass  = get_class($model);
160 2
                $relType = $this->getModelSchemas()->getRelationshipType($modelClass, $modelRelName);
161 2
                if ($relType === RelationshipTypes::BELONGS_TO) {
162 2
                    $fkName = $this->getModelSchemas()->getForeignKey($modelClass, $modelRelName);
163 2
                    if ($this->hasProperty($model, $fkName) === true) {
164 1
                        $reverseIndex  = $model->{$fkName};
165 1
                        if ($reverseIndex === null) {
166 1
                            $identifier = null;
167
                        } else {
168
                            $reverseSchema = $this->getJsonSchemas()
169
                                ->getRelationshipSchema(static::class, $jsonRelName);
170
                            $reverseType   = $reverseSchema->getType();
171
                            $identifier    = new Identifier($reverseIndex, $reverseType, false, null);
172
                        }
173 1
                        $relationships[$jsonRelName] = [
174 1
                            static::RELATIONSHIP_DATA       => $identifier,
175 1
                            static::RELATIONSHIP_LINKS_SELF => $this->isAddSelfLinkInRelationshipWithData($jsonRelName),
176
                        ];
177 1
                        continue;
178
                    }
179
                }
180
181
                // if we are here it's nothing left but show relationship as a link
182 2
                $relationships[$jsonRelName] = [static::RELATIONSHIP_LINKS_SELF => true];
183
            }
184
        }
185
186 2
        return $relationships;
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192 2
    public function isAddSelfLinkInRelationshipWithData(string $relationshipName): bool
193
    {
194 2
        return false;
195
    }
196
197
    /**
198
     * @return ModelSchemaInfoInterface
199
     */
200 2
    protected function getModelSchemas(): ModelSchemaInfoInterface
201
    {
202 2
        return $this->modelSchemas;
203
    }
204
205
    /**
206
     * @return JsonSchemasInterface
207
     */
208
    protected function getJsonSchemas(): JsonSchemasInterface
209
    {
210
        return $this->jsonSchemas;
211
    }
212
213
    /**
214
     * @param ModelInterface $model
215
     * @param string         $modelRelName
216
     * @param string         $jsonRelName
217
     *
218
     * @return array
219
     *
220
     * @SuppressWarnings(PHPMD.ElseExpression)
221
     */
222 1
    protected function createRelationshipRepresentationFromData(
223
        ModelInterface $model,
224
        string $modelRelName,
225
        string $jsonRelName
226
    ): array {
227 1
        assert($this->hasProperty($model, $modelRelName) === true);
228 1
        $relationshipData = $model->{$modelRelName};
229 1
        $isPaginatedData  = $relationshipData instanceof PaginatedDataInterface;
230
231 1
        $description = [static::RELATIONSHIP_LINKS_SELF => $this->isAddSelfLinkInRelationshipWithData($jsonRelName)];
232
233 1
        if ($isPaginatedData === false) {
234
            $description[static::RELATIONSHIP_DATA] = $relationshipData;
235
236
            return $description;
237
        }
238
239 1
        assert($relationshipData instanceof PaginatedDataInterface);
240
241 1
        $description[static::RELATIONSHIP_DATA] = $relationshipData->getData();
242
243 1
        if ($relationshipData->hasMoreItems() === false) {
244
            return $description;
245
        }
246
247
        // if we are here then relationship contains paginated data, so we have to add pagination links
248 1
        $offset    = $relationshipData->getOffset();
249 1
        $limit     = $relationshipData->getLimit();
250 1
        $urlPrefix = $this->getRelationshipSelfSubUrl($model, $jsonRelName) . '?';
251
        $buildLink = function (int $offset, int $limit) use ($urlPrefix) : LinkInterface {
252
            $paramsWithPaging = [
253 1
                JsonApiQueryParserInterface::PARAM_PAGING_OFFSET => $offset,
254 1
                JsonApiQueryParserInterface::PARAM_PAGING_LIMIT  => $limit,
255
            ];
256
257 1
            $subUrl = $urlPrefix . http_build_query($paramsWithPaging);
258
259 1
            return $this->getFactory()->createLink(true, $subUrl, false);
260 1
        };
261
262 1
        $nextOffset = $offset + $limit;
263 1
        $nextLimit  = $limit;
264 1
        if ($offset <= 0) {
265
            $description[static::RELATIONSHIP_LINKS] = [
266
                DocumentInterface::KEYWORD_NEXT => $buildLink($nextOffset, $nextLimit),
267
            ];
268
        } else {
269 1
            $prevOffset = $offset - $limit;
270 1
            if ($prevOffset < 0) {
271
                // set offset 0 and decrease limit
272 1
                $prevLimit  = $limit + $prevOffset;
273 1
                $prevOffset = 0;
274
            } else {
275 1
                $prevLimit = $limit;
276
            }
277 1
            $description[static::RELATIONSHIP_LINKS] = [
278 1
                DocumentInterface::KEYWORD_PREV => $buildLink($prevOffset, $prevLimit),
279 1
                DocumentInterface::KEYWORD_NEXT => $buildLink($nextOffset, $nextLimit),
280
            ];
281
        }
282
283 1
        return $description;
284
    }
285
286
    /**
287
     * @param ModelInterface $model
288
     * @param string         $name
289
     *
290
     * @return bool
291
     */
292 2
    private function hasProperty(ModelInterface $model, string $name): bool
293
    {
294 2
        $hasRelationship = property_exists($model, $name);
295
296 2
        return $hasRelationship;
297
    }
298
299
    /**
300
     * @param ModelInterface $model
301
     * @param string         $jsonRelName
302
     *
303
     * @return string
304
     */
305 1
    private function getRelationshipSelfSubUrl(ModelInterface $model, string $jsonRelName): string
306
    {
307 1
        return $this->getSelfSubUrl($model) . '/relationships/' . $jsonRelName;
308
    }
309
}
310