Passed
Push — master ( 219e21...0cbac8 )
by Aimeos
03:14
created

Standard::closeContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 30
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), 2015-2023
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Product\Export;
12
13
14
/**
15
 * Job controller for product exports.
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/product/export/name
25
	 * Class name of the used product 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\Product\Export\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Product\Export\Myalgorithm
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/product/export/name = Myalgorithm
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 "MyOptimizer"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2015.01
55
	 */
56
57
	/** controller/jobs/product/export/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the product export 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/product/export/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 2015.01
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/product/export/decorators/global
79
	 * @see controller/jobs/product/export/decorators/local
80
	 */
81
82
	/** controller/jobs/product/export/decorators/global
83
	 * Adds a list of globally available decorators only to the product export 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/product/export/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 2015.01
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/product/export/decorators/excludes
102
	 * @see controller/jobs/product/export/decorators/local
103
	 */
104
105
	/** controller/jobs/product/export/decorators/local
106
	 * Adds a list of local decorators only to the product export 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\Product\Export\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/product/export/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Product\Export\Decorator\Decorator2"
121
	 * only to the job controller.
122
	 *
123
	 * @param array List of decorator names
124
	 * @since 2015.01
125
	 * @see controller/jobs/common/decorators/default
126
	 * @see controller/jobs/product/export/decorators/excludes
127
	 * @see controller/jobs/product/export/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', 'Product export' );
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', 'Exports all available products' );
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
		$this->export( $container, false );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $container seems to be never defined.
Loading history...
Unused Code introduced by
The call to Aimeos\Controller\Jobs\P...port\Standard::export() has too many arguments starting with false. ( Ignorable by Annotation )

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

160
		$this->/** @scrutinizer ignore-call */ 
161
         export( $container, false );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
161
	}
162
163
164
	/**
165
	 * Exports the products
166
	 *
167
	 * @param bool|null $default True to filter exported products by default criteria
168
	 * @return array List of file names
169
	 */
170
	protected function export( ?bool $default = true ) : array
171
	{
172
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
173
		$filter = $manager->filter( $default )->order( 'product.id' )->slice( 0, $this->max() );
174
		$cursor = $manager->cursor( $filter );
175
176
		$domains = $this->domains();
177
		$fs = $this->fs();
178
179
		$filenum = 1;
180
		$files = [];
181
182
		while( $items = $manager->iterate( $cursor, $domains ) )
183
		{
184
			$filename = $this->call( 'filename', $filenum++ );
185
			$fs->write( $filename, $this->render( $items ) );
186
			$files[] = $filename;
187
		}
188
189
		return $files;
190
	}
191
192
193
	/**
194
	 * Returns the domain names whose items should be exported too
195
	 *
196
	 * @return array List of domain names
197
	 */
198
	protected function domains() : array
199
	{
200
		/** controller/jobs/product/export/domains
201
		 * List of associated items from other domains that should be exported too
202
		 *
203
		 * Products consist not only of the base data but also of texts, media
204
		 * files, prices, attrbutes and other details. Those information is
205
		 * associated to the products via their lists. Using the "domains" option
206
		 * you can make more or less associated items available in the template.
207
		 *
208
		 * @param array List of domain names
209
		 * @since 2015.01
210
		 * @see controller/jobs/product/export/filename
211
		 * @see controller/jobs/product/export/max-items
212
		 */
213
		$default = ['attribute', 'media', 'price', 'product', 'text'];
214
215
		return $this->context()->config()->get( 'controller/jobs/product/export/domains', $default );
216
	}
217
218
219
	/**
220
	 * Returns the file name for the new content file
221
	 *
222
	 * @param int $number Current file number
223
	 * @return string New file name
224
	 */
225
	protected function filename( int $number ) : string
226
	{
227
		/** controller/jobs/product/export/filename
228
		 * Template for the generated file names
229
		 *
230
		 * The generated export files will be named according to the given
231
		 * string which can contain two place holders: The number of the
232
		 * exported product and the ISO date/time when the file was created.
233
		 *
234
		 * @param string File name template
235
		 * @since 2018.04
236
		 * @see controller/jobs/product/export/max-items
237
		 * @see controller/jobs/product/export/domains
238
		 */
239
		$name = $this->context()->config()->get( 'controller/jobs/product/export/filename', 'aimeos-products-%1$d_%2$s.xml' );
240
241
		return sprintf( $name, $number, date( 'Y-m-d_H:i:s' ) );
242
	}
243
244
245
	/**
246
	 * Returns the file system for storing the exported files
247
	 *
248
	 * @return \Aimeos\Base\Filesystem\Iface File system to store files to
249
	 */
250
	protected function fs() : \Aimeos\Base\Filesystem\Iface
251
	{
252
		return $this->context()->fs( 'fs-export' );
253
	}
254
255
256
	/**
257
	 * Returns the maximum number of exported products per file
258
	 *
259
	 * @return int Maximum number of exported products per file
260
	 */
261
	protected function max() : int
262
	{
263
		/** controller/jobs/product/export/max-items
264
		 * Maximum number of exported products per file
265
		 *
266
		 * Limits the number of exported products per file as the memory
267
		 * consumption of processing big files is rather high. Splitting
268
		 * the data into several files that can also be processed in
269
		 * parallel is able to speed up importing the files again.
270
		 *
271
		 * @param integer Number of products entries per file
272
		 * @since 2015.01
273
		 * @see controller/jobs/product/export/filename
274
		 * @see controller/jobs/product/export/domains
275
		 */
276
		return $this->context()->config()->get( 'controller/jobs/product/export/max-items', 10000 );
277
	}
278
279
280
	/**
281
	 * Renders the output for the given items
282
	 *
283
	 * @param \Aimeos\Map $items List of product items implementing \Aimeos\MShop\Product\Item\Iface
284
	 * @return string Rendered content
285
	 */
286
	protected function render( \Aimeos\Map $items ) : string
287
	{
288
		/** controller/jobs/product/export/template-items
289
		 * Relative path to the XML items template of the product site map job controller.
290
		 *
291
		 * The template file contains the XML code and processing instructions
292
		 * to generate the site map files. The configuration string is the path
293
		 * to the template file relative to the templates directory (usually in
294
		 * templates/controller/jobs).
295
		 *
296
		 * You can overwrite the template file configuration in extensions and
297
		 * provide alternative templates. These alternative templates should be
298
		 * named like the default one but with the string "standard" replaced by
299
		 * an unique name. You may use the name of your project for this. If
300
		 * you've implemented an alternative client class as well, "standard"
301
		 * should be replaced by the name of the new class.
302
		 *
303
		 * @param string Relative path to the template creating XML code for the site map items
304
		 * @since 2015.01
305
		 * @see controller/jobs/product/export/domains
306
		 * @see controller/jobs/product/export/filename
307
		 * @see controller/jobs/product/export/max-items
308
		 */
309
		$tplconf = 'controller/jobs/product/export/template-items';
310
		$default = 'product/export/items-body-standard';
311
312
		$context = $this->context();
313
		$view = $context->view();
314
315
		$view->exportItems = $items;
316
317
		return $view->render( $context->config()->get( $tplconf, $default ) );
318
	}
319
}
320