Mail   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 32
c 4
b 0
f 0
dl 0
loc 107
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A mailCss() 0 6 3
A mailLogo() 0 4 3
A mailIntro() 0 18 4
A mailView() 0 11 2
A mailTo() 0 16 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022-2025
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\ContextIface Context object
26
	 */
27
	abstract protected function context() : \Aimeos\MShop\ContextIface;
28
29
30
	/**
31
	 * Returns the CSS rules for the given theme
32
	 *
33
	 * @param string|null $theme Theme name
34
	 * @return string|null CSS rules
35
	 */
36
	protected function mailCss( ?string $theme ) : ?string
37
	{
38
		$theme = $theme ?: 'default';
39
		$fs = $this->context()->fs( 'fs-theme' );
40
41
		return $fs->has( $theme . '/email.css' ) ? $fs->read( $theme . '/email.css' ) : null;
42
	}
43
44
45
	/**
46
	 * Returns the e-mail intro message
47
	 *
48
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Address item object
49
	 * @return string Intro message with salutation
50
	 */
51
	protected function mailIntro( \Aimeos\MShop\Common\Item\Address\Iface $addr ) : string
52
	{
53
		switch( $addr->getSalutation() )
54
		{
55
			case '':
56
				/// E-mail intro with first name (%1$s) and last name (%2$s)
57
				$msg = $this->context()->translate( 'controller/jobs', 'Dear %1$s %2$s' ); break;
58
			case 'mr':
59
				/// E-mail intro with first name (%1$s) and last name (%2$s)
60
				$msg = $this->context()->translate( 'controller/jobs', 'Dear Mr %1$s %2$s' ); break;
61
			case 'ms':
62
				/// E-mail intro with first name (%1$s) and last name (%2$s)
63
				$msg = $this->context()->translate( 'controller/jobs', 'Dear Ms %1$s %2$s' ); break;
64
			default:
65
				$msg = $this->context()->translate( 'controller/jobs', 'Dear customer' );
66
		}
67
68
		return sprintf( $msg, $addr->getFirstName(), $addr->getLastName() );
69
	}
70
71
72
	/**
73
	 * Returns the logo for the given path
74
	 *
75
	 * @param string|null $path Logo path relative to fs-media file system
76
	 * @return string Binary logo data
77
	 */
78
	protected function mailLogo( ?string $path ) : string
79
	{
80
		$fs = $this->context()->fs( 'fs-media' );
81
		return $path && $fs->has( $path ) ? $fs->read( $path ) : '';
82
	}
83
84
85
	/**
86
	 * Prepares and returns a new mail message
87
	 *
88
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Address item object
89
	 * @return \Aimeos\Base\Mail\Message\Iface Prepared mail message
90
	 */
91
	protected function mailTo( \Aimeos\MShop\Common\Item\Address\Iface $addr ) : \Aimeos\Base\Mail\Message\Iface
92
	{
93
		$context = $this->context();
94
		$config = $context->config();
95
96
		$mail = $context->mail()->create()
97
			->header( 'X-MailGenerator', 'Aimeos' )
98
			->from( $config->get( 'resource/email/from-email' ), $config->get( 'resource/email/from-name' ) )
99
			->to( $addr->getEMail(), $addr->getFirstName() . ' ' . $addr->getLastName() )
100
			->bcc( $config->get( 'resource/email/bcc-email' ) );
101
102
		if( $replyTo = $config->get( 'resource/email/reply-email' ) ) {
103
			$mail->replyTo( $replyTo );
104
		}
105
106
		return $mail;
107
	}
108
109
110
	/**
111
	 * Returns the view for generating the mail message content
112
	 *
113
	 * @param string|null $langId Language ID the content should be generated for
114
	 * @return \Aimeos\Base\View\Iface View object
115
	 */
116
	protected function mailView( ?string $langId = null ) : \Aimeos\Base\View\Iface
117
	{
118
		$view = $this->context()->view();
119
120
		$helper = new \Aimeos\Base\View\Helper\Number\Locale( $view, $langId );
121
		$view->addHelper( 'number', $helper );
122
123
		$helper = new \Aimeos\Base\View\Helper\Translate\Standard( $view, $this->context()->i18n( $langId ?: 'en' ) );
124
		$view->addHelper( 'translate', $helper );
125
126
		return $view;
127
	}
128
}
129