IncludesRelations::loadRelations()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 04.11.18
6
 * Time: 19:59.
7
 */
8
9
namespace Foundation\Abstracts\Transformers;
10
11
use Illuminate\Database\Eloquent\Collection;
12
use Illuminate\Database\Eloquent\Model;
13
14
/**
15
 * Trait IncludesRelations.
16
 */
17
trait IncludesRelations
18
{
19
    /**
20
     * @return array
21
     */
22
    protected function parseRequestIncludeParameter()
23
    {
24
        $request = request();
25
        if (isset($request->include) && is_string($request->include)) {
26
            return explode(',', $request->include);
27
        }
28
29
        return [];
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    public function compileRelations()
36
    {
37
        $requestedRelations = $this->parseRequestIncludeParameter();
38
        $relations = [];
39
        foreach ($requestedRelations as $requestedRelation) {
40
            if (isset($this->available[$requestedRelation])) {
41
                $relations[$requestedRelation] = $this->available[$requestedRelation];
42
            }
43
        }
44
        $merge = array_merge($this->include, $relations);
45
46
        return array_unique($merge);
47
    }
48
49
    /**
50
     * @param $relation
51
     *
52
     * @return $this
53
     */
54
    public function include($relation)
55
    {
56
        if (is_array($relation)) {
57
            $this->include = array_merge($this->include, $relation);
0 ignored issues
show
Bug Best Practice introduced by
The property include does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
        } else {
59
            $this->include[] = $relation;
60
        }
61
62
        return $this;
63
    }
64
65
    protected function includeRelations()
66
    {
67
        $relations = [];
68
        if ($this->resource instanceof Model) {
69
            $relations = $this->compileRelations();
70
            foreach ($relations as $relation => $transformer) {
71
                $relationMethodName = 'transform'.ucfirst(strtolower($relation));
72
                if (method_exists($this, $relationMethodName)) {
73
                    $relations[$relation] = $this->$relationMethodName($this->resource->$relation);
74
                } else {
75
                    if ($this->resource->$relation instanceof Model) {
76
                        $relations[$relation] = $transformer::resource($this->whenLoaded($relation));
0 ignored issues
show
Bug introduced by
It seems like whenLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
                        $relations[$relation] = $transformer::resource($this->/** @scrutinizer ignore-call */ whenLoaded($relation));
Loading history...
77
                    } elseif ($this->resource->$relation instanceof Collection) {
78
                        $relations[$relation] = $transformer::collection($this->whenLoaded($relation));
79
                    }
80
                    if ($this->resource->$relation == null) {
81
                        $relations[$relation] = null;
82
                    }
83
                }
84
            }
85
        }
86
87
        return $relations;
88
    }
89
90
    private static function loadRelations($resource, $extraRelations)
91
    {
92
        if ($resource instanceof Model || $resource instanceof Collection) {
93
            $relations = (array) call_class_function(static::class, 'compileRelations');
94
            $relations = array_merge($relations, $extraRelations);
95
            $resource->loadMissing(array_keys($relations));
96
        }
97
98
        return $resource;
99
    }
100
}
101