Passed
Push — master ( 0fb12b...96f712 )
by Aimeos
10:47
created

Standard::sites()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
rs 10
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
	/**
28
	 * Returns the localized name of the job.
29
	 *
30
	 * @return string Name of the job
31
	 */
32
	public function getName() : string
33
	{
34
		return $this->context()->translate( '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() : string
44
	{
45
		return $this->context()->translate( '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->context();
57
		$queue = $context->queue( 'mq-email', 'customer/email/account' );
58
		$custManager = \Aimeos\MShop::create( $context, 'customer' );
59
60
		while( ( $msg = $queue->get() ) !== null )
61
		{
62
			try
63
			{
64
				if( ( $list = json_decode( $msg->getBody(), true ) ) === null )
65
				{
66
					$str = sprintf( 'Invalid JSON encode message: %1$s', $msg->getBody() );
67
					throw new \Aimeos\Controller\Jobs\Exception( $str );
68
				}
69
70
				$password = $list['customer.password'] ?? null;
71
				$item = $custManager->create()->fromArray( $list, true );
72
				$sites = $this->sites( $item->getSiteId() );
73
74
				$view = $this->view( $item->getPaymentAddress(), $sites->getTheme()->filter()->last() );
75
				$view->account = $item->getCode();
76
				$view->password = $password;
77
78
				$this->send( $view, $item->getPaymentAddress(), $sites->getLogo()->filter()->last() );
79
80
				$str = sprintf( 'Sent customer account e-mail to "%1$s"', $item->getPaymentAddress()->getEmail() );
81
				$context->logger()->debug( $str, 'email/customer/account' );
82
			}
83
			catch( \Exception $e )
84
			{
85
				$str = 'Error while trying to send customer account e-mail: ' . $e->getMessage();
86
				$context->logger()->error( $str . PHP_EOL . $e->getTraceAsString(), 'email/customer/account' );
87
			}
88
89
			$queue->del( $msg );
90
		}
91
	}
92
93
94
	/**
95
	 * Sends the account creation e-mail to the e-mail address of the customer
96
	 *
97
	 * @param \Aimeos\MW\View\Iface $view View object
98
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
99
	 * @param string|null $logoPath Path to the logo
100
	 */
101
	protected function send( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $address, string $logoPath = null )
102
	{
103
		$context = $this->context();
104
		$config = $context->config();
105
106
		$msg = $this->call( 'mailTo', $address );
107
		$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) );
108
109
		$msg->subject( $context->translate( 'client', 'Your new account' ) )
110
			->html( $view->render( $config->get( 'controller/jobs/customer/email/account/template-html', 'customer/email/account/html' ) ) )
111
			->text( $view->render( $config->get( 'controller/jobs/customer/email/account/template-text', 'customer/email/account/text' ) ) )
112
			->send();
113
	}
114
115
116
	/**
117
	 * Returns the list of site items from the given site ID up to the root site
118
	 *
119
	 * @param string|null $siteId Site ID like "1.2.4."
120
	 * @return \Aimeos\Map List of site items
121
	 */
122
	protected function sites( string $siteId = null ) : \Aimeos\Map
123
	{
124
		if( !$siteId && !isset( $this->sites[''] ) ) {
125
			$this->sites[''] = map( \Aimeos\MShop::create( $this->context(), 'locale/site' )->find( 'default' ) );
0 ignored issues
show
Bug Best Practice introduced by
The property sites does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
126
		}
127
128
		if( !isset( $this->sites[(string) $siteId] ) )
129
		{
130
			$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
131
			$siteIds = explode( '.', trim( $siteId, '.' ) );
0 ignored issues
show
Bug introduced by
It seems like $siteId can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
			$siteIds = explode( '.', trim( /** @scrutinizer ignore-type */ $siteId, '.' ) );
Loading history...
132
133
			$this->sites[$siteId] = $manager->getPath( end( $siteIds ) );
134
		}
135
136
		return $this->sites[$siteId];
137
	}
138
139
140
	/**
141
	 * Returns the view populated with common data
142
	 *
143
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Address item
144
	 * @param string|null $theme Theme name
145
	 * @return \Aimeos\MW\View\Iface View object
146
	 */
147
	protected function view( \Aimeos\MShop\Common\Item\Address\Iface $address, string $theme = null ) : \Aimeos\MW\View\Iface
148
	{
149
		$view = $this->call( 'mailView', $address->getLanguageId() );
150
		$view->intro = $this->call( 'mailIntro', $address );
151
		$view->css = $this->call( 'mailCss', $theme );
152
		$view->addressItem = $address;
153
		$view->urlparams = [
154
			'site' => $this->context()->locale()->getSiteItem()->getCode(),
155
			'locale' => $address->getLanguageId(),
156
		];
157
158
		return $view;
159
	}
160
}
161