| Total Complexity | 49 |
| Total Lines | 292 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 1 |
Complex classes like EagerLoadedDataList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EagerLoadedDataList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class EagerLoadedDataList extends DataList{ |
||
| 14 | |||
| 15 | const ID_LIMIT = 5000; |
||
| 16 | public $withList = []; |
||
| 17 | public $_relatedMaps = [ |
||
| 18 | 'has_one' => [], |
||
| 19 | 'has_many' => [], |
||
| 20 | 'many_many' => [], |
||
| 21 | ]; |
||
| 22 | |||
| 23 | public function __construct($classOrList) |
||
| 30 | } |
||
| 31 | |||
| 32 | } |
||
| 33 | public $_relatedCache = []; |
||
| 34 | public static function cloneFrom(DataList $list) |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Create a DataObject from the given SQL row |
||
| 45 | * |
||
| 46 | * @param array $row |
||
| 47 | * @return DataObject |
||
| 48 | */ |
||
| 49 | public function createDataObject($row) |
||
| 56 | } |
||
| 57 | |||
| 58 | private $relationsPrepared = false; |
||
| 59 | |||
| 60 | public function prepareEagerRelations() { |
||
| 61 | if($this->relationsPrepared) return; |
||
| 62 | $this->relationsPrepared = true; |
||
| 63 | $localClass = $this->dataClass(); |
||
| 64 | $config = Config::forClass($localClass); |
||
| 65 | $hasOnes = (array)$config->get('has_one'); |
||
| 66 | $hasManys = (array)$config->get('has_many'); |
||
| 67 | $manyManys = (array)$config->get('many_many'); |
||
| 68 | |||
| 69 | //collect has_ones |
||
| 70 | $withHasOnes = array_filter($this->withList,function($dep)use($hasOnes){ return array_key_exists($dep[0],$hasOnes); }); |
||
| 71 | $withHasManys = array_filter($this->withList,function($dep)use($hasManys){ return array_key_exists($dep[0],$hasManys); }); |
||
| 72 | $withManyManys = array_filter($this->withList,function($dep)use($manyManys){ return array_key_exists($dep[0],$manyManys); }); |
||
| 73 | |||
| 74 | if(!count($withHasOnes) && !count($withHasManys) && !count($withManyManys)){ |
||
| 75 | // do nothing if no matches |
||
| 76 | /** @todo report errors */ |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | |||
| 80 | $data = $this->column('ID'); |
||
| 81 | if(count($withHasOnes)){ |
||
| 82 | $this->_prepareCache($hasOnes, $withHasOnes); |
||
| 83 | $this->eagerLoadHasOne($data, $hasOnes, $withHasOnes); |
||
| 84 | } |
||
| 85 | if(count($withHasManys)){ |
||
| 86 | $this->_prepareCache($hasManys, $withHasManys); |
||
| 87 | $this->eagerLoadHasMany($data, $hasManys, $withHasManys); |
||
| 88 | } |
||
| 89 | if(count($withManyManys)){ |
||
| 90 | $this->_prepareCache($manyManys, $withManyManys); |
||
| 91 | $this->eagerLoadManyMany($data, $manyManys, $withManyManys); |
||
| 92 | } |
||
| 93 | |||
| 94 | } |
||
| 95 | |||
| 96 | public function eagerLoadHasOne(&$ids, $hasOnes, $withHasOnes) |
||
| 97 | { |
||
| 98 | $schema = DataObject::getSchema(); |
||
| 99 | |||
| 100 | //collect required IDS |
||
| 101 | $fields = ['ID']; |
||
| 102 | foreach($withHasOnes as $depSeq) { |
||
| 103 | $dep = $depSeq[0]; |
||
| 104 | $fields[] = "{$dep}ID"; |
||
| 105 | } |
||
| 106 | $table = Config::forClass($this->dataClass)->get('table_name'); |
||
| 107 | $data = new SQLSelect(implode(',',$fields),[$table],["ID IN (".implode(',',$ids).")"]); |
||
| 108 | $data = Utils::EnsureArray($data->execute(),'ID'); |
||
| 109 | |||
| 110 | foreach($withHasOnes as $depSeq) { |
||
| 111 | $dep = $depSeq[0]; |
||
| 112 | $depClass = $hasOnes[$dep]; |
||
| 113 | |||
| 114 | $descriptor = [ |
||
| 115 | 'class' => $depClass, |
||
| 116 | 'localField' => "{$dep}ID", |
||
| 117 | 'map' => [], |
||
| 118 | ]; |
||
| 119 | |||
| 120 | $component = $schema->hasOneComponent($this->dataClass, $dep); |
||
|
|
|||
| 121 | |||
| 122 | $descriptor['map'] = Utils::extractField($data,$descriptor['localField']); |
||
| 123 | $uniqueIDs = array_unique($descriptor['map']); |
||
| 124 | while(count($uniqueIDs)) { |
||
| 125 | $IDsubset = array_splice($uniqueIDs,0,self::ID_LIMIT); |
||
| 126 | $result = DataObject::get($depClass)->filter('ID',$IDsubset); |
||
| 127 | if(count($depSeq)>1){ |
||
| 128 | $result = $result |
||
| 129 | ->with(implode('.',array_slice($depSeq,1))); |
||
| 130 | } |
||
| 131 | |||
| 132 | foreach($result as $depRecord) { |
||
| 133 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->_relatedMaps['has_one'][$dep] = $descriptor; |
||
| 138 | |||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | public function eagerLoadHasMany($data, $hasManys, $withHasManys) |
||
| 143 | { |
||
| 144 | $localClass = $this->dataClass(); |
||
| 145 | $localClassTail = basename(str_replace('\\','/',$localClass)); |
||
| 146 | |||
| 147 | foreach($withHasManys as $depSeq) { |
||
| 148 | $dep = $depSeq[0]; |
||
| 149 | $depClass = $hasManys[$dep]; |
||
| 150 | $localNameInDep = $localClassTail; |
||
| 151 | $depKey = "{$localNameInDep}ID"; |
||
| 152 | $descriptor = [ |
||
| 153 | 'class' => $depClass, |
||
| 154 | 'remoteRelation' => $localNameInDep, |
||
| 155 | 'remoteField' => $depKey, |
||
| 156 | 'map' => [], |
||
| 157 | ]; |
||
| 158 | $result = DataObject::get($depClass)->filter($depKey,$data); |
||
| 159 | if(count($depSeq)>1){ |
||
| 160 | $result = $result |
||
| 161 | ->with(implode('.',array_slice($depSeq,1))); |
||
| 162 | } |
||
| 163 | |||
| 164 | $collection = []; |
||
| 165 | |||
| 166 | foreach($data as $localRecordID){ |
||
| 167 | $collection[$localRecordID] = []; |
||
| 168 | } |
||
| 169 | foreach($result as $depRecord) { |
||
| 170 | |||
| 171 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
||
| 172 | $collection[$depRecord->$depKey][] = $depRecord->ID; |
||
| 173 | } |
||
| 174 | $descriptor['map'] = $collection; |
||
| 175 | $this->_relatedMaps['has_many'][$dep] = $descriptor; |
||
| 176 | |||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | public function eagerLoadManyMany(&$data, $manyManys, $withManyManys) |
||
| 226 | |||
| 227 | } |
||
| 228 | |||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | public function fulfillEagerRelations(DataObject $item) |
||
| 233 | { |
||
| 234 | foreach($this->_relatedMaps['has_one'] as $dep => $depInfo){ |
||
| 235 | $depClass = $depInfo['class']; |
||
| 236 | if(isset($depInfo['map'][$item->ID])) { |
||
| 237 | $depID = $depInfo['map'][$item->ID]; |
||
| 238 | if(isset($this->_relatedCache[$depClass][$depID])) |
||
| 239 | { |
||
| 240 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
||
| 241 | $item->setComponent($dep, $depRecord); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | foreach($this->_relatedMaps['has_many'] as $dep => $depInfo){ |
||
| 247 | $depClass = $depInfo['class']; |
||
| 248 | $collection = []; |
||
| 249 | if(isset($depInfo['map'][$item->ID])){ |
||
| 250 | foreach($depInfo['map'][$item->ID] as $depID){ |
||
| 251 | if(isset($this->_relatedCache[$depClass][$depID])) |
||
| 252 | { |
||
| 253 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
||
| 254 | $collection[] = $depRecord; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | if(!method_exists($item,'addEagerRelation')) { |
||
| 259 | throw new \Exception("Model {$item->ClassName} must include Gurucomkz\EagerLoading\EagerLoaderMultiAccessor trait to use eager loading for \$has_many"); |
||
| 260 | } |
||
| 261 | $item->addEagerRelation($dep, $collection); |
||
| 262 | } |
||
| 263 | |||
| 264 | foreach($this->_relatedMaps['many_many'] as $dep => $depInfo){ |
||
| 265 | $depClass = $depInfo['class']; |
||
| 266 | $collection = []; |
||
| 267 | if(isset($depInfo['map'][$item->ID])){ |
||
| 268 | foreach($depInfo['map'][$item->ID] as $depIDlist){ |
||
| 269 | foreach($depIDlist as $depID){ |
||
| 270 | if(isset($this->_relatedCache[$depClass][$depID])) |
||
| 271 | { |
||
| 272 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
||
| 273 | $collection[] = $depRecord; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | if(!method_exists($item,'addEagerRelation')) { |
||
| 279 | throw new \Exception("Model {$item->ClassName} must include Gurucomkz\EagerLoading\EagerLoaderMultiAccessor trait to use eager loading for \$many_many"); |
||
| 280 | } |
||
| 281 | $item->addEagerRelation($dep, $collection); |
||
| 282 | } |
||
| 283 | |||
| 284 | } |
||
| 285 | /** |
||
| 286 | * Returns a generator for this DataList |
||
| 287 | * |
||
| 288 | * @return \Generator&DataObject[] |
||
| 289 | */ |
||
| 290 | public function getGenerator() |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | private function _prepareCache($all,$selected) |
||
| 311 |