Passed
Push — master ( 0a49bb...da903b )
by Aimeos
03:06
created

Standard::names()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 17
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\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
	/** controller/jobs/subscription/process/begin/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\Begin\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Subscription\Process\Begin\Mybegin
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/subscription/process/begin/name = Mybegin
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 "MyBegin"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2018.04
55
	 * @category Developer
56
	 */
57
58
	/** controller/jobs/subscription/process/begin/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the subscription process CSV job controller
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/jobs/common/decorators/default" before they are wrapped
68
	 * around the job controller.
69
	 *
70
	 *  controller/jobs/subscription/process/begin/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
74
	 * "controller/jobs/common/decorators/default" to the job controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2018.04
78
	 * @category Developer
79
	 * @see controller/jobs/common/decorators/default
80
	 * @see controller/jobs/subscription/process/begin/decorators/global
81
	 * @see controller/jobs/subscription/process/begin/decorators/local
82
	 */
83
84
	/** controller/jobs/subscription/process/begin/decorators/global
85
	 * Adds a list of globally available decorators only to the subscription process CSV job controller
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
94
	 *
95
	 *  controller/jobs/subscription/process/begin/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2018.04
102
	 * @category Developer
103
	 * @see controller/jobs/common/decorators/default
104
	 * @see controller/jobs/subscription/process/begin/decorators/excludes
105
	 * @see controller/jobs/subscription/process/begin/decorators/local
106
	 */
107
108
	/** controller/jobs/subscription/process/begin/decorators/local
109
	 * Adds a list of local decorators only to the subscription process CSV job controller
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Jobs\Subscription\Process\Begin\Decorator\*") around the job
118
	 * controller.
119
	 *
120
	 *  controller/jobs/subscription/process/begin/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\Controller\Jobs\Subscription\Process\Begin\Decorator\Decorator2"
124
	 * only to the job controller.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2018.04
128
	 * @category Developer
129
	 * @see controller/jobs/common/decorators/default
130
	 * @see controller/jobs/subscription/process/begin/decorators/excludes
131
	 * @see controller/jobs/subscription/process/begin/decorators/global
132
	 */
133
134
135
	/**
136
	 * Returns the localized name of the job.
137
	 *
138
	 * @return string Name of the job
139
	 */
140
	public function getName() : string
141
	{
142
		return $this->context()->translate( 'controller/jobs', 'Subscription process start' );
143
	}
144
145
146
	/**
147
	 * Returns the localized description of the job.
148
	 *
149
	 * @return string Description of the job
150
	 */
151
	public function getDescription() : string
152
	{
153
		return $this->context()->translate( 'controller/jobs', 'Process subscriptions initially' );
154
	}
155
156
157
	/**
158
	 * Executes the job.
159
	 *
160
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
161
	 */
162
	public function run()
163
	{
164
		$context = $this->context();
165
		$processors = $this->getProcessors( $this->names() );
166
167
		$orderManager = \Aimeos\MShop::create( $context, 'order' );
168
		$manager = \Aimeos\MShop::create( $context, 'subscription' );
169
170
		$filter = $manager->filter( true )->add( 'subscription.datenext', '==', null )->slice( 0, $this->max() );
171
		$cursor = $manager->cursor( $filter );
172
173
		while( $items = $manager->iterate( $cursor ) )
174
		{
175
			$search = $orderManager->filter()->add( 'order.baseid', '==', $items->getOrderBaseId() )->slice( 0, 0x7fffffff );
176
			$orders = $orderManager->search( $search, $this->domains() )->col( null, 'order.baseid' );
177
178
			foreach( $items as $item )
179
			{
180
				if( ( $order = $orders->get( $item->getOrderBaseId() ) ) === null ) {
181
					continue;
182
				}
183
184
				$manager->begin();
185
				$orderManager->begin();
186
187
				try
188
				{
189
					$manager->save( $this->process( $item, $order, $processors ) );
190
					$orderManager->save( $order );
191
192
					$orderManager->commit();
193
					$manager->commit();
194
				}
195
				catch( \Exception $e )
196
				{
197
					$orderManager->rollback();
198
					$manager->rollback();
199
200
					$str = 'Unable to begin subscription with ID "%1$s": %2$s';
201
					$msg = sprintf( $str, $item->getId(), $e->getMessage() . "\n" . $e->getTraceAsString() );
202
					$context->logger()->error( $msg, 'subscription/process/begin' );
203
				}
204
			}
205
		}
206
	}
207
208
209
	/**
210
	 * Returns the domains that should be fetched together with the order data
211
	 *
212
	 * @return array List of domain names
213
	 */
214
	protected function domains() : array
215
	{
216
		/** controller/jobs/subscription/process/domains
217
		 * Associated items that should be available too in the subscription
218
		 *
219
		 * Orders consist of address, coupons, products and services. They can be
220
		 * fetched together with the subscription items and passed to the processor.
221
		 * Available domains for those items are:
222
		 *
223
		 * - order/base
224
		 * - order/base/address
225
		 * - order/base/coupon
226
		 * - order/base/product
227
		 * - order/base/service
228
		 *
229
		 * @param array Referenced domain names
230
		 * @since 2022.04
231
		 * @see controller/jobs/order/email/delivery/limit-days
232
		 * @see controller/jobs/order/service/delivery/batch-max
233
		 */
234
		$domains = ['order/base', 'order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'];
235
		return $this->context()->config()->get( 'controller/jobs/subscription/process/domains', $domains );
236
	}
