Completed
Push — master ( 8d5e0f...0ed5fa )
by Aimeos
02:42
created

Standard   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 117
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getDescription() 0 4 1
C run() 0 85 9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Subscription\Process\Begin;
12
13
14
/**
15
 * Job controller for subscription processs start.
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Subscription\Process\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/**
25
	 * Returns the localized name of the job.
26
	 *
27
	 * @return string Name of the job
28
	 */
29
	public function getName()
30
	{
31
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Subscription process start' );
32
	}
33
34
35
	/**
36
	 * Returns the localized description of the job.
37
	 *
38
	 * @return string Description of the job
39
	 */
40
	public function getDescription()
41
	{
42
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Process subscriptions initially' );
43
	}
44
45
46
	/**
47
	 * Executes the job.
48
	 *
49
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
50
	 */
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