Completed
Push — master ( 187043...48cde4 )
by
unknown
01:30
created

AddsIncludesToQuery   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addIncludesToQuery() 0 8 1
A findInclude() 0 7 1
A allowedIncludes() 0 29 5
A ensureAllIncludesExist() 0 22 3
1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\QueryBuilder\AllowedInclude;
8
use Spatie\QueryBuilder\Exceptions\InvalidIncludeQuery;
9
use Spatie\QueryBuilder\Includes\IncludeInterface;
10
11
trait AddsIncludesToQuery
12
{
13
    /** @var \Illuminate\Support\Collection */
14
    protected $allowedIncludes;
15
16
    public function allowedIncludes($includes): self
17
    {
18
        $includes = is_array($includes) ? $includes : func_get_args();
19
20
        $this->allowedIncludes = collect($includes)
21
            ->reject(function ($include) {
22
                return empty($include);
23
            })
24
            ->flatMap(function ($include): Collection {
25
                if ($include instanceof IncludeInterface) {
26
                    return collect([$include]);
27
                }
28
29
                if (Str::endsWith($include, config('query-builder.count_suffix'))) {
30
                    return AllowedInclude::count($include);
31
                }
32
33
                return AllowedInclude::relationship($include);
34
            })
35
            ->unique(function (AllowedInclude $allowedInclude) {
36
                return $allowedInclude->getName();
37
            });
38
39
        if ($this->ensureAllIncludesExist()) {
40
            $this->addIncludesToQuery($this->request->includes());
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...
41
        }
42
43
        return $this;
44
    }
45
46
    protected function addIncludesToQuery(Collection $includes)
47
    {
48
        $includes->each(function ($include) {
49
            $include = $this->findInclude($include);
50
51
            $include->include($this);
52
        });
53
    }
54
55
    protected function findInclude(string $include): ?AllowedInclude
56
    {
57
        return $this->allowedIncludes
58
            ->first(function (AllowedInclude $included) use ($include) {
59
                return $included->isForInclude($include);
60
            });
61
    }
62
63
    protected function ensureAllIncludesExist(): bool
64
    {
65
        $includes = $this->request->includes();
66
67
        $allowedIncludeNames = $this->allowedIncludes->map(function (AllowedInclude $allowedInclude) {
68
            return $allowedInclude->getName();
69
        });
70
71
        $diff = $includes->diff($allowedIncludeNames);
72
73
        if ($diff->count()) {
74
            if ($this->throwInvalidQueryExceptions) {
0 ignored issues
show
Bug introduced by
The property throwInvalidQueryExceptions 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...
75
                throw InvalidIncludeQuery::includesNotAllowed($diff, $allowedIncludeNames);
76
            } else {
77
                return false;
78
            }
79
        }
80
81
        // TODO: Check for non-existing relationships?
82
83
        return true;
84
    }
85
}
86