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

WithCriteria   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
ccs 0
cts 11
cp 0
rs 10
c 1
b 0
f 0
wmc 4
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 4 1
A setWith() 0 10 2
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