Completed
Push — master ( d88c3d...073799 )
by Adrian
06:02
created

SearchCriteria::apply()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 10
Ratio 45.45 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 1
nop 2
dl 10
loc 22
ccs 0
cts 14
cp 0
crap 20
rs 8.9197
c 0
b 0
f 0
1
<?php
2
namespace Anavel\Crud\Repository\Criteria;
3
4
use ANavallaSuiza\Laravel\Database\Contracts\Repository\Criteria;
5
use ANavallaSuiza\Laravel\Database\Contracts\Repository\Repository;
6
7
class SearchCriteria implements Criteria
8
{
9
    protected $columns;
10
    protected $queryString;
11
12
    public function __construct(array $columns, $queryString)
13
    {
14
        $this->columns = $columns;
15
        $this->queryString = $queryString;
16
    }
17
18
    public function apply($model, Repository $repository)
19
    {
20
        $query = $model->where(function ($query) use ($repository) {
21
            $firstColumn = array_shift($this->columns);
22
23 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...
24
                $query = $this->setRelationFieldCondition($query, $firstColumn, false);
25
            } else {
26
                $query = $query->where($firstColumn, 'LIKE', '%'.$this->queryString.'%');
27
            }
28
29
            foreach ($this->columns as $column) {
30 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...
31
                    $query = $this->setRelationFieldCondition($query, $column);
32
                } else {
33
                    $query->orWhere($column, 'LIKE', '%'.$this->queryString.'%');
34
                }
35
            }
36
        });
37
38
        return $query;
39
    }
40
41
    private function setRelationFieldCondition($query, $column, $or = true)
42
    {
43
        $columnRelation = explode('.', $column);
44
45
        if ($or) {
46
            $query->orWhereHas($columnRelation[0], function ($subquery) use ($columnRelation) {
47
                $subquery->where($columnRelation[1], 'LIKE', '%'.$this->queryString.'%');
48
            });
49
        } else {
50
            $query->whereHas($columnRelation[0], function ($subquery) use ($columnRelation) {
51
                $subquery->where($columnRelation[1], 'LIKE', '%'.$this->queryString.'%');
52
            });
53
        }
54
55
        return $query;
56
    }
57
}
58