Completed
Push — master ( 5e61a0...2e1bfd )
by Michael
04:48 queued 02:16
created

IncludesRelations   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setAllowedWith() 0 5 1
A setDefaultWith() 0 5 1
A applyWith() 0 17 3
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 3
    public function setAllowedWith(array $allowed)
14
    {
15 3
        $this->allowed_with = $allowed;
16
17 3
        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 32
    private function applyWith($query)
28
    {
29 32
        $requested_with = $this->resource_context->with();
30
31 32
        if ($this->allowed_with === ['*']) {
32 1
            $with = $requested_with;
33
        } else {
34 31
            $with = array_intersect($this->allowed_with, $requested_with);
35
        }
36
37 32
        $with = array_unique(array_merge($with, $this->default_with));
38
39 32
        if (!empty($with)) {
40 3
            $query->with($with);
41
        }
42
43 32
        return $this;
44
    }
45
}
46