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

SearchCriteria   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 19.61 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 10
loc 51
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B apply() 10 22 4
A setRelationFieldCondition() 0 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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