| 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, '.')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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, '.')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.