| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Created by PhpStorm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * User: VITALYIEGOROV | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Date: 08.12.15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Time: 23:11 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace samsoncms\api\query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use samsoncms\api\Material; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use samsonframework\orm\ArgumentInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * TODO: We have create a Record class that lays behind Entity-Generic and we store entity fields in fieldIDs static variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * which cannot be accessed in parent function calls like orderBy, applySorting, to get intermediary static class variables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * from Generic class. This affect code duplication of method in Record to Generic to give access to static class fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * described in fieldIDs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Material with additional fields query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @package samsoncms\api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class Generic extends Record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var string Table class name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected static $identifier = Material::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var string Table primary field name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected static $primaryFieldName = Material::F_PRIMARY; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |        /** @var array Collection of all supported entity fields */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected static $fieldIDs = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         Material::F_PRIMARY=> Material::F_PRIMARY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         Material::F_PRIORITY => Material::F_PRIORITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         Material::F_IDENTIFIER => Material::F_IDENTIFIER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         Material::F_DELETION => Material::F_DELETION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         Material::F_PUBLISHED => Material::F_PUBLISHED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         Material::F_PARENT => Material::F_PARENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         Material::F_CREATED => Material::F_CREATED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         Material::F_MODIFIED => Material::F_MODIFIED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** @var array Collection of all supported entity fields */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected static $fieldNames = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         Material::F_PRIMARY => Material::F_PRIMARY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         Material::F_PRIORITY => Material::F_PRIORITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         Material::F_IDENTIFIER => Material::F_IDENTIFIER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         Material::F_DELETION => Material::F_DELETION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         Material::F_PUBLISHED => Material::F_PUBLISHED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         Material::F_PARENT => Material::F_PARENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         Material::F_CREATED => Material::F_CREATED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         Material::F_MODIFIED => Material::F_MODIFIED | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** @var string Entity navigation identifiers */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected static $navigationIDs = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Add primary field query condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $value Field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $relation @see ArgumentInterface types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return $this Chaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @see Material::where() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function primary($value, $relation = ArgumentInterface::EQUAL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $this->where(Material::F_PRIMARY, $value, $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Add identifier field query condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $value Field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $relation @see ArgumentInterface types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return $this Chaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @see Material::where() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function identifier($value, $relation = ArgumentInterface::EQUAL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this->where(Material::F_IDENTIFIER, $value, $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Add active flag condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param bool $value Field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $relation @see ArgumentInterface types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return $this Chaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @see Material::where() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function active($value, $relation = ArgumentInterface::EQUAL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $this->where(Material::F_DELETION, $value, $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Add entity published field query condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param string $value Field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param string $relation @see ArgumentInterface types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return $this Chaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @see Material::where() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     public function published($value, $relation = ArgumentInterface::EQUAL) | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         return $this->where(Material::F_PUBLISHED, $value, $relation); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Add entity creation field query condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param string $value Field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param string $relation @see ArgumentInterface types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return $this Chaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @see Material::where() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function created($value, $relation = ArgumentInterface::EQUAL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $this->where(Material::F_CREATED, $this->convertToDateTime($value), $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Add entity modification field query condition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param string $value Field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param string $relation @see ArgumentInterface types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return $this Chaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @see Material::where() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function modified($value, $relation = ArgumentInterface::EQUAL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         return $this->where(Material::F_MODIFIED, $this->convertToDateTime($value), $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * Set field for sorting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * TODO: We have code duplication in Record::orderBy() due to late static binding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param string $fieldName Additional field name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param string $order     Sorting order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return $this Chaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 151 |  | View Code Duplication |     public function orderBy($fieldName, $order = 'ASC') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         if (in_array($fieldName, self::$fieldIDs)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $this->orderBy = array($fieldName, $order); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * Add sorting to entity identifiers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * TODO: We have code duplication in Record::orderBy() due to late static binding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param array  $entityIDs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param string $fieldName Additional field name for sorting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param string $order     Sorting order(ASC|DESC) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return array Collection of entity identifiers ordered by additional field value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 169 |  | View Code Duplication |     protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         if (array_key_exists($fieldName, self::$fieldIDs)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             // Order by parent fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             return $this->query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 ->entity(static::$identifier) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 ->where(static::$primaryFieldName, $entityIDs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                 ->orderBy($fieldName, $order) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 ->fields(static::$primaryFieldName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } else { // Nothing is changed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             return $entityIDs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 182 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |  | 
            
                        
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.