| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace lroman242\LaravelCassandra\Eloquent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Cassandra\Rows; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use lroman242\LaravelCassandra\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Builder extends EloquentBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * Create a collection of models from plain arrays. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param  \Cassandra\Rows  $rows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 13 |  |     public function hydrateRows(Rows $rows) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 13 |  |         $instance = $this->newModelInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 13 |  |         return $instance->newCassandraCollection($rows); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Execute the query as a "select" statement. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param  array  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function getPage($columns = ['*']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $builder = $this->applyScopes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return $builder->getModelsPage($columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Get the hydrated models without eager loading. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param  array  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 50 |  | View Code Duplication |     public function getModelsPage($columns = ['*']) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $results = $this->query->getPage($columns); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         if ($results instanceof Collection) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $results = $results->getRows(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } elseif (!$results instanceof Rows) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             throw new \Exception('Invalid type of getPage response. Expected lroman242\LaravelCassandra\Collection or Cassandra\Rows'); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return $this->model->hydrateRows($results); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Execute the query as a "select" statement. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param array $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return \Illuminate\Database\Eloquent\Collection|static[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 13 |  |     public function get($columns = ['*']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 13 |  |         $builder = $this->applyScopes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 13 |  |         return $builder->getModels($columns); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Get the hydrated models without eager loading. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param  array  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return \Illuminate\Database\Eloquent\Collection|static[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 88 | 13 | View Code Duplication |     public function getModels($columns = ['*']) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 13 |  |         $results = $this->query->get($columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 13 |  |         if ($results instanceof Collection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 13 |  |             $results = $results->getRows(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } elseif (!$results instanceof Rows) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             throw new \Exception('Invalid type of getPage response. Expected lroman242\LaravelCassandra\Collection or Cassandra\Rows'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 13 |  |         return $this->model->hydrateRows($results); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Add the "updated at" column to an array of values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param  array  $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |     protected function addUpdatedAtColumn(array $values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |         if (!$this->model->usesTimestamps() || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |             is_null($this->model->getUpdatedAtColumn())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             return $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |         $column = $this->model->getUpdatedAtColumn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |         $values = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |             [$column => $this->model->freshTimestampString()], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |             $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |         $values[$this->qualifyColumn($column)] = $values[$column]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |         if ($column != $this->qualifyColumn($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             unset($values[$column]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 2 |  |         return $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                        
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.