| Conditions | 5 |
| Paths | 19 |
| Total Lines | 134 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 52 | public function run() |
||
| 53 | { |
||
| 54 | $aimeos = $this->getAimeos(); |
||
| 55 | $context = $this->getContext(); |
||
| 56 | $config = $context->getConfig(); |
||
| 57 | $mailer = $context->getMail(); |
||
| 58 | $view = $context->getView(); |
||
| 59 | |||
| 60 | $templatePaths = $aimeos->getCustomPaths( 'client/html/templates' ); |
||
| 61 | |||
| 62 | $helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config ); |
||
| 63 | $view->addHelper( 'config', $helper ); |
||
| 64 | |||
| 65 | $client = \Aimeos\Client\Html\Email\Payment\Factory::createClient( $context, $templatePaths ); |
||
| 66 | |||
| 67 | $orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $context ); |
||
| 68 | $orderStatusManager = $orderManager->getSubManager( 'status' ); |
||
| 69 | $orderBaseManager = $orderManager->getSubManager( 'base' ); |
||
| 70 | |||
| 71 | /** controller/jobs/order/email/payment/standard/limit-days |
||
| 72 | * Only send payment e-mails of orders that were created in the past within the configured number of days |
||
| 73 | * |
||
| 74 | * The payment e-mails are normally send immediately after the payment |
||
| 75 | * status has changed. This option prevents e-mails for old order from |
||
| 76 | * being send in case anything went wrong or an update failed to avoid |
||
| 77 | * confusion of customers. |
||
| 78 | * |
||
| 79 | * @param integer Number of days |
||
| 80 | * @since 2014.03 |
||
| 81 | * @category User |
||
| 82 | * @category Developer |
||
| 83 | * @see controller/jobs/order/email/delivery/standard/limit-days |
||
| 84 | * @see controller/jobs/service/delivery/process/limit-days |
||
| 85 | */ |
||
| 86 | $limit = $config->get( 'controller/jobs/order/email/payment/standard/limit-days', 30 ); |
||
| 87 | $limitDate = date( 'Y-m-d H:i:s', time() - $limit * 86400 ); |
||
| 88 | |||
| 89 | $default = array( |
||
| 90 | \Aimeos\MShop\Order\Item\Base::PAY_REFUND, |
||
| 91 | \Aimeos\MShop\Order\Item\Base::PAY_PENDING, |
||
| 92 | \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, |
||
| 93 | \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED, |
||
| 94 | ); |
||
| 95 | |||
| 96 | /** controller/jobs/order/email/payment/standard/status |
||
| 97 | * Only send order payment notification e-mails for these payment status values |
||
| 98 | * |
||
| 99 | * Notification e-mail about payment status changes can be sent for these |
||
| 100 | * status values: |
||
| 101 | * * 0: deleted |
||
| 102 | * * 1: canceled |
||
| 103 | * * 2: refused |
||
| 104 | * * 3: refund |
||
| 105 | * * 4: pending |
||
| 106 | * * 5: authorized |
||
| 107 | * * 6: received |
||
| 108 | * |
||
| 109 | * User-defined status values are possible but should be in the private |
||
| 110 | * block of values between 30000 and 32767. |
||
| 111 | * |
||
| 112 | * @param integer Payment status constant |
||
| 113 | * @since 2014.03 |
||
| 114 | * @category User |
||
| 115 | * @category Developer |
||
| 116 | * @see controller/jobs/order/email/delivery/standard/status |
||
| 117 | * @see controller/jobs/order/email/payment/standard/limit-days |
||
| 118 | */ |
||
| 119 | foreach( (array) $config->get( 'controller/jobs/order/email/payment/standard/status', $default ) as $status ) |
||
| 120 | { |
||
| 121 | $orderSearch = $orderManager->createSearch(); |
||
| 122 | |||
| 123 | $param = array( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_PAYMENT, $status ); |
||
| 124 | $orderFunc = $orderSearch->createFunction( 'order.containsStatus', $param ); |
||
| 125 | |||
| 126 | $expr = array( |
||
| 127 | $orderSearch->compare( '>=', 'order.mtime', $limitDate ), |
||
| 128 | $orderSearch->compare( '==', 'order.statuspayment', $status ), |
||
| 129 | $orderSearch->compare( '==', $orderFunc, 0 ), |
||
| 130 | ); |
||
| 131 | $orderSearch->setConditions( $orderSearch->combine( '&&', $expr ) ); |
||
| 132 | |||
| 133 | $start = 0; |
||
| 134 | |||
| 135 | do |
||
| 136 | { |
||
| 137 | $items = $orderManager->searchItems( $orderSearch ); |
||
| 138 | |||
| 139 | foreach( $items as $id => $item ) |
||
| 140 | { |
||
| 141 | try |
||
| 142 | { |
||
| 143 | $orderBaseItem = $orderBaseManager->load( $item->getBaseId() ); |
||
| 144 | |||
| 145 | $addr = $orderBaseItem->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
||
| 146 | |||
| 147 | $view->extAddressItem = $addr; |
||
| 148 | $view->extOrderBaseItem = $orderBaseItem; |
||
| 149 | $view->extOrderItem = $item; |
||
| 150 | |||
| 151 | $helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $context->getI18n( $addr->getLanguageId() ) ); |
||
| 152 | $view->addHelper( 'translate', $helper ); |
||
| 153 | |||
| 154 | $message = $mailer->createMessage(); |
||
| 155 | $helper = new \Aimeos\MW\View\Helper\Mail\Standard( $view, $message ); |
||
| 156 | $view->addHelper( 'mail', $helper ); |
||
| 157 | |||
| 158 | $client->setView( $view ); |
||
| 159 | $client->getHeader(); |
||
| 160 | $client->getBody(); |
||
| 161 | |||
| 162 | $mailer->send( $message ); |
||
| 163 | |||
| 164 | $statusItem = $orderStatusManager->createItem(); |
||
| 165 | $statusItem->setParentId( $id ); |
||
| 166 | $statusItem->setType( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_PAYMENT ); |
||
| 167 | $statusItem->setValue( $status ); |
||
| 168 | |||
| 169 | $orderStatusManager->saveItem( $statusItem ); |
||
| 170 | } |
||
| 171 | catch( \Exception $e ) |
||
| 172 | { |
||
| 173 | $str = 'Error while trying to send payment e-mail for order ID "%1$s" and status "%2$s": %3$s'; |
||
| 174 | $msg = sprintf( $str, $item->getId(), $item->getPaymentStatus(), $e->getMessage() ); |
||
| 175 | $context->getLogger()->log( $msg ); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | $count = count( $items ); |
||
| 180 | $start += $count; |
||
| 181 | $orderSearch->setSlice( $start ); |
||
| 182 | } |
||
| 183 | while( $count >= $orderSearch->getSliceSize() ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 |