Passed
Push — master ( 7e5570...a35432 )
by Aimeos
04:19
created

Standard::hydrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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