Passed
Push — master ( d008ba...e8d8c6 )
by Aimeos
03:59
created

Standard::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 2
dl 0
loc 17
rs 9.8333
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), 2016-2022
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
	use \Aimeos\Controller\Jobs\Mail;
25
26
27
	private $client;
0 ignored issues
show
introduced by
The private property $client is not used, and could be removed.
Loading history...
28
29
30
	/**
31
	 * Returns the localized name of the job.
32
	 *
33
	 * @return string Name of the job
34
	 */
35
	public function getName() : string
36
	{
37
		return $this->context()->translate( 'controller/jobs', 'Customer account e-mails' );
38
	}
39
40
41
	/**
42
	 * Returns the localized description of the job.
43
	 *
44
	 * @return string Description of the job
45
	 */
46
	public function getDescription() : string
47
	{
48
		return $this->context()->translate( 'controller/jobs', 'Sends e-mails for new customer accounts' );
49
	}
50
51
52
	/**
53
	 * Executes the job.
54
	 *
55
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
56
	 */
57
	public function run()
58
	{
59
		$context = $this->context();
60
		$queue = $context->queue( 'mq-email', 'customer/email/account' );
61
		$custManager = \Aimeos\MShop::create( $context, 'customer' );
62
63
		while( ( $msg = $queue->get() ) !== null )
64
		{
65
			try
66
			{
67
				if( ( $list = json_decode( $msg->getBody(), true ) ) === null )
68
				{
69
					$str = sprintf( 'Invalid JSON encode message: %1$s', $msg->getBody() );
70
					throw new \Aimeos\Controller\Jobs\Exception( $str );
71
				}
72
73
				$password = $list['customer.password'] ?? null;
74
				$item = $custManager->create()->fromArray( $list, true );
75
76
				$this->send( $item, $password );
77
78
				$str = sprintf( 'Sent customer account e-mail to "%1$s"', $item->getPaymentAddress()->getEmail() );
79
				$context->logger()->debug( $str, 'email/customer/account' );
80
			}
81
			catch( \Exception $e )
82
			{
83
				$str = 'Error while trying to send customer account e-mail: ' . $e->getMessage();
84
				$context->logger()->error( $str . PHP_EOL . $e->getTraceAsString(), 'email/customer/account' );
85
			}
86
87
			$queue->del( $msg );
88
		}
89
	}
90
91
92
	/**
93
	 * Sends the account creation e-mail to the e-mail address of the customer
94
	 *
95
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
96
	 * @param string|null $password Customer clear text password
97
	 */
98
	protected function send( \Aimeos\MShop\Customer\Item\Iface $item, string $password = null )
99
	{
100
		$context = $this->context();
101
		$config = $context->config();
102
		$address = $item->getPaymentAddress();
103
104
		$view = $this->call( 'mailView', $address->getLanguageId() );
105
		$view->intro = $this->call( 'mailIntro', $address );
106
		$view->account = $item->getCode();
107
		$view->password = $password;
108
		$view->addressItem = $address;
109
110
		return $this->call( 'mailTo', $address )
111
			->subject( $context->translate( 'client', 'Your new account' ) )
112
			->html( $view->render( $config->get( 'controller/jobs/customer/email/account/template-html', 'customer/email/account/html' ) ) )
113
			->text( $view->render( $config->get( 'controller/jobs/customer/email/account/template-text', 'customer/email/account/text' ) ) )
114
			->send();
115
	}
116
}
117