Completed
Push — master ( 7313f0...a0f3ba )
by Aimeos
02:53
created

Standard   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 93
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getDescription() 0 4 1
B run() 0 61 5
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\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
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Controller\Jobs\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
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 end' );
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', 'Terminates expired subscriptions' );
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
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'subscription' );
72
73
		$search = $manager->createSearch( true );
74
		$expr = [
75
			$search->compare( '<', 'subscription.dateend', date( 'Y-m-d' ) ),
76
			$search->getConditions(),
77
		];
78
		$search->setConditions( $search->combine( '&&', $expr ) );
79
		$search->setSortations( [$search->sort( '+', 'subscription.id' )] );
80
81
		$start = 0;
82
83
		do
84
		{
85
			$search->setSlice( $start, 100 );
86
			$items = $manager->searchItems( $search );
87
88
			foreach( $items as $item )
89
			{
90
				try
91
				{
92
					foreach( $processors as $processor ) {
93
						$processor->end( $item );
94
					}
95
96
					$item->setStatus( 0 );
97
					$manager->saveItem( $item );
98
				}
99
				catch( \Exception $e )
100
				{
101
					$msg = 'Unable to process subscription with ID "%1$S": %2$s';
102
					$logger->log( sprintf( $msg, $item->getId(), $e->getMessage() ) );
103
					$logger->log( $e->getTraceAsString() );
104
				}
105
			}
106
107
			$count = count( $items );
108
			$start += $count;
109
		}
110
		while( $count === $search->getSliceSize() );
111
	}
112
}
113