Passed
Push — master ( 23e72e...dc62e4 )
by Aimeos
03:22
created

Standard   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A send() 0 12 1
A view() 0 12 1
A getDescription() 0 3 1
A sites() 0 15 4
A run() 0 36 4
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 $sites = [];
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
				$sites = $this->sites( $item->getSiteId() );
76
77
				$view = $this->view( $item->getPaymentAddress(), $sites->getTheme()->filter()->last() );
78
				$view->account = $item->getCode();
79
				$view->password = $password;
80
81
				$this->send( $view, $item->getPaymentAddress(), $sites->getLogo()->filter()->last() );
82
83
				$str = sprintf( 'Sent customer account e-mail to "%1$s"', $item->getPaymentAddress()->getEmail() );
84
				$context->logger()->debug( $str, 'email/customer/account' );
85
			}
86
			catch( \Exception $e )
87
			{
88
				$str = 'Error while trying to send customer account e-mail: ' . $e->getMessage();
89
				$context->logger()->error( $str . PHP_EOL . $e->getTraceAsString(), 'email/customer/account' );
90
			}
91
92
			$queue->del( $msg );
93
		}
94
	}
95
96
97
	/**
98
	 * Sends the account creation e-mail to the e-mail address of the customer
99
	 *
100
	 * @param \Aimeos\MW\View\Iface $view View object
101
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
102
	 * @param string|null $logoPath Path to the logo
103
	 */
104
	protected function send( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $address, string $logoPath = null )
105
	{
106
		$context = $this->context();
107
		$config = $context->config();
108
109
		$msg = $this->call( 'mailTo', $address );
110
		$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) );
111
112
		$msg->subject( $context->translate( 'client', 'Your new account' ) )
113
			->html( $view->render( $config->get( 'controller/jobs/customer/email/account/template-html', 'customer/email/account/html' ) ) )
114
			->text( $view->render( $config->get( 'controller/jobs/customer/email/account/template-text', 'customer/email/account/text' ) ) )
115
			->send();
116
	}
117
118
119
	/**
120
	 * Returns the list of site items from the given site ID up to the root site
121
	 *
122
	 * @param string|null $siteId Site ID like "1.2.4."
123
	 * @return \Aimeos\Map List of site items
124
	 */
125
	protected function sites( string $siteId = null ) : \Aimeos\Map
126
	{
127
		if( !$siteId && !isset( $this->sites[''] ) ) {
128
			$this->sites[''] = map( \Aimeos\MShop::create( $this->context(), 'locale/site' )->find( 'default' ) );
129
		}
130
131
		if( !isset( $this->sites[(string) $siteId] ) )
132
		{
133
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
134
			$siteIds = explode( '.', trim( (string) $siteId, '.' ) );
135
136
			$this->sites[$siteId] = $manager->getPath( end( $siteIds ) );
137
		}
138
139
		return $this->sites[$siteId];
140
	}
141
142
143
	/**
144
	 * Returns the view populated with common data
145
	 *
146
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
147
	 * @param string|null $theme Theme name
148
	 * @return \Aimeos\MW\View\Iface View object
149
	 */
150
	protected function view( \Aimeos\MShop\Common\Item\Address\Iface $address, string $theme = null ) : \Aimeos\MW\View\Iface
151
	{
152
		$view = $this->call( 'mailView', $address->getLanguageId() );
153
		$view->intro = $this->call( 'mailIntro', $address );
154
		$view->css = $this->call( 'mailCss', $theme );
155
		$view->addressItem = $address;
156
		$view->urlparams = [
157
			'site' => $this->context()->locale()->getSiteItem()->getCode(),
158
			'locale' => $address->getLanguageId(),
159
		];
160
161
		return $view;
162
	}
163
}
164