Passed
Push — master ( 396f3f...e59562 )
by Anton
32s
created

Action::linkRelationships()   D

Complexity

Conditions 10
Paths 16

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 20
nc 16
nop 2

How to fix   Complexity   

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
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi\actions;
7
8
use tuyakhov\jsonapi\ResourceInterface;
9
use yii\db\ActiveRecordInterface;
10
use yii\db\BaseActiveRecord;
11
use yii\helpers\ArrayHelper;
12
13
class Action extends \yii\rest\Action
14
{
15
    /**
16
     * Links the relationships with primary model.
17
     * @var callable
18
     */
19
    public $linkRelationships;
20
21
    /**
22
     * @var bool Weather allow to do a full replacement of a to-many relationship
23
     */
24
    public $allowFullReplacement = true;
25
26
    /**
27
     * Links the relationships with primary model.
28
     * @param $model ActiveRecordInterface
29
     * @param array $data
30
     */
31
    protected function linkRelationships($model, array $data = [])
32
    {
33
        if ($this->linkRelationships !== null) {
34
            call_user_func($this->linkRelationships, $this, $model, $data);
35
            return;
36
        }
37
38
        if (!$model instanceof ResourceInterface) {
39
            return;
40
        }
41
42
        foreach ($data as $name => $relationship) {
43
            if (!$related = $model->getRelation($name, false)) {
0 ignored issues
show
Bug introduced by
The method getRelation() does not seem to exist on object<tuyakhov\jsonapi\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
                continue;
45
            }
46
            /** @var BaseActiveRecord $relatedClass */
47
            $relatedClass = new $related->modelClass;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
48
            $relationships = ArrayHelper::keyExists($relatedClass->formName(), $relationship) ? $relationship[$relatedClass->formName()] : [];
49
50
            $ids = [];
51
            foreach ($relationships as $index => $relObject) {
52
                if (!isset($relObject['id'])) {
53
                    continue;
54
                }
55
                $ids[] = $relObject['id'];
56
            }
57
58
            if ($related->multiple && !$this->allowFullReplacement) {
59
                continue;
60
            }
61
            $records = $relatedClass::find()->andWhere(['in', $relatedClass::primaryKey(), $ids])->all();
62
            $model->setResourceRelationship($name, $records);
63
        }
64
    }
65
}