| Total Complexity | 47 |
| Total Lines | 431 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 21 | class Standard |
||
| 22 | implements \Aimeos\Controller\Common\Order\Iface |
||
| 23 | { |
||
| 24 | private $context; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Initializes the object. |
||
| 29 | * |
||
| 30 | * @param \Aimeos\MShop\Context\Item\Iface $context |
||
| 31 | */ |
||
| 32 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
| 35 | } |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Blocks the resources listed in the order. |
||
| 40 | * |
||
| 41 | * Every order contains resources like products or redeemed coupon codes |
||
| 42 | * that must be blocked so they can't be used by another customer in a |
||
| 43 | * later order. This method reduces the the stock level of products, the |
||
| 44 | * counts of coupon codes and others. |
||
| 45 | * |
||
| 46 | * It's save to call this method multiple times for one order. In this case, |
||
| 47 | * the actions will be executed only once. All subsequent calls will do |
||
| 48 | * nothing as long as the resources haven't been unblocked in the meantime. |
||
| 49 | * |
||
| 50 | * You can also block and unblock resources several times. Please keep in |
||
| 51 | * mind that unblocked resources may be reused by other orders in the |
||
| 52 | * meantime. This can lead to an oversell of products! |
||
| 53 | * |
||
| 54 | * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
||
| 55 | * @return \Aimeos\MShop\Order\Item\Iface Order item object |
||
| 56 | */ |
||
| 57 | public function block( \Aimeos\MShop\Order\Item\Iface $orderItem ) : \Aimeos\MShop\Order\Item\Iface |
||
| 58 | { |
||
| 59 | $this->updateStatus( $orderItem, \Aimeos\MShop\Order\Item\Status\Base::STOCK_UPDATE, 1, -1 ); |
||
| 60 | $this->updateStatus( $orderItem, \Aimeos\MShop\Order\Item\Status\Base::COUPON_UPDATE, 1, -1 ); |
||
| 61 | |||
| 62 | return $orderItem; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Frees the resources listed in the order. |
||
| 68 | * |
||
| 69 | * If customers created orders but didn't pay for them, the blocked resources |
||
| 70 | * like products and redeemed coupon codes must be unblocked so they can be |
||
| 71 | * ordered again or used by other customers. This method increased the stock |
||
| 72 | * level of products, the counts of coupon codes and others. |
||
| 73 | * |
||
| 74 | * It's save to call this method multiple times for one order. In this case, |
||
| 75 | * the actions will be executed only once. All subsequent calls will do |
||
| 76 | * nothing as long as the resources haven't been blocked in the meantime. |
||
| 77 | * |
||
| 78 | * You can also unblock and block resources several times. Please keep in |
||
| 79 | * mind that unblocked resources may be reused by other orders in the |
||
| 80 | * meantime. This can lead to an oversell of products! |
||
| 81 | * |
||
| 82 | * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
||
| 83 | * @return \Aimeos\MShop\Order\Item\Iface Order item object |
||
| 84 | */ |
||
| 85 | public function unblock( \Aimeos\MShop\Order\Item\Iface $orderItem ) : \Aimeos\MShop\Order\Item\Iface |
||
| 86 | { |
||
| 87 | $this->updateStatus( $orderItem, \Aimeos\MShop\Order\Item\Status\Base::STOCK_UPDATE, 0, +1 ); |
||
| 88 | $this->updateStatus( $orderItem, \Aimeos\MShop\Order\Item\Status\Base::COUPON_UPDATE, 0, +1 ); |
||
| 89 | |||
| 90 | return $orderItem; |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Blocks or frees the resources listed in the order if necessary. |
||
| 96 | * |
||
| 97 | * After payment status updates, the resources like products or coupon |
||
| 98 | * codes listed in the order must be blocked or unblocked. This method |
||
| 99 | * cares about executing the appropriate action depending on the payment |
||
| 100 | * status. |
||
| 101 | * |
||
| 102 | * It's save to call this method multiple times for one order. In this case, |
||
| 103 | * the actions will be executed only once. All subsequent calls will do |
||
| 104 | * nothing as long as the payment status hasn't changed in the meantime. |
||
| 105 | * |
||
| 106 | * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
||
| 107 | * @return \Aimeos\MShop\Order\Item\Iface Order item object |
||
| 108 | */ |
||
| 109 | public function update( \Aimeos\MShop\Order\Item\Iface $orderItem ) : \Aimeos\MShop\Order\Item\Iface |
||
| 110 | { |
||
| 111 | switch( $orderItem->getStatusPayment() ) |
||
| 112 | { |
||
| 113 | case \Aimeos\MShop\Order\Item\Base::PAY_DELETED: |
||
| 114 | case \Aimeos\MShop\Order\Item\Base::PAY_CANCELED: |
||
| 115 | case \Aimeos\MShop\Order\Item\Base::PAY_REFUSED: |
||
| 116 | case \Aimeos\MShop\Order\Item\Base::PAY_REFUND: |
||
| 117 | $this->unblock( $orderItem ); |
||
| 118 | break; |
||
| 119 | |||
| 120 | case \Aimeos\MShop\Order\Item\Base::PAY_PENDING: |
||
| 121 | case \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED: |
||
| 122 | case \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED: |
||
| 123 | $this->block( $orderItem ); |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | |||
| 127 | return $orderItem; |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Adds a new status record to the order with the type and value. |
||
| 133 | * |
||
| 134 | * @param string $parentid Order ID |
||
| 135 | * @param string $type Status type |
||
| 136 | * @param string $value Status value |
||
| 137 | * @return \Aimeos\Controller\Common\Order\Iface Order controller for fluent interface |
||
| 138 | */ |
||
| 139 | protected function addStatusItem( string $parentid, string $type, string $value ) : Iface |
||
| 140 | { |
||
| 141 | $manager = \Aimeos\MShop::create( $this->getContext(), 'order/status' ); |
||
| 142 | |||
| 143 | $item = $manager->create(); |
||
| 144 | $item->setParentId( $parentid ); |
||
| 145 | $item->setType( $type ); |
||
| 146 | $item->setValue( $value ); |
||
| 147 | |||
| 148 | $manager->save( $item, false ); |
||
| 149 | |||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the product articles and their bundle product codes for the given article ID |
||
| 156 | * |
||
| 157 | * @param string $prodId Product ID of the article whose stock level changed |
||
| 158 | * @return array Associative list of article codes as keys and lists of bundle product codes as values |
||
| 159 | */ |
||
| 160 | protected function getBundleMap( string $prodId ) : array |
||
| 161 | { |
||
| 162 | $bundleMap = []; |
||
| 163 | $productManager = \Aimeos\MShop::create( $this->context, 'product' ); |
||
| 164 | |||
| 165 | $search = $productManager->filter(); |
||
| 166 | $func = $search->make( 'product:has', ['product', 'default', $prodId] ); |
||
| 167 | $expr = array( |
||
| 168 | $search->compare( '==', 'product.type', 'bundle' ), |
||
| 169 | $search->compare( '!=', $func, null ), |
||
| 170 | ); |
||
| 171 | $search->setConditions( $search->and( $expr ) ); |
||
| 172 | $search->slice( 0, 0x7fffffff ); |
||
| 173 | |||
| 174 | $bundleItems = $productManager->search( $search, array( 'product' ) ); |
||
| 175 | |||
| 176 | foreach( $bundleItems as $bundleItem ) |
||
| 177 | { |
||
| 178 | foreach( $bundleItem->getRefItems( 'product', null, 'default' ) as $item ) { |
||
| 179 | $bundleMap[$item->getId()][] = $bundleItem->getId(); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | return $bundleMap; |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the context item object. |
||
| 189 | * |
||
| 190 | * @return \Aimeos\MShop\Context\Item\Iface Context item object |
||
| 191 | */ |
||
| 192 | protected function getContext() : \Aimeos\MShop\Context\Item\Iface |
||
| 193 | { |
||
| 194 | return $this->context; |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the last status item for the given order ID. |
||
| 200 | * |
||
| 201 | * @param string $parentid Order ID |
||
| 202 | * @param string $type Status type constant |
||
| 203 | * @param string $status New status value stored along with the order item |
||
| 204 | * @return \Aimeos\MShop\Order\Item\Status\Iface|null Order status item or NULL if no item is available |
||
| 205 | */ |
||
| 206 | protected function getLastStatusItem( string $parentid, string $type, string $status ) : ?\Aimeos\MShop\Order\Item\Status\Iface |
||
| 207 | { |
||
| 208 | $manager = \Aimeos\MShop::create( $this->getContext(), 'order/status' ); |
||
| 209 | |||
| 210 | $search = $manager->filter(); |
||
| 211 | $expr = array( |
||
| 212 | $search->compare( '==', 'order.status.parentid', $parentid ), |
||
| 213 | $search->compare( '==', 'order.status.type', $type ), |
||
| 214 | $search->compare( '==', 'order.status.value', $status ), |
||
| 215 | ); |
||
| 216 | $search->setConditions( $search->and( $expr ) ); |
||
| 217 | $search->setSortations( array( $search->sort( '-', 'order.status.ctime' ) ) ); |
||
| 218 | $search->slice( 0, 1 ); |
||
| 219 | |||
| 220 | return $manager->search( $search )->first(); |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the stock items for the given product codes |
||
| 226 | * |
||
| 227 | * @param iterable $prodIds List of product codes |
||
| 228 | * @param string $stockType Stock type code the stock items must belong to |
||
| 229 | * @return \Aimeos\Map Associative list of \Aimeos\MShop\Stock\Item\Iface and IDs as values |
||
| 230 | */ |
||
| 231 | protected function getStockItems( iterable $prodIds, string $stockType ) : \Aimeos\Map |
||
| 232 | { |
||
| 233 | $stockManager = \Aimeos\MShop::create( $this->context, 'stock' ); |
||
| 234 | |||
| 235 | $search = $stockManager->filter()->slice( 0, count( $prodIds ) ) |
||
| 236 | ->add( ['stock.productid' => $prodIds, 'stock.type' => $stockType] ); |
||
| 237 | |||
| 238 | return $stockManager->search( $search ); |
||
| 239 | } |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Increases or decreses the coupon code counts referenced in the order by the given value. |
||
| 244 | * |
||
| 245 | * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
||
| 246 | * @param int $how Positive or negative integer number for increasing or decreasing the coupon count |
||
| 247 | * @return \Aimeos\Controller\Common\Order\Iface Order controller for fluent interface |
||
| 248 | */ |
||
| 249 | protected function updateCoupons( \Aimeos\MShop\Order\Item\Iface $orderItem, int $how = +1 ) |
||
| 250 | { |
||
| 251 | $context = $this->getContext(); |
||
| 252 | $manager = \Aimeos\MShop::create( $context, 'order/base/coupon' ); |
||
| 253 | $couponCodeManager = \Aimeos\MShop::create( $context, 'coupon/code' ); |
||
| 254 | |||
| 255 | $search = $manager->filter(); |
||
| 256 | $search->setConditions( $search->compare( '==', 'order.base.coupon.baseid', $orderItem->getBaseId() ) ); |
||
| 257 | |||
| 258 | $start = 0; |
||
| 259 | |||
| 260 | $couponCodeManager->begin(); |
||
| 261 | |||
| 262 | try |
||
| 263 | { |
||
| 264 | do |
||
| 265 | { |
||
| 266 | $items = $manager->search( $search ); |
||
| 267 | |||
| 268 | foreach( $items as $item ) { |
||
| 269 | $couponCodeManager->decrease( $item->getCode(), $how * -1 ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $count = count( $items ); |
||
| 273 | $start += $count; |
||
| 274 | $search->slice( $start ); |
||
| 275 | } |
||
| 276 | while( $count >= $search->getLimit() ); |
||
| 277 | |||
| 278 | $couponCodeManager->commit(); |
||
| 279 | } |
||
| 280 | catch( \Exception $e ) |
||
| 281 | { |
||
| 282 | $couponCodeManager->rollback(); |
||
| 283 | throw $e; |
||
| 284 | } |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Increases or decreases the stock level or the coupon code count for referenced items of the given order. |
||
| 292 | * |
||
| 293 | * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
||
| 294 | * @param string $type Constant from \Aimeos\MShop\Order\Item\Status\Base, e.g. STOCK_UPDATE or COUPON_UPDATE |
||
| 295 | * @param string $status New status value stored along with the order item |
||
| 296 | * @param int $value Number to increse or decrease the stock level or coupon code count |
||
| 297 | * @return \Aimeos\Controller\Common\Order\Iface Order controller for fluent interface |
||
| 298 | */ |
||
| 299 | protected function updateStatus( \Aimeos\MShop\Order\Item\Iface $orderItem, string $type, string $status, int $value ) |
||
| 314 | } |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Increases or decreses the stock levels of the products referenced in the order by the given value. |
||
| 319 | * |
||
| 320 | * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object |
||
| 321 | * @param int $how Positive or negative integer number for increasing or decreasing the stock levels |
||
| 322 | * @return \Aimeos\Controller\Common\Order\Iface Order controller for fluent interface |
||
| 323 | */ |
||
| 324 | protected function updateStock( \Aimeos\MShop\Order\Item\Iface $orderItem, int $how = +1 ) |
||
| 370 | } |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Updates the stock levels of bundles for a specific type |
||
| 375 | * |
||
| 376 | * @param string $prodId Unique product ID |
||
| 377 | * @param string $stockType Unique stock type |
||
| 378 | * @return \Aimeos\Controller\Common\Order\Iface Order controller for fluent interface |
||
| 379 | */ |
||
| 380 | protected function updateStockBundle( string $prodId, string $stockType ) |
||
| 422 | } |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Updates the stock levels of selection products for a specific type |
||
| 427 | * |
||
| 428 | * @param string $prodId Unique product ID |
||
| 429 | * @param string $stocktype Unique stock type |
||
| 430 | * @return \Aimeos\Controller\Common\Order\Iface Order controller for fluent interface |
||
| 431 | */ |
||
| 432 | protected function updateStockSelection( string $prodId, string $stocktype ) |
||
| 454 |