| Conditions | 9 |
| Paths | 60 |
| Total Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | public function run() |
||
| 52 | { |
||
| 53 | $context = $this->getContext(); |
||
| 54 | $config = $context->getConfig(); |
||
| 55 | $logger = $context->getLogger(); |
||
| 56 | |||
| 57 | /** controller/common/subscription/process/processors |
||
| 58 | * List of processor names that should be executed for subscriptions |
||
| 59 | * |
||
| 60 | * For each subscription a number of processors for different tasks can be executed. |
||
| 61 | * They can for example add a group to the customers' account during the customer |
||
| 62 | * has an active subscribtion. |
||
| 63 | * |
||
| 64 | * @param array List of processor names |
||
| 65 | * @since 2018.04 |
||
| 66 | * @category Developer |
||
| 67 | */ |
||
| 68 | $names = (array) $config->get( 'controller/common/subscription/process/processors', [] ); |
||
| 69 | |||
| 70 | $processors = $this->getProcessors( $names ); |
||
| 71 | $orderManager = \Aimeos\MShop\Factory::createManager( $context, 'order' ); |
||
| 72 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'subscription' ); |
||
| 73 | |||
| 74 | $search = $manager->createSearch( true ); |
||
| 75 | $expr = [ |
||
| 76 | $search->compare( '==', 'subscription.datenext', null ), |
||
| 77 | $search->getConditions(), |
||
| 78 | ]; |
||
| 79 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
| 80 | $search->setSortations( [$search->sort( '+', 'subscription.id' )] ); |
||
| 81 | |||
| 82 | $status = \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED; |
||
| 83 | $start = 0; |
||
| 84 | |||
| 85 | do |
||
| 86 | { |
||
| 87 | $ordBaseIds = $payStatus = []; |
||
| 88 | |||
| 89 | $search->setSlice( $start, 100 ); |
||
| 90 | $items = $manager->searchItems( $search ); |
||
| 91 | |||
| 92 | foreach( $items as $item ) { |
||
| 93 | $ordBaseIds[] = $item->getOrderBaseId(); |
||
| 94 | } |
||
| 95 | |||
| 96 | $orderSearch = $orderManager->createSearch()->setSlice( 0, $search->getSliceSize() ); |
||
| 97 | $orderSearch->setConditions( $orderSearch->compare( '==', 'order.base.id', $ordBaseIds ) ); |
||
| 98 | |||
| 99 | foreach( $orderManager->searchItems( $orderSearch ) as $orderItem ) { |
||
| 100 | $payStatus[$orderItem->getBaseId()] = $orderItem->getPaymentStatus(); |
||
| 101 | } |
||
| 102 | |||
| 103 | foreach( $items as $item ) |
||
| 104 | { |
||
| 105 | try |
||
| 106 | { |
||
| 107 | if( isset( $payStatus[$item->getOrderBaseId()] ) && $payStatus[$item->getOrderBaseId()] >= $status ) |
||
| 108 | { |
||
| 109 | foreach( $processors as $processor ) { |
||
| 110 | $processor->begin( $item ); |
||
| 111 | } |
||
| 112 | |||
| 113 | $interval = new \DateInterval( $item->getInterval() ); |
||
| 114 | $item->setDateNext( date_create( $item->getTimeCreated() )->add( $interval )->format( 'Y-m-d' ) ); |
||
| 115 | } |
||
| 116 | else |
||
| 117 | { |
||
| 118 | $item->setStatus( 0 ); |
||
| 119 | } |
||
| 120 | |||
| 121 | $manager->saveItem( $item ); |
||
| 122 | } |
||
| 123 | catch( \Exception $e ) |
||
| 124 | { |
||
| 125 | $msg = 'Unable to process subscription with ID "%1$S": %2$s'; |
||
| 126 | $logger->log( sprintf( $msg, $item->getId(), $e->getMessage() ) ); |
||
| 127 | $logger->log( $e->getTraceAsString() ); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $count = count( $items ); |
||
| 132 | $start += $count; |
||
| 133 | } |
||
| 134 | while( $count === $search->getSliceSize() ); |
||
| 135 | } |
||
| 136 | } |
||
| 137 |