Completed
Push — master ( cc87a0...64b84e )
by Aimeos
07:07
created

Standard::run()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 13
nc 4
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
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
	private $client;
25
26
27
	/**
28
	 * Returns the localized name of the job.
29
	 *
30
	 * @return string Name of the job
31
	 */
32
	public function getName()
33
	{
34
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Customer account e-mails' );
35
	}
36
37
38
	/**
39
	 * Returns the localized description of the job.
40
	 *
41
	 * @return string Description of the job
42
	 */
43
	public function getDescription()
44
	{
45
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Sends e-mails for new customer accounts' );
46
	}
47
48
49
	/**
50
	 * Executes the job.
51
	 *
52
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
53
	 */
54
	public function run()
55
	{
56
		$context = $this->getContext();
57
		$queue = $context->getMessageQueue( 'mq-email', 'customer/email/account' );
58
		$custManager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
59
60
		while( ( $msg = $queue->get() ) !== null )
61
		{
62
			if( ( $list = json_decode( $msg->getBody(), true ) ) !== null )
63
			{
64
				$password = ( isset( $list['customer.password'] ) ? $list['customer.password'] : '' );
65
				$item = $custManager->createItem();
66
				$item->fromArray( $list );
67
68
				$this->sendEmail( $context, $item, $password );
69
			}
70
			else
71
			{
72
				$context->getLogger()->log( sprintf( 'Invalid JSON encode message: %1$s', $msg->getBody() ) );
73
			}
74
75
			$queue->del( $msg );
76
		}
77
	}
78
79
80
	/**
81
	 * Returns the product notification e-mail client
82
	 *
83
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
84
	 * @return \Aimeos\Client\Html\Iface Product notification e-mail client
85
	 */
86
	protected function getClient( \Aimeos\MShop\Context\Item\Iface $context )
87
	{
88
		if( !isset( $this->client ) )
89
		{
90
			$templatePaths = $this->getAimeos()->getCustomPaths( 'client/html' );
91
			$this->client = \Aimeos\Client\Html\Email\Account\Factory::createClient( $context, $templatePaths );
92
		}
93
94
		return $this->client;
95
	}
96
97
98
	/**
99
	 * Sends the account creation e-mail to the e-mail address of the customer
100
	 *
101
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
102
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
103
	 * @param string $password Customer clear text password
104
	 */
105
	protected function sendEmail( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $item, $password )
106
	{
107
		$address = $item->getPaymentAddress();
108
109
		$view = $context->getView();
110
		$view->extAddressItem = $address;
111
		$view->extAccountCode = $item->getCode();
112
		$view->extAccountPassword = $password;
113
114
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $context->getI18n( $address->getLanguageId() ) );
115
		$view->addHelper( 'translate', $helper );
116
117
		$mailer = $context->getMail();
118
		$message = $mailer->createMessage();
119
120
		$helper = new \Aimeos\MW\View\Helper\Mail\Standard( $view, $message );
121
		$view->addHelper( 'mail', $helper );
122
123
		$client = $this->getClient( $context );
124
		$client->setView( $view );
125
		$client->getHeader();
126
		$client->getBody();
127
128
		$mailer->send( $message );
129
	}
130
}
131