Passed
Push — master ( da903b...844cfd )
by Aimeos
07:56
created

Standard::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 3
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Subscription\Process\End;
12
13
14
/**
15
 * Job controller for subscription processs end.
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
	/** controller/jobs/subscription/process/end/name
25
	 * Class name of the used subscription suggestions scheduler controller implementation
26
	 *
27
	 * Each default job controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Jobs\Subscription\Process\End\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Subscription\Process\End\Myend
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/subscription/process/end/name = Myend
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyEnd"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2018.04
55
	 */
56
57
	/** controller/jobs/subscription/process/end/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the subscription process CSV job controller
59
	 *
60
	 * Decorators extend the functionality of a class by adding new aspects
61
	 * (e.g. log what is currently done), executing the methods of the underlying
62
	 * class only in certain conditions (e.g. only for logged in users) or
63
	 * modify what is returned to the caller.
64
	 *
65
	 * This option allows you to remove a decorator added via
66
	 * "controller/jobs/common/decorators/default" before they are wrapped
67
	 * around the job controller.
68
	 *
69
	 *  controller/jobs/subscription/process/end/decorators/excludes = array( 'decorator1' )
70
	 *
71
	 * This would remove the decorator named "decorator1" from the list of
72
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
73
	 * "controller/jobs/common/decorators/default" to the job controller.
74
	 *
75
	 * @param array List of decorator names
76
	 * @since 2018.04
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/subscription/process/end/decorators/global
79
	 * @see controller/jobs/subscription/process/end/decorators/local
80
	 */
81
82
	/** controller/jobs/subscription/process/end/decorators/global
83
	 * Adds a list of globally available decorators only to the subscription process CSV job controller
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
92
	 *
93
	 *  controller/jobs/subscription/process/end/decorators/global = array( 'decorator1' )
94
	 *
95
	 * This would add the decorator named "decorator1" defined by
96
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
97
	 *
98
	 * @param array List of decorator names
99
	 * @since 2018.04
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/subscription/process/end/decorators/excludes
102
	 * @see controller/jobs/subscription/process/end/decorators/local
103
	 */
104
105
	/** controller/jobs/subscription/process/end/decorators/local
106
	 * Adds a list of local decorators only to the subscription process CSV job controller
107
	 *
108
	 * Decorators extend the functionality of a class by adding new aspects
109
	 * (e.g. log what is currently done), executing the methods of the underlying
110
	 * class only in certain conditions (e.g. only for logged in users) or
111
	 * modify what is returned to the caller.
112
	 *
113
	 * This option allows you to wrap local decorators
114
	 * ("\Aimeos\Controller\Jobs\Subscription\Process\End\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/subscription/process/end/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Subscription\Process\End\Decorator\Decorator2"
121
	 * only to the job controller.
122
	 *
123
	 * @param array List of decorator names
124
	 * @since 2018.04
125
	 * @see controller/jobs/common/decorators/default
126
	 * @see controller/jobs/subscription/process/end/decorators/excludes
127
	 * @see controller/jobs/subscription/process/end/decorators/global
128
	 */
129
130
131
	/**
132
	 * Returns the localized name of the job.
133
	 *
134
	 * @return string Name of the job
135
	 */
136
	public function getName() : string
137
	{
138
		return $this->context()->translate( 'controller/jobs', 'Subscription process end' );
139
	}
140
141
142
	/**
143
	 * Returns the localized description of the job.
144
	 *
145
	 * @return string Description of the job
146
	 */
147
	public function getDescription() : string
148
	{
149
		return $this->context()->translate( 'controller/jobs', 'Terminates expired subscriptions' );
150
	}
151
152
153
	/**
154
	 * Executes the job.
155
	 *
156
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
157
	 */
158
	public function run()
159
	{
160
		$context = $this->context();
161
		$processors = $this->getProcessors( $this->names() );
162
163
		$orderManager = \Aimeos\MShop::create( $context, 'order' );
164
		$manager = \Aimeos\MShop::create( $context, 'subscription' );
165
166
		$filter = $manager->filter( true )->add( 'subscription.dateend', '<', date( 'Y-m-d' ) )->slice( 0, $this->max() );
167
		$cursor = $manager->cursor( $filter );
168
169
		while( $items = $manager->iterate( $cursor ) )
170
		{
171
			$search = $orderManager->filter()->add( 'order.baseid', '==', $items->getOrderBaseId() )->slice( 0, 0x7fffffff );
172
			$orders = $orderManager->search( $search, $this->domains() )->col( null, 'order.baseid' );
173
174
			foreach( $items as $item )
175
			{
176
				if( ( $order = $orders->get( $item->getOrderBaseId() ) ) === null ) {
177
					continue;
178
				}
179
180
				$manager->begin();
181
				$orderManager->begin();
182
183
				try
184
				{
185
					$manager->save( $this->process( $item, $order, $processors ) );
186
					$orderManager->save( $order );
187
188
					$orderManager->commit();
189
					$manager->commit();
190
				}
191
				catch( \Exception $e )
192
				{
193
					$orderManager->rollback();
194
					$manager->rollback();
195
196
					$str = 'Unable to end subscription with ID "%1$s": %2$s';
197
					$msg = sprintf( $str, $item->getId(), $e->getMessage() . "\n" . $e->getTraceAsString() );
198
					$context->logger()->error( $msg, 'subscription/process/end' );
199
				}
200
			}
201
		}
202
	}
