IncludesRelations::applyWith()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
1
<?php
2
3
namespace Mblarsen\LaravelRepository\Traits;
4
5
trait IncludesRelations
6
{
7
    /** @var array $allowed_with */
8
    protected $allowed_with = [];
9
10
    /** @var array $default_with */
11
    protected $default_with = [];
12
13 4
    public function setAllowedWith(array $allowed)
14
    {
15 4
        $this->allowed_with = $allowed;
16
17 4
        return $this;
18
    }
19
20 1
    public function setDefaultWith(array $with)
21
    {
22 1
        $this->default_with = $with;
23
24 1
        return $this;
25
    }
26
27 42
    private function applyWith($query)
28
    {
29 42
        $requested_with = $this->resource_context->with();
30
31 42
        if ($this->allowed_with === ['*']) {
32 1
            $with = $requested_with;
33
        } else {
34 41
            $with = array_intersect($this->allowed_with, $requested_with);
35
        }
36
37 42
        $with = array_unique(array_merge($with, $this->default_with));
38
39 42
        if (!empty($with)) {
40 4
            $query->with($with);
41
        }
42
43 42
        return $this;
44
    }
45
}
46