Standard::products()   B
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 13
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 26
rs 8.4444
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 Customer
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Customer\Email\Watch;
12
13
14
/**
15
 * Product notification e-mail job controller.
16
 *
17
 * @package Controller
18
 * @subpackage Customer
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/** controller/jobs/customer/email/watch/name
25
	 * Class name of the used product notification e-mail 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\Customer\Email\Watch\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Customer\Email\Watch\Mywatch
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/customer/email/watch/name = Mywatch
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 "MyWatch"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 */
56
57
	/** controller/jobs/customer/email/watch/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the customer email watch 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/customer/email/watch/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 2015.09
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/customer/email/watch/decorators/global
79
	 * @see controller/jobs/customer/email/watch/decorators/local
80
	 */
81
82
	/** controller/jobs/customer/email/watch/decorators/global
83
	 * Adds a list of globally available decorators only to the customer email watch 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/customer/email/watch/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 2015.09
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/customer/email/watch/decorators/excludes
102
	 * @see controller/jobs/customer/email/watch/decorators/local
103
	 */
104
105
	/** controller/jobs/customer/email/watch/decorators/local
106
	 * Adds a list of local decorators only to the customer email watch 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\Customer\Email\Watch\Decorator\*") around this job controller.
115
	 *
116
	 *  controller/jobs/customer/email/watch/decorators/local = array( 'decorator2' )
117
	 *
118
	 * This would add the decorator named "decorator2" defined by
119
	 * "\Aimeos\Controller\Jobs\Customer\Email\Watch\Decorator\Decorator2" only to this job
120
	 * controller.
121
	 *
122
	 * @param array List of decorator names
123
	 * @since 2015.09
124
	 * @see controller/jobs/common/decorators/default
125
	 * @see controller/jobs/customer/email/watch/decorators/excludes
126
	 * @see controller/jobs/customer/email/watch/decorators/global
127
	 */
128
129
130
	use \Aimeos\Controller\Jobs\Mail;
131
132
133
	private array $sites = [];
134
135
136
	/**
137
	 * Returns the localized name of the job.
138
	 *
139
	 * @return string Name of the job
140
	 */
141
	public function getName() : string
142
	{
143
		return $this->context()->translate( 'controller/jobs', 'Product notification e-mails' );
144
	}
145
146
147
	/**
148
	 * Returns the localized description of the job.
149
	 *
150
	 * @return string Description of the job
151
	 */
152
	public function getDescription() : string
153
	{
154
		return $this->context()->translate( 'controller/jobs', 'Sends e-mails for watched products' );
155
	}
156
157
158
	/**
159
	 * Executes the job.
160
	 *
161
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
162
	 */
163
	public function run()
164
	{
165
		$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
166
167
		$search = $manager->filter( true );
168
		$func = $search->make( 'customer:has', ['product', 'watch'] );
169
		$search->add( $search->is( $func, '!=', null ) )->order( 'customer.id' );
170
171
		$start = 0;
172
173
		do
174
		{
175
			$customers = $manager->search( $search->slice( $start ), ['product' => ['watch'], 'price'] );
176
			$customers = $this->notify( $customers );
177
			$customers = $manager->save( $customers );
178
179
			$count = count( $customers );
0 ignored issues
show
Bug introduced by
It seems like $customers can also be of type Aimeos\MShop\Common\Item\Iface; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

179
			$count = count( /** @scrutinizer ignore-type */ $customers );
Loading history...
180
			$start += $count;
181
		}
182
		while( $count >= $search->getLimit() );
183
	}
184
185
186
	/**
187
	 * Sends product notifications for the given customers in their language
188
	 *
189
	 * @param \Aimeos\Map $customers List of customer items implementing \Aimeos\MShop\Customer\Item\Iface
190
	 * @return \Aimeos\Map List of customer items implementing \Aimeos\MShop\Customer\Item\Iface
191
	 */
192
	protected function notify( \Aimeos\Map $customers ) : \Aimeos\Map
193
	{
194
		$date = date( 'Y-m-d H:i:s' );
195
		$context = $this->context();
196
197
198
		foreach( $customers as $customer )
199
		{
200
			$listItems = $customer->getListItems( 'product', null, null, false );
201
			$products = $this->products( $listItems );
202
203
			try
204
			{
205
				if( !empty( $products ) )
206
				{
207
					$sites = $this->sites( $customer->getSiteId() );
208
					$context->locale()->setLanguageId( $customer->getPaymentAddress()->getLanguageId() );
209
210
					$view = $this->view( $customer->getPaymentAddress(), $sites->getTheme()->filter()->last() );
211
					$view->products = $products;
212
213
					$this->send( $view, $customer->getPaymentAddress(), $sites->getLogo()->filter()->last() );
214
				}
215
216
				$str = sprintf( 'Sent product notification e-mail to "%1$s"', $customer->getPaymentAddress()->getEmail() );
217
				$context->logger()->debug( $str, 'email/customer/watch' );
218
			}
219
			catch( \Exception $e )
220
			{
221
				$str = 'Error while trying to send product notification e-mail for customer ID "%1$s": %2$s';
222
				$msg = sprintf( $str, $customer->getId(), $e->getMessage() ) . PHP_EOL . $e->getTraceAsString();
223
				$context->logger()->error( $msg, 'email/customer/watch' );
224
			}
225
226
			$remove = $listItems->diffKeys( $products )->filter( function( $listItem ) use ( $date ) {
227
				return $listItem->getDateEnd() < $date;
228
			} );
229
230
			$customer->deleteListItems( $remove );
231
		}
232
233
		return $customers;
234
	}