203
204
205
	/**
206
	 * Returns the domains that should be fetched together with the order data
207
	 *
208
	 * @return array List of domain names
209
	 */
210
	protected function domains() : array
211
	{
212
		/** controller/jobs/subscription/process/domains
213
		 * Associated items that should be available too in the subscription
214
		 *
215
		 * Orders consist of address, coupons, products and services. They can be
216
		 * fetched together with the subscription items and passed to the processor.
217
		 * Available domains for those items are:
218
		 *
219
		 * - order/base
220
		 * - order/base/address
221
		 * - order/base/coupon
222
		 * - order/base/product
223
		 * - order/base/service
224
		 *
225
		 * @param array Referenced domain names
226
		 * @since 2022.04
227
		 * @see controller/jobs/order/email/delivery/limit-days
228
		 * @see controller/jobs/order/service/delivery/batch-max
229
		 */
230
		$domains = ['order/base', 'order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'];
231
		return $this->context()->config()->get( 'controller/jobs/subscription/process/domains', $domains );
232
	}
233
234
235
	/**
236
	 * Returns the maximum number of orders processed at once
237
	 *
238
	 * @return int Maximum number of items
239
	 */
240
	protected function max() : int
241
	{
242
		/** controller/jobs/subscription/process/batch-max
243
		 * Maximum number of subscriptions processed at once by the subscription process job
244
		 *
245
		 * This setting configures the maximum number of subscriptions including
246
		 * orders that will be processed at once. Bigger batches an improve the
247
		 * performance but requires more memory.
248
		 *
249
		 * @param integer Number of subscriptions
250
		 * @since 2023.04
251
		 * @see controller/jobs/subscription/process/domains
252
		 * @see controller/jobs/subscription/process/names
253
		 * @see controller/jobs/subscription/process/payment-days
254
		 * @see controller/jobs/subscription/process/payment-status
255
		 */
256
		return $this->context()->config()->get( 'controller/jobs/subscription/process/batch-max', 100 );
257
	}
258
259
260
	/**
261
	 * Returns the names of the subscription processors
262
	 *
263
	 * @return array List of processor names
264
	 */
265
	protected function names() : array
266
	{
267
		/** controller/jobs/subscription/process/processors
268
		 * List of processor names that should be executed for subscriptions
269
		 *
270
		 * For each subscription a number of processors for different tasks can be executed.
271
		 * They can for example add a group to the customers' account during the customer
272
		 * has an active subscribtion.
273
		 *
274
		 * @param array List of processor names
275
		 * @since 2018.04
276
		 * @see controller/jobs/subscription/process/domains
277
		 * @see controller/jobs/subscription/process/max
278
		 * @see controller/jobs/subscription/process/payment-days
279
		 * @see controller/jobs/subscription/process/payment-status
280
		 */
281
		return (array) $this->context()->config()->get( 'controller/jobs/subscription/process/processors', [] );
282
	}
283
284
285
	/**
286
	 * Runs the passed processors over all items and updates the properties
287
	 *
288
	 * @param \Aimeos\MShop\Subscription\Item\Iface $item Subscription item
289
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order item including associated items
290
	 * @param iterable $processors List of processor objects
291
	 * @return \Aimeos\MShop\Subscription\Item\Iface Updated subscription item
292
	 */
293
	protected function process( \Aimeos\MShop\Subscription\Item\Iface $item,
294
		\Aimeos\MShop\Order\Item\Iface $order, iterable $processors ) : \Aimeos\MShop\Subscription\Item\Iface
295
	{
296
		foreach( $processors as $processor ) {
297
			$processor->end( $item, $orderItem );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $orderItem does not exist. Did you maybe mean $order?
Loading history...
298
		}
299
300
		if( ( $reason = $item->getReason() ) === null ) {
301
			$reason = \Aimeos\MShop\Subscription\Item\Iface::REASON_END;
302
		}
303
304
		return $item->setReason( $reason )->setStatus( 0 );
305
	}
306
}
307