Passed
Push — master ( d5fac0...e5b08c )
by Arthur
14:05
created

IncludesRelations::include()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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