Complex classes like DealerLoop 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DealerLoop, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | * @package Dealer\Loop |
||
| 37 | */ |
||
| 38 | class DealerLoop extends BaseI18nLoop implements PropelSearchLoopInterface |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param LoopResult $loopResult |
||
| 43 | * |
||
| 44 | * @return LoopResult |
||
| 45 | */ |
||
| 46 | public function parseResults(LoopResult $loopResult) |
||
| 47 | { |
||
| 48 | /** @var Dealer $dealer */ |
||
| 49 | foreach ($loopResult->getResultDataCollection() as $dealer) { |
||
| 50 | $loopResultRow = new LoopResultRow($dealer); |
||
| 51 | |||
| 52 | $loopResultRow |
||
| 53 | ->set('ID', $dealer->getId()) |
||
| 54 | ->set("ADDRESS1", $dealer->getAddress1()) |
||
| 55 | ->set("ADDRESS2", $dealer->getAddress2()) |
||
| 56 | ->set("ADDRESS3", $dealer->getAddress3()) |
||
| 57 | ->set("ZIPCODE", $dealer->getZipcode()) |
||
| 58 | ->set("CITY", $dealer->getCity()) |
||
| 59 | ->set("COUNTRY_ID", $dealer->getCountryId()) |
||
| 60 | ->set("LAT", $dealer->getLatitude()) |
||
| 61 | ->set("LON", $dealer->getLongitude()) |
||
| 62 | ->set("CREATE_DATE", $dealer->getCreatedAt()) |
||
| 63 | ->set("UPDATE_DATE", $dealer->getUpdatedAt()) |
||
| 64 | ->set("VISIBLE", $dealer->getVisible()) |
||
| 65 | ; |
||
| 66 | |||
| 67 | if ($dealer->hasVirtualColumn('i18n_TITLE')) { |
||
| 68 | $loopResultRow->set("TITLE", $dealer->getVirtualColumn('i18n_TITLE')); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($dealer->hasVirtualColumn('i18n_ACCESS')) { |
||
| 72 | $loopResultRow->set("ACCESS", $dealer->getVirtualColumn('i18n_ACCESS')); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($dealer->hasVirtualColumn('i18n_DESCRIPTION')) { |
||
| 76 | $loopResultRow->set("DESCRIPTION", $dealer->getVirtualColumn('i18n_DESCRIPTION')); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($this->getWithPrevNextInfo()) { |
||
| 80 | |||
| 81 | $previous = $this->getPrevious($dealer); |
||
| 82 | $next = $this->getNext($dealer); |
||
| 83 | $loopResultRow |
||
| 84 | ->set("HAS_PREVIOUS", $previous != null ? 1 : 0) |
||
| 85 | ->set("HAS_NEXT", $next != null ? 1 : 0) |
||
| 86 | ->set("PREVIOUS", $previous != null ? $previous->getId() : -1) |
||
| 87 | ->set("NEXT", $next != null ? $next->getId() : -1); |
||
| 88 | } |
||
| 89 | |||
| 90 | $loopResult->addRow($loopResultRow); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $loopResult; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @inheritdoc |
||
| 98 | */ |
||
| 99 | protected function getArgDefinitions() |
||
| 100 | { |
||
| 101 | return new ArgumentCollection( |
||
| 102 | Argument::createIntListTypeArgument('id'), |
||
| 103 | Argument::createIntListTypeArgument('country_id'), |
||
| 104 | Argument::createIntListTypeArgument('content_id'), |
||
| 105 | Argument::createIntListTypeArgument('folder_id'), |
||
| 106 | Argument::createIntListTypeArgument('brand_id'), |
||
| 107 | Argument::createIntListTypeArgument('product_id'), |
||
| 108 | Argument::createBooleanTypeArgument('visible'), |
||
| 109 | Argument::createAnyListTypeArgument('city'), |
||
| 110 | Argument::createBooleanTypeArgument('with_prev_next_info', false), |
||
| 111 | Argument::createEnumListTypeArgument('order', [ |
||
| 112 | 'id', |
||
| 113 | 'id-reverse', |
||
| 114 | 'date', |
||
| 115 | 'date-reverse', |
||
| 116 | - 'title', |
||
| 117 | - 'title-reverse' |
||
| 118 | ], 'id') |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | public function buildModelCriteria() |
||
| 126 | { |
||
| 127 | $query = DealerQuery::create(); |
||
| 128 | |||
| 129 | // manage translations |
||
| 130 | $this->configureI18nProcessing( |
||
| 131 | $query, |
||
| 132 | [ |
||
| 133 | 'TITLE', |
||
| 134 | 'DESCRIPTION', |
||
| 135 | 'ACCESS' |
||
| 136 | ], |
||
| 137 | null, |
||
| 138 | 'ID', |
||
| 139 | $this->getForceReturn() |
||
| 140 | ); |
||
| 141 | |||
| 142 | if (null != $id = $this->getId()) { |
||
| 143 | $query->filterById($id); |
||
| 144 | } |
||
| 145 | |||
| 146 | if (null != $country_id = $this->getCountryId()) { |
||
| 147 | $query->filterByCountryId($country_id); |
||
| 148 | } |
||
| 149 | |||
| 150 | if (null != $city = $this->getCity()) { |
||
| 151 | $query->filterByCity($city); |
||
| 152 | } |
||
| 153 | |||
| 154 | if (null != $visible = $this->getVisible()) { |
||
| 155 | $query->filterByVisible($visible); |
||
| 156 | } |
||
| 157 | |||
| 158 | if($content = $this->getContentId()){ |
||
| 159 | if(is_array($content)){ |
||
| 160 | $content = implode(",", $content); |
||
| 161 | } |
||
| 162 | $contentJoin = new Join(DealerTableMap::ID,DealerContentTableMap::DEALER_ID,Criteria::LEFT_JOIN); |
||
| 163 | $query |
||
| 164 | ->addJoinObject($contentJoin) |
||
| 165 | ->where(DealerContentTableMap::CONTENT_ID." ".Criteria::IN." (".$content.")"); |
||
| 166 | ; |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | if($folder = $this->getFolderId()){ |
||
| 171 | if(is_array($folder)){ |
||
| 172 | $folder = implode(",", $folder); |
||
| 173 | } |
||
| 174 | $contentJoin = new Join(DealerTableMap::ID,DealerFolderTableMap::DEALER_ID,Criteria::LEFT_JOIN); |
||
| 175 | $query |
||
| 176 | ->addJoinObject($contentJoin) |
||
| 177 | ->where(DealerFolderTableMap::FOLDER_ID." ".Criteria::IN." (".$folder.")"); |
||
| 178 | ; |
||
| 179 | |||
| 180 | } |
||
| 181 | |||
| 182 | if($brand = $this->getBrandId()){ |
||
| 183 | if(is_array($brand)){ |
||
| 184 | $brand = implode(",", $brand); |
||
| 185 | } |
||
| 186 | $contentJoin = new Join(DealerTableMap::ID,DealerBrandTableMap::DEALER_ID,Criteria::LEFT_JOIN); |
||
| 187 | $query |
||
| 188 | ->addJoinObject($contentJoin) |
||
| 189 | ->where(DealerBrandTableMap::BRAND_ID." ".Criteria::IN." (".$brand.")"); |
||
| 190 | ; |
||
| 191 | |||
| 192 | } |
||
| 193 | |||
| 194 | if($product = $this->getProductId()){ |
||
| 195 | if(is_array($product)){ |
||
| 196 | $product = implode(",", $product); |
||
| 197 | } |
||
| 198 | $contentJoin = new Join(DealerTableMap::ID,DealerProductTableMap::DEALER_ID,Criteria::LEFT_JOIN); |
||
| 199 | $query |
||
| 200 | ->addJoinObject($contentJoin) |
||
| 201 | ->where(DealerProductTableMap::PRODUCT_ID." ".Criteria::IN." (".$product.")"); |
||
| 202 | ; |
||
| 203 | |||
| 204 | } |
||
| 205 | |||
| 206 | $query =$this->getAdminDealer($query); |
||
| 207 | |||
| 208 | foreach ($this->getOrder() as $order) { |
||
| 209 | switch ($order) { |
||
| 210 | case 'id' : |
||
|
|
|||
| 211 | $query->orderById(); |
||
| 212 | break; |
||
| 213 | case 'id-reverse' : |
||
| 214 | $query->orderById(Criteria::DESC); |
||
| 215 | break; |
||
| 216 | case 'date' : |
||
| 217 | $query->orderByCreatedAt(); |
||
| 218 | break; |
||
| 219 | case 'date-reverse' : |
||
| 220 | $query->orderByCreatedAt(Criteria::DESC); |
||
| 221 | break; |
||
| 222 | case 'title' : |
||
| 223 | - $query->useDealerI18nQuery()->orderByTitle()->endUse(); |
||
| 224 | - break; |
||
| 225 | - case 'title-reverse' : |
||
| 226 | - $query->useDealerI18nQuery()->orderByTitle(Criteria::DESC)->endUse(); |
||
| 227 | - break; |
||
| 228 | default: |
||
| 229 | break; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | return $query; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param Dealer $dealer |
||
| 238 | * @return Dealer |
||
| 239 | */ |
||
| 240 | protected function getPrevious($dealer) |
||
| 241 | { |
||
| 242 | $query = DealerQuery::create(); |
||
| 243 | |||
| 244 | foreach ($this->getOrder() as $order) { |
||
| 245 | switch ($order) { |
||
| 246 | case 'id' : |
||
| 247 | $query->orderById(Criteria::DESC); |
||
| 248 | $query->filterById($dealer->getId(),Criteria::LESS_THAN); |
||
| 249 | break; |
||
| 250 | case 'id-reverse' : |
||
| 251 | $query->orderById(); |
||
| 252 | $query->filterById($dealer->getId(),Criteria::GREATER_THAN); |
||
| 253 | break; |
||
| 254 | default: |
||
| 255 | $query->orderById(Criteria::DESC); |
||
| 256 | $query->filterById($dealer->getId(),Criteria::LESS_THAN); |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | return $query->findOne(); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param Dealer $dealer |
||
| 266 | * @return Dealer |
||
| 267 | */ |
||
| 268 | protected function getNext($dealer) |
||
| 269 | { |
||
| 270 | $query = DealerQuery::create(); |
||
| 271 | |||
| 272 | foreach ($this->getOrder() as $order) { |
||
| 273 | switch ($order) { |
||
| 274 | case 'id' : |
||
| 275 | $query->orderById(); |
||
| 276 | $query->filterById($dealer->getId(),Criteria::GREATER_THAN); |
||
| 277 | break; |
||
| 278 | case 'id-reverse' : |
||
| 279 | $query->orderById(Criteria::DESC); |
||
| 280 | $query->filterById($dealer->getId(),Criteria::LESS_THAN); |
||
| 281 | break; |
||
| 282 | default: |
||
| 283 | $query->orderById(); |
||
| 284 | $query->filterById($dealer->getId(),Criteria::GREATER_THAN); |
||
| 285 | break; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | return $query->findOne(); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param DealerQuery $query |
||
| 294 | * @return DealerQuery |
||
| 295 | */ |
||
| 296 | protected function getAdminDealer($query) |
||
| 297 | { |
||
| 298 | $admin = $this->securityContext->getAdminUser(); |
||
| 299 | $auth = $this->securityContext->isGranted(array("ADMIN"), array(AdminResources::MODULE), array('Dealer'), array(AccessManager::VIEW)); |
||
| 300 | |||
| 301 | if ($admin === null) { |
||
| 302 | return $query; |
||
| 303 | } |
||
| 304 | |||
| 312 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.