| Total Complexity | 42 |
| Total Lines | 500 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Standard |
||
| 23 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
||
| 24 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
||
| 25 | { |
||
| 26 | /** admin/jqadm/customer/product/name |
||
| 27 | * Name of the product subpart used by the JQAdm customer implementation |
||
| 28 | * |
||
| 29 | * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Customer\Address\Myname". |
||
| 30 | * The name is case-sensitive and you should avoid camel case names like "MyName". |
||
| 31 | * |
||
| 32 | * @param string Last part of the JQAdm class name |
||
| 33 | * @since 2017.06 |
||
| 34 | * @category Developer |
||
| 35 | */ |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Copies a resource |
||
| 40 | * |
||
| 41 | * @return string HTML output |
||
| 42 | */ |
||
| 43 | public function copy() |
||
| 44 | { |
||
| 45 | $view = $this->getView(); |
||
| 46 | |||
| 47 | try |
||
| 48 | { |
||
| 49 | $view->productListTypes = $this->getListTypes(); |
||
| 50 | $view->productBody = ''; |
||
| 51 | |||
| 52 | foreach( $this->getSubClients() as $client ) { |
||
| 53 | $view->productBody .= $client->copy(); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | catch( \Aimeos\MShop\Exception $e ) |
||
| 57 | { |
||
| 58 | $error = array( 'customer-product' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 59 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 60 | $this->logException( $e ); |
||
| 61 | } |
||
| 62 | catch( \Exception $e ) |
||
| 63 | { |
||
| 64 | $error = array( 'customer-product' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 65 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 66 | $this->logException( $e ); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $this->render( $view ); |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Creates a new resource |
||
| 75 | * |
||
| 76 | * @return string HTML output |
||
| 77 | */ |
||
| 78 | public function create() |
||
| 79 | { |
||
| 80 | $view = $this->getView(); |
||
| 81 | |||
| 82 | try |
||
| 83 | { |
||
| 84 | $view->productListTypes = $this->getListTypes(); |
||
| 85 | $view->productBody = ''; |
||
| 86 | |||
| 87 | foreach( $this->getSubClients() as $client ) { |
||
| 88 | $view->productBody .= $client->create(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | catch( \Aimeos\MShop\Exception $e ) |
||
| 92 | { |
||
| 93 | $error = array( 'customer-product' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 94 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 95 | $this->logException( $e ); |
||
| 96 | } |
||
| 97 | catch( \Exception $e ) |
||
| 98 | { |
||
| 99 | $error = array( 'customer-product' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 100 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 101 | $this->logException( $e ); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $this->render( $view ); |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns a single resource |
||
| 110 | * |
||
| 111 | * @return string HTML output |
||
| 112 | */ |
||
| 113 | public function get() |
||
| 114 | { |
||
| 115 | $view = $this->getView(); |
||
| 116 | |||
| 117 | try |
||
| 118 | { |
||
| 119 | $total = 0; |
||
| 120 | $params = $this->storeSearchParams( $view->param( 'up', [] ), 'customerproduct' ); |
||
| 121 | $listItems = $this->getListItems( $view->item, $params, $total ); |
||
| 122 | |||
| 123 | $view->productItems = $this->getProductItems( $listItems ); |
||
| 124 | $view->productData = $this->toArray( $listItems ); |
||
| 125 | $view->productListTypes = $this->getListTypes(); |
||
| 126 | $view->productTotal = $total; |
||
| 127 | $view->productBody = ''; |
||
| 128 | |||
| 129 | foreach( $this->getSubClients() as $client ) { |
||
| 130 | $view->productBody .= $client->search(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | catch( \Aimeos\MShop\Exception $e ) |
||
| 134 | { |
||
| 135 | $error = array( 'customer-product' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 136 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 137 | $this->logException( $e ); |
||
| 138 | } |
||
| 139 | catch( \Exception $e ) |
||
| 140 | { |
||
| 141 | $error = array( 'customer-product' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 142 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 143 | $this->logException( $e ); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->render( $view ); |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Saves the data |
||
| 152 | */ |
||
| 153 | public function save() |
||
| 154 | { |
||
| 155 | $view = $this->getView(); |
||
| 156 | $context = $this->getContext(); |
||
| 157 | |||
| 158 | $manager = \Aimeos\MShop::create( $context, 'customer/lists' ); |
||
| 159 | |||
| 160 | $manager->begin(); |
||
| 161 | |||
| 162 | try |
||
| 163 | { |
||
| 164 | $this->storeSearchParams( $view->param( 'up', [] ), 'customerproduct' ); |
||
| 165 | $this->fromArray( $view->item, $view->param( 'product', [] ) ); |
||
| 166 | $view->productBody = ''; |
||
| 167 | |||
| 168 | foreach( $this->getSubClients() as $client ) { |
||
| 169 | $view->productBody .= $client->save(); |
||
| 170 | } |
||
| 171 | |||
| 172 | $manager->commit(); |
||
| 173 | return; |
||
| 174 | } |
||
| 175 | catch( \Aimeos\MShop\Exception $e ) |
||
| 176 | { |
||
| 177 | $error = array( 'customer-item-media' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 178 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 179 | $this->logException( $e ); |
||
| 180 | } |
||
| 181 | catch( \Exception $e ) |
||
| 182 | { |
||
| 183 | $error = array( 'customer-item-media' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
| 184 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
| 185 | $this->logException( $e ); |
||
| 186 | } |
||
| 187 | |||
| 188 | $manager->rollback(); |
||
| 189 | |||
| 190 | throw new \Aimeos\Admin\JQAdm\Exception(); |
||
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the sub-client given by its name. |
||
| 196 | * |
||
| 197 | * @param string $type Name of the client type |
||
| 198 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 199 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
| 200 | */ |
||
| 201 | public function getSubClient( $type, $name = null ) |
||
| 202 | { |
||
| 203 | /** admin/jqadm/customer/product/decorators/excludes |
||
| 204 | * Excludes decorators added by the "common" option from the customer JQAdm client |
||
| 205 | * |
||
| 206 | * Decorators extend the functionality of a class by adding new aspects |
||
| 207 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 208 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 209 | * modify what is returned to the caller. |
||
| 210 | * |
||
| 211 | * This option allows you to remove a decorator added via |
||
| 212 | * "admin/jqadm/common/decorators/default" before they are wrapped |
||
| 213 | * around the JQAdm client. |
||
| 214 | * |
||
| 215 | * admin/jqadm/customer/product/decorators/excludes = array( 'decorator1' ) |
||
| 216 | * |
||
| 217 | * This would remove the decorator named "decorator1" from the list of |
||
| 218 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
| 219 | * "admin/jqadm/common/decorators/default" to the JQAdm client. |
||
| 220 | * |
||
| 221 | * @param array List of decorator names |
||
| 222 | * @since 2017.07 |
||
| 223 | * @category Developer |
||
| 224 | * @see admin/jqadm/common/decorators/default |
||
| 225 | * @see admin/jqadm/customer/product/decorators/global |
||
| 226 | * @see admin/jqadm/customer/product/decorators/local |
||
| 227 | */ |
||
| 228 | |||
| 229 | /** admin/jqadm/customer/product/decorators/global |
||
| 230 | * Adds a list of globally available decorators only to the customer JQAdm client |
||
| 231 | * |
||
| 232 | * Decorators extend the functionality of a class by adding new aspects |
||
| 233 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 234 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 235 | * modify what is returned to the caller. |
||
| 236 | * |
||
| 237 | * This option allows you to wrap global decorators |
||
| 238 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
| 239 | * |
||
| 240 | * admin/jqadm/customer/product/decorators/global = array( 'decorator1' ) |
||
| 241 | * |
||
| 242 | * This would add the decorator named "decorator1" defined by |
||
| 243 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
| 244 | * |
||
| 245 | * @param array List of decorator names |
||
| 246 | * @since 2017.07 |
||
| 247 | * @category Developer |
||
| 248 | * @see admin/jqadm/common/decorators/default |
||
| 249 | * @see admin/jqadm/customer/product/decorators/excludes |
||
| 250 | * @see admin/jqadm/customer/product/decorators/local |
||
| 251 | */ |
||
| 252 | |||
| 253 | /** admin/jqadm/customer/product/decorators/local |
||
| 254 | * Adds a list of local decorators only to the customer JQAdm client |
||
| 255 | * |
||
| 256 | * Decorators extend the functionality of a class by adding new aspects |
||
| 257 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 258 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 259 | * modify what is returned to the caller. |
||
| 260 | * |
||
| 261 | * This option allows you to wrap local decorators |
||
| 262 | * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client. |
||
| 263 | * |
||
| 264 | * admin/jqadm/customer/product/decorators/local = array( 'decorator2' ) |
||
| 265 | * |
||
| 266 | * This would add the decorator named "decorator2" defined by |
||
| 267 | * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client. |
||
| 268 | * |
||
| 269 | * @param array List of decorator names |
||
| 270 | * @since 2017.07 |
||
| 271 | * @category Developer |
||
| 272 | * @see admin/jqadm/common/decorators/default |
||
| 273 | * @see admin/jqadm/customer/product/decorators/excludes |
||
| 274 | * @see admin/jqadm/customer/product/decorators/global |
||
| 275 | */ |
||
| 276 | return $this->createSubClient( 'customer/product/' . $type, $name ); |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the customer list items referencing the products |
||
| 282 | * |
||
| 283 | * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object |
||
| 284 | * @param array $params Associative list of GET/POST parameters |
||
| 285 | * @param integer $total Value/result parameter that will contain the item total afterwards |
||
| 286 | * @return \Aimeos\MShop\Common\Item\List\Iface[] Customer list items referencing the products |
||
| 287 | */ |
||
| 288 | protected function getListItems( \Aimeos\MShop\Customer\Item\Iface $item, array $params = [], &$total ) |
||
| 289 | { |
||
| 290 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer/lists' ); |
||
| 291 | |||
| 292 | $search = $manager->createSearch(); |
||
| 293 | $search->setSortations( [$search->sort( '-', 'customer.lists.ctime' )] ); |
||
| 294 | |||
| 295 | $search = $this->initCriteria( $search, $params ); |
||
| 296 | $expr = [ |
||
| 297 | $search->getConditions(), |
||
| 298 | $search->compare( '==', 'customer.lists.parentid', $item->getId() ), |
||
| 299 | $search->compare( '==', 'customer.lists.domain', 'product' ), |
||
| 300 | ]; |
||
| 301 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
| 302 | |||
| 303 | return $manager->searchItems( $search, [], $total ); |
||
| 304 | } |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns the available product list types |
||
| 309 | * |
||
| 310 | * @return \Aimeos\MShop\Common\Item\Type\Iface[] Associative list of type IDs as keys and type codes as values |
||
| 311 | */ |
||
| 312 | protected function getListTypes() |
||
| 313 | { |
||
| 314 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer/lists/type' ); |
||
| 315 | |||
| 316 | $search = $manager->createSearch( true )->setSlice( 0, 10000 ); |
||
| 317 | $search->setConditions( $search->compare( '==', 'customer.lists.type.domain', 'product' ) ); |
||
| 318 | $search->setSortations( [$search->sort( '+', 'customer.lists.type.position' )] ); |
||
| 319 | |||
| 320 | return $this->map( $manager->searchItems( $search ) ); |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns the product items referenced by the given list items |
||
| 326 | * |
||
| 327 | * @param \Aimeos\MShop\Common\Item\List\Iface[] $listItems Customer list items referencing the products |
||
| 328 | * @return \Aimeos\MShop\Product\Item\Iface[] Associative list of product IDs as keys and items as values |
||
| 329 | */ |
||
| 330 | protected function getProductItems( array $listItems ) |
||
| 331 | { |
||
| 332 | $list = []; |
||
| 333 | |||
| 334 | foreach( $listItems as $listItem ) { |
||
| 335 | $list[] = $listItem->getRefId(); |
||
| 336 | } |
||
| 337 | |||
| 338 | $manager = \Aimeos\MShop::create( $this->getContext(), 'product' ); |
||
| 339 | |||
| 340 | $search = $manager->createSearch()->setSlice( 0, count( $list ) ); |
||
| 341 | $search->setConditions( $search->compare( '==', 'product.id', $list ) ); |
||
| 342 | |||
| 343 | return $manager->searchItems( $search ); |
||
| 344 | } |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns the list of sub-client names configured for the client. |
||
| 349 | * |
||
| 350 | * @return array List of JQAdm client names |
||
| 351 | */ |
||
| 352 | protected function getSubClientNames() |
||
| 353 | { |
||
| 354 | /** admin/jqadm/customer/product/standard/subparts |
||
| 355 | * List of JQAdm sub-clients rendered within the customer product section |
||
| 356 | * |
||
| 357 | * The output of the frontend is composed of the code generated by the JQAdm |
||
| 358 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
| 359 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 360 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
| 361 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
| 362 | * the output that is placed inside the container of its parent. |
||
| 363 | * |
||
| 364 | * At first, always the JQAdm code generated by the parent is printed, then |
||
| 365 | * the JQAdm code of its sub-clients. The product of the JQAdm sub-clients |
||
| 366 | * determines the product of the output of these sub-clients inside the parent |
||
| 367 | * container. If the configured list of clients is |
||
| 368 | * |
||
| 369 | * array( "subclient1", "subclient2" ) |
||
| 370 | * |
||
| 371 | * you can easily change the product of the output by reproducting the subparts: |
||
| 372 | * |
||
| 373 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 374 | * |
||
| 375 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 376 | * |
||
| 377 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
| 378 | * |
||
| 379 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
| 380 | * should support adding, removing or reproducting content by a fluid like |
||
| 381 | * design. |
||
| 382 | * |
||
| 383 | * @param array List of sub-client names |
||
| 384 | * @since 2017.07 |
||
| 385 | * @category Developer |
||
| 386 | */ |
||
| 387 | return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/product/standard/subparts', [] ); |
||
| 388 | } |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * Creates new and updates existing items using the data array |
||
| 393 | * |
||
| 394 | * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object without referenced domain items |
||
| 395 | * @param string[] $data Data array |
||
| 396 | */ |
||
| 397 | protected function fromArray( \Aimeos\MShop\Customer\Item\Iface $item, array $data ) |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Constructs the data array for the view from the given item |
||
| 472 | * |
||
| 473 | * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems Customer list items referencing the products |
||
| 474 | * @return string[] Multi-dimensional associative list of item data |
||
| 475 | */ |
||
| 476 | protected function toArray( array $listItems ) |
||
| 477 | { |
||
| 478 | $data = []; |
||
| 479 | |||
| 480 | foreach( $listItems as $listItem ) |
||
| 481 | { |
||
| 482 | foreach( $listItem->toArray( true ) as $key => $value ) { |
||
| 483 | $data[$key][] = $value; |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | return $data; |
||
| 488 | } |
||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns the rendered template including the view data |
||
| 493 | * |
||
| 494 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
| 495 | * @return string HTML output |
||
| 496 | */ |
||
| 497 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
| 522 | } |
||
| 523 | } |
||
| 524 |