Passed
Push — master ( 471523...a46036 )
by Arthur
13:15
created

IncludesRelations   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 69
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseRequestIncludeParameter() 0 8 3
A includeRelations() 0 21 6
A include() 0 9 2
A compileRelations() 0 11 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
    /**
21
     * @return array
22
     */
23 20
    protected function parseRequestIncludeParameter()
24
    {
25 20
        $request = request();
26 20
        if (isset($request->include) && is_string($request->include)) {
27 6
            return explode(',', $request->include);
28
        }
29
30 17
        return [];
31
    }
32
33
    /**
34
     * @return array
35
     */
36 20
    public function compileRelations()
37
    {
38 20
        $requestedRelations = $this->parseRequestIncludeParameter();
39 20
        $relations = [];
40 20
        foreach ($requestedRelations as $requestedRelation) {
41 6
            if (isset($this->available[$requestedRelation])) {
42 6
                $relations[$requestedRelation] = $this->available[$requestedRelation];
43
            }
44
        }
45 20
        $merge = array_merge($this->include, $relations);
46 20
        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 18
    protected function includeRelations()
66
    {
67 18
        $relations = [];
68 18
        if ($this->resource instanceof Model) {
69 18
            $relations = $this->compileRelations();
70 18
            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
                    } else if ($this->resource->$relation instanceof Collection) {
78 8
                        $relations[$relation] = $transformer::collection($this->whenLoaded($relation));
79
                    }
80
                }
81
82
            }
83
84
        }
85 18
        return $relations;
86
    }
87
}
88