Completed
Push — master ( 33a95e...daccbb )
by Aimeos
09:31
created

Standard::sendEmail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 17
nc 1
nop 2
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
				$item = $custManager->createItem();
65
				$item->fromArray( $list );
66
67
				$this->sendEmail( $context, $item );
68
			}
69
			else
70
			{
71
				$context->getLogger()->log( sprintf( 'Invalid JSON encode message: %1$s', $msg->getBody() ) );
72
			}
73
74
			$queue->del( $msg );
75
		}
76
	}
77
78
79
	/**
80
	 * Returns the product notification e-mail client
81
	 *
82
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
83
	 * @return \Aimeos\Client\Html\Iface Product notification e-mail client
84
	 */
85
	protected function getClient( \Aimeos\MShop\Context\Item\Iface $context )
86
	{
87
		if( !isset( $this->client ) )
88
		{
89
			$templatePaths = $this->getAimeos()->getCustomPaths( 'client/html' );
90
			$this->client = \Aimeos\Client\Html\Email\Account\Factory::createClient( $context, $templatePaths );
91
		}
92
93
		return $this->client;
94
	}
95
96
97
	/**
98
	 * Sends the account creation e-mail to the e-mail address of the customer
99
	 *
100
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
101
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
102
	 */
103
	protected function sendEmail( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $item )
104
	{
105
		$address = $item->getPaymentAddress();
106
107
		$view = $context->getView();
108
		$view->extAddressItem = $address;
109
		$view->extAccountCode = $item->getCode();
110
		$view->extAccountPassword = $item->getPassword();
111
112
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $context->getI18n( $address->getLanguageId() ) );
113
		$view->addHelper( 'translate', $helper );
114
115
		$mailer = $context->getMail();
116
		$message = $mailer->createMessage();
117
118
		$helper = new \Aimeos\MW\View\Helper\Mail\Standard( $view, $message );
119
		$view->addHelper( 'mail', $helper );
120
121
		$client = $this->getClient( $context );
122
		$client->setView( $view );
123
		$client->getHeader();
124
		$client->getBody();
125
126
		$mailer->send( $message );
127
	}
128
}
129