235
236
237
	/**
238
	 * Returns a filtered list of products for which a notification should be sent
239
	 *
240
	 * @param \Aimeos\Map $listItems List of customer list items
241
	 * @return array Associative list of list IDs as key and product items values
242
	 */
243
	protected function products( \Aimeos\Map $listItems ) : array
244
	{
245
		$result = [];
246
		$priceManager = \Aimeos\MShop::create( $this->context(), 'price' );
247
248
		foreach( $listItems as $id => $listItem )
249
		{
250
			try
251
			{
252
				if( $product = $listItem->getRefItem() )
253
				{
254
					$config = $listItem->getConfig();
255
					$prices = $product->getRefItems( 'price', 'default', 'default' );
256
					$price = $priceManager->getLowestPrice( $prices, 1, $config['currency'] ?? null );
257
258
					if( ( $config['stock'] ?? null ) && $product->inStock()
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($config['stock'] ?? nul... 0 > $price->getValue(), Probably Intended Meaning: $config['stock'] ?? null...0 > $price->getValue())
Loading history...
259
						|| ( $config['price'] ?? null ) && ( $config['pricevalue'] ?? 0 ) > $price->getValue()
260
					) {
261
						$result[$id] = $product->set( 'price', $price );
262
					}
263
				}
264
			}
265
			catch( \Exception $e ) { ; } // no price available
266
		}
267
268
		return $result;
269
	}
270
271
272
	/**
273
	 * Sends the notification e-mail for the given customer address and products
274
	 *
275
	 * @param \Aimeos\Base\View\Iface $view View object
276
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
277
	 * @param string|null $logoPath Path to the logo
278
	 */
279
	protected function send( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $address, ?string $logoPath = null )
280
	{
281
		/** controller/jobs/customer/email/watch/template-html
282
		 * Relative path to the template for the HTML part of the watch emails.
283
		 *
284
		 * The template file contains the HTML code and processing instructions
285
		 * to generate the result shown in the body of the frontend. The
286
		 * configuration string is the path to the template file relative
287
		 * to the templates directory (usually in templates/controller/jobs).
288
		 * You can overwrite the template file configuration in extensions and
289
		 * provide alternative templates.
290
		 *
291
		 * @param string Relative path to the template
292
		 * @since 2022.04
293
		 * @see controller/jobs/customer/email/watch/template-text
294
		 */
295
296
		/** controller/jobs/customer/email/watch/template-text
297
		 * Relative path to the template for the text part of the watch emails.
298
		 *
299
		 * The template file contains the text and processing instructions
300
		 * to generate the result shown in the body of the frontend. The
301
		 * configuration string is the path to the template file relative
302
		 * to the templates directory (usually in templates/controller/jobs).
303
		 * You can overwrite the template file configuration in extensions and
304
		 * provide alternative templates.
305
		 *
306
		 * @param string Relative path to the template
307
		 * @since 2022.04
308
		 * @see controller/jobs/customer/email/watch/template-html
309
		 */
310
311
		$context = $this->context();
312
		$config = $context->config();
313
314
		$msg = $this->call( 'mailTo', $address );
315
		$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) );
316
317
		$msg->subject( $context->translate( 'client', 'Your watched products' ) )
318
			->html( $view->render( $config->get( 'controller/jobs/customer/email/watch/template-html', 'customer/email/watch/html' ) ) )
319
			->text( $view->render( $config->get( 'controller/jobs/customer/email/watch/template-text', 'customer/email/watch/text' ) ) )
320
			->send();
321
	}
322
323
324
	/**
325
	 * Returns the list of site items from the given site ID up to the root site
326
	 *
327
	 * @param string|null $siteId Site ID like "1.2.4."
328
	 * @return \Aimeos\Map List of site items
329
	 */
330
	protected function sites( ?string $siteId = null ) : \Aimeos\Map
331
	{
332
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
333
334
		if( !$siteId && !isset( $this->sites[''] ) )
335
		{
336
			$default = $this->context()->config()->get( 'mshop/locale/site', 'default' );
337
			$this->sites[''] = map( $manager->find( $default ) );
338
		}
339
340
		if( !isset( $this->sites[(string) $siteId] ) )
341
		{
342
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
343
			$siteIds = explode( '.', trim( (string) $siteId, '.' ) );
344
345
			$this->sites[$siteId] = $manager->getPath( end( $siteIds ) );
346
		}
347
348
		return $this->sites[$siteId];
349
	}
350
351
352
	/**
353
	 * Returns the view populated with common data
354
	 *
355
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
356
	 * @param string|null $theme Theme name
357
	 * @return \Aimeos\Base\View\Iface View object
358
	 */
359
	protected function view( \Aimeos\MShop\Common\Item\Address\Iface $address, ?string $theme = null ) : \Aimeos\Base\View\Iface
360
	{
361
		$view = $this->call( 'mailView', $address->getLanguageId() );
362
		$view->intro = $this->call( 'mailIntro', $address );
363
		$view->css = $this->call( 'mailCss', $theme );
364
		$view->urlparams = [
365
			'site' => $this->context()->locale()->getSiteItem()->getCode(),
366
			'locale' => $address->getLanguageId(),
367
		];
368
369
		return $view;
370
	}
371
}
372