Standard   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 53
c 2
b 0
f 0
dl 0
loc 281
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
A send() 0 42 1
A view() 0 12 1
A sites() 0 15 4
A run() 0 39 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2025
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Customer\Email\Account;
12
13
14
/**
15
 * Customer account e-mail job 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/customer/email/account/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\Account\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Customer\Email\Account\Myaccount
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/customer/email/account/name = Myaccount
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 "MyAccount"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2016.04
55
	 */
56
57
	/** controller/jobs/customer/email/account/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the customer email account 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/account/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 2016.04
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/customer/email/account/decorators/global
79
	 * @see controller/jobs/customer/email/account/decorators/local
80
	 */
81
82
	/** controller/jobs/customer/email/account/decorators/global
83
	 * Adds a list of globally available decorators only to the customer email account 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/account/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 2016.04
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/customer/email/account/decorators/excludes
102
	 * @see controller/jobs/customer/email/account/decorators/local
103
	 */
104
105
	/** controller/jobs/customer/email/account/decorators/local
106
	 * Adds a list of local decorators only to the customer email account 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\Account\Decorator\*") around this job controller.
115
	 *
116
	 *  controller/jobs/customer/email/account/decorators/local = array( 'decorator2' )
117
	 *
118
	 * This would add the decorator named "decorator2" defined by
119
	 * "\Aimeos\Controller\Jobs\Customer\Email\Account\Decorator\Decorator2" only to this job
120
	 * controller.
121
	 *
122
	 * @param array List of decorator names
123
	 * @since 2016.04
124
	 * @see controller/jobs/common/decorators/default
125
	 * @see controller/jobs/customer/email/account/decorators/excludes
126
	 * @see controller/jobs/customer/email/account/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', 'Customer account 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 new customer accounts' );
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
		$context = $this->context();
166
		$queue = $context->queue( 'mq-email', 'customer/email/account' );
167
		$custManager = \Aimeos\MShop::create( $context, 'customer' );
168
169
		while( ( $msg = $queue->get() ) !== null )
170
		{
171
			try
172
			{
173
				if( ( $list = json_decode( $msg->getBody(), true ) ) === null )
174
				{
175
					$str = sprintf( 'Invalid JSON encode message: %1$s', $msg->getBody() );
176
					throw new \Aimeos\Controller\Jobs\Exception( $str );
177
				}
178
179
				$pass = $list['customer.password'] ?? null;
180
				$item = $custManager->create()->fromArray( $list, true );
181
				$sites = $this->sites( $item->getSiteId() );
182
183
				$address = $item->getPaymentAddress();
184
				$context->locale()->setLanguageId( $address->getLanguageId() ); // for translation
185
186
				$view = $this->view( $address, $sites->getTheme()->filter()->last() );
187
				$view->account = $item->getCode();
188
				$view->password = $pass;
189
190
				$this->send( $view, $address, $sites->getLogo()->filter()->last() );
191
192
				$str = sprintf( 'Sent customer account e-mail to "%1$s"', $address->getEmail() );
193
				$context->logger()->debug( $str, 'email/customer/account' );
194
			}
195
			catch( \Exception $e )
196
			{
197
				$str = 'Error while trying to send customer account e-mail: ' . $e->getMessage();
198
				$context->logger()->error( $str . PHP_EOL . $e->getTraceAsString(), 'email/customer/account' );
199
			}
200
201
			$queue->del( $msg );
202
		}
203
	}
204
205
206
	/**
207
	 * Sends the account creation e-mail to the e-mail address of the customer
208
	 *
209
	 * @param \Aimeos\Base\View\Iface $view View object
210
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
211
	 * @param string|null $logoPath Path to the logo
212
	 */
213
	protected function send( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $address, ?string $logoPath = null )
214
	{
215
		/** controller/jobs/customer/email/account/template-html
216
		 * Relative path to the template for the HTML part of the account emails.
217
		 *
218
		 * The template file contains the HTML code and processing instructions
219
		 * to generate the result shown in the body of the frontend. The
220
		 * configuration string is the path to the template file relative
221
		 * to the templates directory (usually in templates/controller/jobs).
222
		 * You can overwrite the template file configuration in extensions and
223
		 * provide alternative templates.
224
		 *
225
		 * @param string Relative path to the template
226
		 * @since 2022.04
227
		 * @see controller/jobs/customer/email/account/template-text
228
		 */
229
230
		/** controller/jobs/customer/email/account/template-text
231
		 * Relative path to the template for the text part of the account emails.
232
		 *
233
		 * The template file contains the text and processing instructions
234
		 * to generate the result shown in the body of the frontend. The
235
		 * configuration string is the path to the template file relative
236
		 * to the templates directory (usually in templates/controller/jobs).
237
		 * You can overwrite the template file configuration in extensions and
238
		 * provide alternative templates.
239
		 *
240
		 * @param string Relative path to the template
241
		 * @since 2022.04
242
		 * @see controller/jobs/customer/email/account/template-html
243
		 */
244
245
		$context = $this->context();
246
		$config = $context->config();
247
248
		$msg = $this->call( 'mailTo', $address );
249
		$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) );
250
251
		$msg->subject( $context->translate( 'controller/jobs', 'Your new account' ) )
252
			->html( $view->render( $config->get( 'controller/jobs/customer/email/account/template-html', 'customer/email/account/html' ) ) )
253
			->text( $view->render( $config->get( 'controller/jobs/customer/email/account/template-text', 'customer/email/account/text' ) ) )
254
			->send();
255
	}
256
257
258
	/**
259
	 * Returns the list of site items from the given site ID up to the root site
260
	 *
261
	 * @param string|null $siteId Site ID like "1.2.4."
262
	 * @return \Aimeos\Map List of site items
263
	 */
264
	protected function sites( ?string $siteId = null ) : \Aimeos\Map
265
	{
266
		if( !$siteId && !isset( $this->sites[''] ) ) {
267
			$this->sites[''] = map( \Aimeos\MShop::create( $this->context(), 'locale/site' )->find( 'default' ) );
268
		}
269
270
		if( !isset( $this->sites[(string) $siteId] ) )
271
		{
272
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
273
			$siteIds = explode( '.', trim( (string) $siteId, '.' ) );
274
275
			$this->sites[$siteId] = $manager->getPath( end( $siteIds ) );
276
		}
277
278
		return $this->sites[$siteId];
279
	}
280
281
282
	/**
283
	 * Returns the view populated with common data
284
	 *
285
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
286
	 * @param string|null $theme Theme name
287
	 * @return \Aimeos\Base\View\Iface View object
288
	 */
289
	protected function view( \Aimeos\MShop\Common\Item\Address\Iface $address, ?string $theme = null ) : \Aimeos\Base\View\Iface
290
	{
291
		$view = $this->call( 'mailView', $address->getLanguageId() );
292
		$view->intro = $this->call( 'mailIntro', $address );
293
		$view->css = $this->call( 'mailCss', $theme );
294
		$view->addressItem = $address;
295
		$view->urlparams = [
296
			'site' => $this->context()->locale()->getSiteItem()->getCode(),
297
			'locale' => $address->getLanguageId(),
298
		];
299
300
		return $view;
301
	}
302
}
303