Passed
Push — master ( 8d04f1...688970 )
by Lito
03:43
created

WithCriteria::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Anavel\Crud\Repository\Criteria;
4
5
use ANavallaSuiza\Laravel\Database\Contracts\Repository\Criteria;
6
use ANavallaSuiza\Laravel\Database\Contracts\Repository\Repository;
7
8
class WithCriteria implements Criteria
9
{
10
    protected $relation;
11
12
    public function __construct($relation)
13
    {
14
        $this->relation = explode('.', $relation);
15
    }
16
17
    public function apply($model, Repository $repository)
18
    {
19
        return $this->setWith($model);
20
    }
21
22
    private function setWith($model)
23
    {
24
        if (empty($this->relation)) {
25
            return $model;
26
        }
27
28
        return $model->with([array_shift($this->relation) => function ($q) {
29
            $this->setWith($q);
30
        }]);
31
    }
32
}
33