Passed
Push — master ( 802ddf...d5fac0 )
by Arthur
13:53
created

IncludesRelations   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 83.78%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 78
ccs 31
cts 37
cp 0.8378
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A include() 0 9 2
A parseRequestIncludeParameter() 0 8 3
A compileRelations() 0 12 3
A includeRelations() 0 20 6
A loadRelations() 0 8 3
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 21
    protected function parseRequestIncludeParameter()
23
    {
24 21
        $request = request();
25 21
        if (isset($request->include) && is_string($request->include)) {
26 6
            return explode(',', $request->include);
27
        }
28
29 15
        return [];
30
    }
31
32
    /**
33
     * @return array
34
     */
35 21
    public function compileRelations()
36
    {
37 21
        $requestedRelations = $this->parseRequestIncludeParameter();
38 21
        $relations = [];
39 21
        foreach ($requestedRelations as $requestedRelation) {
40 6
            if (isset($this->available[$requestedRelation])) {
41 6
                $relations[$requestedRelation] = $this->available[$requestedRelation];
42
            }
43
        }
44 21
        $merge = array_merge($this->include, $relations);
45
46 21
        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 10
    protected function includeRelations()
66
    {
67 10
        $relations = [];
68 10
        if ($this->resource instanceof Model) {
69 10
            $relations = $this->compileRelations();
70 10
            foreach ($relations as $relation => $transformer) {
71 3
                $relationMethodName = 'transform'.ucfirst(strtolower($relation));
72 3
                if (method_exists($this, $relationMethodName)) {
73 3
                    $relations[$relation] = $this->$relationMethodName($this->resource->$relation);
74
                } else {
75 1
                    if ($this->resource->$relation instanceof Model) {
76 1
                        $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 3
                        $relations[$relation] = $transformer::collection($this->whenLoaded($relation));
79
                    }
80
                }
81
            }
82
        }
83
84 10
        return $relations;
85
    }
86
87 21
    private static function loadRelations($resource, $extraRelations)
88
    {
89 21
        if ($resource instanceof Model || $resource instanceof Collection) {
90 21
            $relations = (array)call_class_function(static::class, 'compileRelations');
91 21
            $relations = array_merge($relations, $extraRelations);
92 21
            $resource->loadMissing(array_keys($relations));
93
        }
94 21
        return $resource;
95
    }
96
}
97