Passed
Push — master ( 8261fc...d6c380 )
by Aimeos
05:31
created

Mail::mailTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9666
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs;
12
13
14
/**
15
 * Mail trait for job controllers
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
trait Mail
21
{
22
	/**
23
	 * Returns the context object
24
	 *
25
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
26
	 */
27
	abstract protected function context() : \Aimeos\MShop\Context\Item\Iface;
28
29
	/**
30
	 * Prepares and returns a new mail message
31
	 *
32
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Address item object
33
	 * @return \Aimeos\Base\Mail\Message\Iface Prepared mail message
34
	 */
35
	protected function mailTo( \Aimeos\MShop\Common\Item\Address\Iface $addr ) : \Aimeos\Base\Mail\Message\Iface
36
	{
37
		$context = $this->context();
38
		$config = $context->config();
39
40
		$message = $context->mail()->create()
41
			->header( 'X-MailGenerator', 'Aimeos' )
42
			->from( $config->get( 'resource/email/from-email' ), $config->get( 'resource/email/from-name' ) )
43
			->to( $addr->getEMail(), $addr->getFirstName() . ' ' . $addr->getLastName() );
44
45
		if( !empty( $email = $config->get( 'resource/email/bcc-email' ) ) ) {
46
			$message->bcc( $email );
47
		}
48
49
		return $message;
50
	}
51
52
53
	/**
54
	 * Returns the e-mail intro message
55
	 *
56
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Address item object
57
	 * @return string Intro message with salutation
58
	 */
59
	protected function mailIntro( \Aimeos\MShop\Common\Item\Address\Iface $addr ) : string
60
	{
61
		switch( $addr->getSalutation() )
62
		{
63
			case \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_UNKNOWN:
64
				/// E-mail intro with first name (%1$s) and last name (%2$s)
65
				return $this->context()->translate( 'client', 'Dear %1$s %2$s' );
66
			case \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR:
67
				/// E-mail intro with first name (%1$s) and last name (%2$s)
68
				return $this->context()->translate( 'client', 'Dear Mr %1$s %2$s' );
69
			case \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MS:
70
				/// E-mail intro with first name (%1$s) and last name (%2$s)
71
				return $this->context()->translate( 'client', 'Dear Ms %1$s %2$s' );
72
		}
73
74
		return $this->context()->translate( 'client', 'Dear customer' );
75
	}
76
77
78
	/**
79
	 * Returns the view for generating the mail message content
80
	 *
81
	 * @param string|null $langId Language ID the content should be generated for
82
	 * @return \Aimeos\MW\View\Iface View object
83
	 */
84
	protected function mailView( string $langId = null ) : \Aimeos\MW\View\Iface
85
	{
86
		$view = $this->context()->view();
87
88
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $this->context()->i18n( $langId ?: 'en' ) );
89
		$view->addHelper( 'translate', $helper );
90
91
		return $view;
92
	}
93
}
94