Completed
Pull Request — master (#403)
by
unknown
01:15
created

AppendsAttributesToResults   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A allowedAppends() 0 10 2
A addAppendsToResults() 0 17 2
A appendLoop() 0 18 4
A ensureAllAppendsExist() 0 10 2
1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery;
8
9
trait AppendsAttributesToResults
10
{
11
    /** @var \Illuminate\Support\Collection */
12
    protected $allowedAppends;
13
14
    public function allowedAppends($appends): self
15
    {
16
        $appends = is_array($appends) ? $appends : func_get_args();
17
18
        $this->allowedAppends = collect($appends);
19
20
        $this->ensureAllAppendsExist();
21
22
        return $this;
23
    }
24
25
    protected function addAppendsToResults(Collection $results)
26
    {
27
        $appends = $this->request->appends();
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
29
        return $results->each(function ($item) use ($appends) {
30
            $appends->each(function ($append) use ($item) {
31
                if (Str::contains($append, '.')) {
32
                    $nestedAppends = collect(explode('.', $append));
33
                    $relation = $nestedAppends->shift();
34
35
                    $this->appendLoop($item, $relation, $nestedAppends);
36
                } else {
37
                    $item->append($append);
38
                }
39
            });
40
        });
41
    }
42
43
    private function appendLoop($item, string $relation, Collection $nestedAppends)
44
    {
45
        if ($item->relationLoaded($relation)) {
46
            if ($nestedAppends->count() === 1) {
47
                $sub = $nestedAppends->first();
48
                if ($item->$relation instanceof \Illuminate\Database\Eloquent\Collection) {
49
                    $item->$relation->each(function ($model) use ($sub) {
50
                        $model->append($sub);
51
                    });
52
                } else {
53
                    $item->$relation->append($sub);
54
                }
55
            } else {
56
                $sub = $nestedAppends->shift();
57
                $this->appendLoop($item->$relation, $sub, $nestedAppends);
58
            }
59
        }
60
    }
61
62
    protected function ensureAllAppendsExist()
63
    {
64
        $appends = $this->request->appends();
65
66
        $diff = $appends->diff($this->allowedAppends);
67
68
        if ($diff->count()) {
69
            throw InvalidAppendQuery::appendsNotAllowed($diff, $this->allowedAppends);
70
        }
71
    }
72
}
73