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

SearchCriteria::apply()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 10
Ratio 50 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 1
nop 2
dl 10
loc 20
ccs 0
cts 13
cp 0
crap 20
rs 9.2
c 0
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 SearchCriteria implements Criteria
9
{
10
    protected $columns;
11
    protected $queryString;
12
13
    public function __construct(array $columns, $queryString)
14
    {
15
        $this->columns = $columns;
16
        $this->queryString = $queryString;
17
    }
18
19
    public function apply($model, Repository $repository)
20
    {
21
        return $model->where(function ($query) {
22
            $firstColumn = array_shift($this->columns);
23
24 View Code Duplication
            if (strpos($firstColumn, '.')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
                $query = $this->setRelationFieldCondition($query, $firstColumn, false);
26
            } else {
27
                $query = $query->where($firstColumn, 'LIKE', '%'.$this->queryString.'%');
28
            }
29
30
            foreach ($this->columns as $column) {
31 View Code Duplication
                if (strpos($column, '.')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
                    $query = $this->setRelationFieldCondition($query, $column);
33
                } else {
34
                    $query->orWhere($column, 'LIKE', '%'.$this->queryString.'%');
35
                }
36
            }
37
        });
38
    }
39
40
    private function setRelationFieldCondition($query, $column, $or = true)
41
    {
42
        $columnRelation = explode('.', $column);
43
        $firstRelation = array_shift($columnRelation);
44
45
        if ($or) {
46
            $query->orWhereHas($firstRelation, $this->getRelationClosure($columnRelation));
47
        } else {
48
            $query->whereHas($firstRelation, $this->getRelationClosure($columnRelation));
49
        }
50
51
        return $query;
52
    }
53
54
    private function getRelationClosure(array $relations)
55
    {
56
        return function ($query) use ($relations) {
57
            $relation = array_shift($relations);
58
59
            if (count($relations) > 0) {
60
                $query->whereHas($relation, $this->getRelationClosure($relations));
61
            } else {
62
                $query->where($relation, 'LIKE', '%'.$this->queryString.'%');
63
            }
64
        };
65
    }
66
}
67