Passed
Push — v2 ( e7bb8d...182303 )
by Alexander
02:36
created

HasRelationships::allRelationsAllowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Transformers\Concerns;
4
5
use Closure;
6
7
/**
8
 * A trait to be used by a transformer to handle relations
9
 *
10
 * @package flugger/laravel-responder
11
 * @author  Alexander Tømmerås <[email protected]>
12
 * @license The MIT License
13
 */
14
trait HasRelationships
15
{
16
    /**
17
     * List of available relations.
18
     *
19
     * @var string[]
20
     */
21
    protected $relations = ['*'];
22
23
    /**
24
     * A list of autoloaded default relations.
25
     *
26
     * @var array
27
     */
28
    protected $load = [];
29
30
    /**
31
     * Indicates if the transformer has whitelisted all relations.
32
     *
33
     * @return bool
34
     */
35 3
    public function allRelationsAllowed(): bool
36
    {
37 3
        return $this->relations == ['*'];
38
    }
39
40
    /**
41
     * Get a list of whitelisted relations.
42
     *
43
     * @return string[]
44
     */
45
    public function getRelations(): array
46
    {
47
        return array_filter($this->relations, function ($relation) {
48
            return $relation !== '*';
49
        });
50
    }
51
52
    /**
53
     * Get a list of default relations.
54
     *
55
     * @return string[]
56
     */
57
    public function getDefaultRelations(): array
58
    {
59
        return collect($this->load)->keys()->mapWithKeys(function ($relation) {
60
            if (method_exists($this, $method = 'load' . ucfirst($relation))) {
61
                return [$relation => $this->makeEagerLoadCallback($method)];
62
            }
63
64
            return $relation;
65
        });
66
    }
67
68
    /**
69
     * Extract a deep list of default relations, recursively.
70
     *
71
     * @return string[]
72
     */
73
    public function extractDefaultRelations(): array
74
    {
75
        return collect($this->getDefaultRelations())->merge($this->load->map(function ($transformer, $relation) {
76
            return collect($this->resolveTransformer($transformer)->extractDefaultRelations())
0 ignored issues
show
Bug introduced by
It seems like resolveTransformer() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
77
                ->keys()
78
                ->map(function ($nestedRelation) use ($relation) {
79
                    return "$relation.$nestedRelation";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $relation instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $nestedRelation instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
80
                });
81
        }))->all();
82
    }
83
84
    /**
85
     * Extract a deep list of default relations, recursively.
86
     *
87
     * @param  string $method
88
     * @return \Closure
89
     */
90
    public function makeEagerLoadCallback(string $method): Closure
91
    {
92
        return function ($query) use ($method) {
93
            return $this->$method($query);
94
        };
95
    }
96
}