Passed
Push — master ( e985b5...b70995 )
by Aimeos
03:29
created

Standard::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Basket\Cleanup;
12
13
14
/**
15
 * Basket cleanup controller.
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/** controller/jobs/basket/cleanup/name
25
	 * Class name of the used basket cleanup scheduler controller implementation
26
	 *
27
	 * Each default log 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\Basket\Cleanup\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Basket\Cleanup\Mylog
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/basket/cleanup/name = Mylog
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 "MyLog"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2023.10
55
	 */
56
57
	/** controller/jobs/basket/cleanup/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the basket cleanup controllers
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/basket/cleanup/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 this job controller.
74
	 *
75
	 * @param array List of decorator names
76
	 * @since 2023.10
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/basket/cleanup/decorators/global
79
	 * @see controller/jobs/basket/cleanup/decorators/local
80
	 */
81
82
	/** controller/jobs/basket/cleanup/decorators/global
83
	 * Adds a list of globally available decorators only to the basket cleanup controllers
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/basket/cleanup/decorators/global = array( 'decorator1' )
94
	 *
95
	 * This would add the decorator named "decorator1" defined by
96
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to this job controller.
97
	 *
98
	 * @param array List of decorator names
99
	 * @since 2023.10
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/basket/cleanup/decorators/excludes
102
	 * @see controller/jobs/basket/cleanup/decorators/local
103
	 */
104
105
	/** controller/jobs/basket/cleanup/decorators/local
106
	 * Adds a list of local decorators only to the basket cleanup controllers
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\Basket\Cleanup\Decorator\*") around this job controller.
115
	 *
116
	 *  controller/jobs/basket/cleanup/decorators/local = array( 'decorator2' )
117
	 *
118
	 * This would add the decorator named "decorator2" defined by
119
	 * "\Aimeos\Controller\Jobs\Basket\Cleanup\Decorator\Decorator2" only to this job
120
	 * controller.
121
	 *
122
	 * @param array List of decorator names
123
	 * @since 2023.10
124
	 * @see controller/jobs/common/decorators/default
125
	 * @see controller/jobs/basket/cleanup/decorators/excludes
126
	 * @see controller/jobs/basket/cleanup/decorators/global
127
	 */
128
129
130
	/**
131
	 * Returns the localized name of the job.
132
	 *
133
	 * @return string Name of the job
134
	 */
135
	public function getName() : string
136
	{
137
		return $this->context()->translate( 'controller/jobs', 'Cleanup baskets' );
138
	}
139
140
141
	/**
142
	 * Returns the localized description of the job.
143
	 *
144
	 * @return string Description of the job
145
	 */
146
	public function getDescription() : string
147
	{
148
		return $this->context()->translate( 'controller/jobs', 'Removes the old baskets from the database' );
149
	}
150
151
152
	/**
153
	 * Executes the job.
154
	 *
155
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
156
	 */
157
	public function run()
158
	{
159
		$manager = \Aimeos\MShop::create( $this->context(), 'order/basket' );
160
		$filter = $manager->filter()->add( 'order.basket.mtime', '<=', $this->timestamp() );
161
		$cursor = $manager->cursor( $filter->slice( 0, 1000 ) );
162
163
		while( $items = $manager->iterate( $cursor ) ) {
164
			$manager->delete( $items );
165
		}
166
	}
167
168
169
	/**
170
	 * Returns the timestamp until the logs entries should be moved
171
	 *
172
	 * @return string Timestamp in "YYYY-MM-DD HH:mm:ss" format
173
	 */
174
	protected function timestamp() : string
175
	{
176
		/** controller/jobs/basket/cleanup/limit-days
177
		 * Only remove log entries that were created berore the configured number of days
178
		 *
179
		 * This option specifies the number of days log entries will be kept in
180
		 * the database. Afterwards, they will be removed and archived.
181
		 *
182
		 * @param integer Number of days
183
		 * @since 2023.10
184
		 * @see controller/jobs/basket/cleanup/path
185
		 */
186
		$limit = $this->context()->config()->get( 'controller/jobs/basket/cleanup/limit-days', 30 );
187
		return date( 'Y-m-d H:i:s', time() - $limit * 86400 );
188
	}
189
}
190