Completed
Branch master (c3c77b)
by Aimeos
03:13
created

Standard::run()   B

Complexity

Conditions 9
Paths 24

Size

Total Lines 180
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 36
nc 24
nop 0
dl 0
loc 180
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package Controller
8
 * @subpackage Jobs
9
 */
10
11
12
namespace Aimeos\Controller\Jobs\Admin\Log;
13
14
15
/**
16
 * Admin log controller.
17
 *
18
 * @package Controller
19
 * @subpackage Jobs
20
 */
21
class Standard
22
	extends \Aimeos\Controller\Jobs\Base
23
	implements \Aimeos\Controller\Jobs\Iface
24
{
25
	/**
26
	 * Returns the localized name of the job.
27
	 *
28
	 * @return string Name of the job
29
	 */
30
	public function getName()
31
	{
32
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Log cleanup' );
33
	}
34
35
36
	/**
37
	 * Returns the localized description of the job.
38
	 *
39
	 * @return string Description of the job
40
	 */
41
	public function getDescription()
42
	{
43
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Removes the old log entries from the database and archives them (optional)' );
44
	}
45
46
47
	/**
48
	 * Executes the job.
49
	 *
50
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
51
	 */
52
	public function run()
53
	{
54
		$context = $this->getContext();
55
		$config = $context->getConfig();
56
		$container = null;
57
58
		/** controller/jobs/admin/log/standard/limit-days
59
		 * Only remove log entries that were created berore the configured number of days
60
		 *
61
		 * This option specifies the number of days log entries will be kept in
62
		 * the database. Afterwards, they will be removed and archived if a
63
		 * path for storing the archive files is configured.
64
		 *
65
		 * @param integer Number of days
66
		 * @since 2014.09
67
		 * @category User
68
		 * @category Developer
69
		 * @see controller/jobs/admin/log/standard/path
70
		 * @see controller/jobs/admin/log/standard/container/type
71
		 * @see controller/jobs/admin/log/standard/container/format
72
		 * @see controller/jobs/admin/log/standard/container/options
73
		 */
74
		$limit = $config->get( 'controller/jobs/admin/log/standard/limit-days', 30 );
75
		$limitDate = date( 'Y-m-d H:i:s', time() - $limit * 86400 );
76
77
		/** controller/jobs/admin/log/standard/path
78
		 * Path to a writable directory where the log archive files should be stored
79
		 *
80
		 * During normal operation, a lot of data can be logged, not only for
81
		 * errors that have occured. By default, these data is written into the
82
		 * log database and its size will grow if old log entries are not
83
		 * removed. There's a job controller available that can delete old log
84
		 * entries.
85
		 *
86
		 * If an absolute path to a writeable directory in the file system is
87
		 * set via this configuration option, the job controller will save the
88
		 * old log entries to a file in this path. They can be analysed later
89
		 * if required.
90
		 *
91
		 * The type and format of these files as well as the time frame after
92
		 * the log entries are removed from the log database can be configured
93
		 * too.
94
		 *
95
		 * @param string Absolute file system path to a writable directory
96
		 * @since 2014.09
97
		 * @category Developer
98
		 * @category User
99
		 * @see controller/jobs/admin/log/standard/container/type
100
		 * @see controller/jobs/admin/log/standard/container/format
101
		 * @see controller/jobs/admin/log/standard/container/options
102
		 * @see controller/jobs/admin/log/standard/limit-days
103
		 */
104
		if( ( $path = $config->get( 'controller/jobs/admin/log/standard/path', null ) ) !== null )
105
		{
106
			/** controller/jobs/admin/log/standard/container/type
107
			 * Container file type storing all coupon code files to import
108
			 *
109
			 * All coupon code files or content objects must be put into one
110
			 * container file so editors don't have to upload one file for each
111
			 * coupon code file.
112
			 *
113
			 * The container file types that are supported by default are:
114
			 * * Zip
115
			 *
116
			 * Extensions implement other container types like spread sheets, XMLs or
117
			 * more advanced ways of handling the exported data.
118
			 *
119
			 * @param string Container file type
120
			 * @since 2014.09
121
			 * @category Developer
122
			 * @category User
123
			 * @see controller/jobs/admin/log/standard/path
124
			 * @see controller/jobs/admin/log/standard/container/format
125
			 * @see controller/jobs/admin/log/standard/container/options
126
			 * @see controller/jobs/admin/log/standard/limit-days
127
			 */
128
129
			/** controller/jobs/admin/log/standard/container/format
130
			 * Format of the coupon code files to import
131
			 *
132
			 * The coupon codes are stored in one or more files or content
133
			 * objects. The format of that file or content object can be configured
134
			 * with this option but most formats are bound to a specific container
135
			 * type.
136
			 *
137
			 * The formats that are supported by default are:
138
			 * * CSV (requires container type "Zip")
139
			 *
140
			 * Extensions implement other container types like spread sheets, XMLs or
141
			 * more advanced ways of handling the exported data.
142
			 *
143
			 * @param string Content file type
144
			 * @since 2014.09
145
			 * @category Developer
146
			 * @category User
147
			 * @see controller/jobs/admin/log/standard/path
148
			 * @see controller/jobs/admin/log/standard/container/type
149
			 * @see controller/jobs/admin/log/standard/container/options
150
			 * @see controller/jobs/admin/log/standard/limit-days
151
			 */
152
153
			/** controller/jobs/admin/log/standard/container/options
154
			 * Options changing the expected format of the coupon codes to import
155
			 *
156
			 * Each content format may support some configuration options to change
157
			 * the output for that content type.
158
			 *
159
			 * The options for the CSV content format are:
160
			 * * csv-separator, default ','
161
			 * * csv-enclosure, default '"'
162
			 * * csv-escape, default '"'
163
			 * * csv-lineend, default '\n'
164
			 *
165
			 * For format options provided by other container types implemented by
166
			 * extensions, please have a look into the extension documentation.
167
			 *
168
			 * @param array Associative list of options with the name as key and its value
169
			 * @since 2014.09
170
			 * @category Developer
171
			 * @category User
172
			 * @see controller/jobs/admin/log/standard/path
173
			 * @see controller/jobs/admin/log/standard/container/type
174
			 * @see controller/jobs/admin/log/standard/container/format
175
			 * @see controller/jobs/admin/log/standard/limit-days
176
			 */
177
178
			$type = $config->get( 'controller/jobs/admin/log/standard/container/type', 'Zip' );
179
			$format = $config->get( 'controller/jobs/admin/log/standard/container/format', 'CSV' );
180
			$options = $config->get( 'controller/jobs/admin/log/standard/container/options', [] );
181
182
			$path .= DIRECTORY_SEPARATOR . str_replace( ' ', '_', $limitDate );
183
			$container = \Aimeos\MW\Container\Factory::getContainer( $path, $type, $format, $options );
184
		}
185
186
		$manager = \Aimeos\MAdmin::create( $context, 'log' );
187
188
		$search = $manager->createSearch();
189
		$search->setConditions( $search->compare( '<=', 'log.timestamp', $limitDate ) );
190
		$search->setSortations( array( $search->sort( '+', 'log.timestamp' ) ) );
191
192
		$start = 0;
193
		$contents = [];
194
195
		do
196
		{
197
			$ids = [];
198
			$items = $manager->searchItems( $search );
199
200
			foreach( $items as $id => $item )
201
			{
202
				if( $container !== null )
203
				{
204
					$facility = $item->getFacility();
1 ignored issue
show
Bug introduced by
The method getFacility() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Item\Iface such as Aimeos\MAdmin\Log\Item\Iface or Aimeos\MAdmin\Log\Item\Standard. ( Ignorable by Annotation )

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

204
					/** @scrutinizer ignore-call */ 
205
     $facility = $item->getFacility();
Loading history...
205
206
					if( !isset( $contents[$facility] ) ) {
207
						$contents[$facility] = $container->create( $facility );
208
					}
209
210
					$contents[$facility]->add( $item->toArray() );
211
				}
212
213
				$ids[] = $id;
214
			}
215
216
			$manager->deleteItems( $ids );
217
218
			$count = count( $items );
219
			$start += $count;
220
			$search->setSlice( $start );
221
		}
222
		while( $count >= $search->getSliceSize() );
223
224
225
		if( $container !== null && !empty( $contents ) )
226
		{
227
			foreach( $contents as $content ) {
228
				$container->add( $content );
229
			}
230
231
			$container->close();
232
		}
233
	}
234
}
235