237
238
239
	/**
240
	 * Returns the payment date until orders should be processed
241
	 *
242
	 * @return string Date/time in "YYYY-MM-DD HH:mm:ss" format
243
	 */
244
	protected function limit() : string
245
	{
246
		/** controller/jobs/subscription/process/payment-days
247
		 * Number of days to wait for the payment until subscription is removed
248
		 *
249
		 * Subscriptions wait for the confiugrable number of days until the payment
250
		 * status changes to a valid payment (by default: "authorized" and "received").
251
		 * If the payment arrives within this time frame, the subscription is activated.
252
		 * Otherwise, the subscription is removed from the list of subscriptions that
253
		 * will be checked for activation.
254
		 *
255
		 * @param float Number of days
256
		 * @since 2018.07
257
		 * @see controller/jobs/subscription/process/processors
258
		 * @see controller/jobs/subscription/process/payment-status
259
		 */
260
		$days = $this->context()->config()->get( 'controller/jobs/subscription/process/payment-days', 90 );
261
		return date( 'Y-m-d H:i:s', time() - 86400 * $days );
262
	}
263
264
265
	/**
266
	 * Returns the maximum number of orders processed at once
267
	 *
268
	 * @return int Maximum number of items
269
	 */
270
	protected function max() : int
271
	{
272
		/** controller/jobs/subscription/process/batch-max
273
		 * Maximum number of subscriptions processed at once by the subscription process job
274
		 *
275
		 * This setting configures the maximum number of subscriptions including
276
		 * orders that will be processed at once. Bigger batches an improve the
277
		 * performance but requires more memory.
278
		 *
279
		 * @param integer Number of subscriptions
280
		 * @since 2023.04
281
		 * @see controller/jobs/subscription/process/domains
282
		 * @see controller/jobs/subscription/process/names
283
		 * @see controller/jobs/subscription/process/payment-days
284
		 * @see controller/jobs/subscription/process/payment-status
285
		 */
286
		return $this->context()->config()->get( 'controller/jobs/subscription/process/batch-max', 100 );
287
	}
288
289
290
	/**
291
	 * Returns the names of the subscription processors
292
	 *
293
	 * @return array List of processor names
294
	 */
295
	protected function names() : array
296
	{
297
		/** controller/jobs/subscription/process/processors
298
		 * List of processor names that should be executed for subscriptions
299
		 *
300
		 * For each subscription a number of processors for different tasks can be executed.
301
		 * They can for example add a group to the customers' account during the customer
302
		 * has an active subscribtion.
303
		 *
304
		 * @param array List of processor names
305
		 * @since 2018.04
306
		 * @see controller/jobs/subscription/process/domains
307
		 * @see controller/jobs/subscription/process/max
308
		 * @see controller/jobs/subscription/process/payment-days
309
		 * @see controller/jobs/subscription/process/payment-status
310
		 */
311
		return (array) $this->context()->config()->get( 'controller/jobs/subscription/process/processors', [] );
312
	}
313
314
315
	/**
316
	 * Runs the passed processors over all items and updates the properties
317
	 *
318
	 * @param \Aimeos\MShop\Subscription\Item\Iface $item Subscription item
319
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order item including associated items
320
	 * @param iterable $processors List of processor objects
321
	 * @return \Aimeos\MShop\Subscription\Item\Iface Updated subscription item
322
	 */
323
	protected function process( \Aimeos\MShop\Subscription\Item\Iface $item,
324
		\Aimeos\MShop\Order\Item\Iface $order, iterable $processors ) : \Aimeos\MShop\Subscription\Item\Iface
325
	{
326
		if( $order->getStatusPayment() >= $this->status() )
327
		{
328
			foreach( $processors as $processor ) {
329
				$processor->begin( $item, $order );
330
			}
331
332
			$interval = new \DateInterval( $item->getInterval() );
333
			$dateNext = date_create( $item->getTimeCreated() )->add( $interval )->format( 'Y-m-d' );
0 ignored issues
show
Bug introduced by
It seems like $item->getTimeCreated() can also be of type null; however, parameter $datetime of date_create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

333
			$dateNext = date_create( /** @scrutinizer ignore-type */ $item->getTimeCreated() )->add( $interval )->format( 'Y-m-d' );
Loading history...
334
335
			return $item->setDateNext( $dateNext )->setPeriod( 1 );
336
		}
337
338
		if( $item->getTimeCreated() < $this->limit() ) {
339
			$item->setStatus( 0 );
340
		}
341
342
		return $item;
343
	}
344
345
346
	/**
347
	 * Returns the minimum payment status to activate subscriptions
348
	 *
349
	 * @return int Minimum payment status
350
	 */
351
	protected function status() : int
352
	{
353
		/** controller/jobs/subscription/process/payment-status
354
		 * Minimum payment status that will activate the subscription
355
		 *
356
		 * Subscriptions will be activated if the payment status of the order is
357
		 * at least the configured payment constant. The default payment status
358
		 * is "authorized" so orders with a payment status of "authorized" (5) and
359
		 * "received" (6) will cause the subscription to be activated. Lower
360
		 * payment status values, e.g. "pending" (4) won't activate the subscription.
361
		 *
362
		 * @param integer Payment status constant
363
		 * @since 2018.07
364
		 * @see controller/jobs/subscription/process/begin/domains
365
		 * @see controller/jobs/subscription/process/begin/max
366
		 * @see controller/jobs/subscription/process/begin/names
367
		 * @see controller/jobs/subscription/process/payment-days
368
		 */
369
		$status = \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED;
370
		return $this->context()->config()->get( 'controller/jobs/subscription/process/payment-status', $status );
371
	}
372
}
373