Completed
Push — master ( 33fb79...c738ff )
by Adam
12s queued 10s
created

Email::setParameterItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Email.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:EmailModule!
9
 * @subpackage     Email
10
 * @since          1.0.0
11
 *
12
 * @date           30.12.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\EmailModule\Email;
18
19
use Nette;
20
use Nette\Application;
21
use Nette\Bridges\ApplicationLatte;
22
use Nette\Mail;
23
use Nette\Utils;
24
25
use IPub\Framework;
26
27
/**
28
 * Email object
29
 *
30
 * @package        iPublikuj:EmailModule!
31
 * @subpackage     Email
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35
final class Email implements IEmail
36
{
37
	/**
38
	 * Implement nette smart magic
39
	 */
40
	use Nette\SmartObject;
41
42
	/**
43
	 * @var Mail\IMailer
44
	 */
45
	private $mailer;
46
47
	/**
48
	 * @var Mail\Message
49
	 */
50
	private $message;
51
52
	/**
53
	 * @var ApplicationLatte\Template|NULL
54
	 */
55
	private $plainTemplate = NULL;
56
57
	/**
58
	 * @var ApplicationLatte\Template|NULL
59
	 */
60
	private $htmlTemplate = NULL;
61
62
	/**
63
	 * @var mixed[]
64
	 */
65
	private $parameters = [];
66
67
	/**
68
	 * @param Mail\Message $message
69
	 * @param Application\UI\ITemplateFactory $templateFactory
70
	 * @param Application\LinkGenerator $linkGenerator
71
	 * @param Mail\IMailer $mailer
72
	 * @param string|null $htmlTemplate
73
	 * @param string|null $plainTemplate
74
	 */
75
	function __construct(
76
		Mail\Message $message,
77
		Application\UI\ITemplateFactory $templateFactory,
78
		Application\LinkGenerator $linkGenerator,
79
		Mail\IMailer $mailer,
80
		?string $htmlTemplate = NULL,
81
		?string $plainTemplate = NULL
82
	) {
83
		if ((!$htmlTemplate || !file_exists($htmlTemplate)) && (!$plainTemplate || !file_exists($plainTemplate))) {
84
			if (!$htmlTemplate || !file_exists($htmlTemplate)) {
85
				throw new Framework\Exceptions\InvalidArgumentException(sprintf('Email html template file "%s" not found.', $htmlTemplate));
86
			} else {
87
				throw new Framework\Exceptions\InvalidArgumentException(sprintf('Email plain template file "%s" not found.', $plainTemplate));
88
			}
89
		}
90
91
		$this->message = $message;
92
		$this->mailer = $mailer;
93
94
		if ($htmlTemplate && file_exists($htmlTemplate)) {
95
			$this->htmlTemplate = $this->createTemplate($templateFactory, $linkGenerator, $htmlTemplate);
96
		}
97
98
		if ($plainTemplate && file_exists($plainTemplate)) {
99
			$this->plainTemplate = $this->createTemplate($templateFactory, $linkGenerator, $plainTemplate);
100
		}
101
	}
102
103
	/**
104
	 * @param Application\UI\ITemplateFactory $templateFactory
105
	 * @param Application\LinkGenerator $linkGenerator
106
	 * @param string $file
107
	 *
108
	 * @return ApplicationLatte\Template
109
	 */
110
	private function createTemplate(
111
		Application\UI\ITemplateFactory $templateFactory,
112
		Application\LinkGenerator $linkGenerator,
113
		string $file
114
	) : ApplicationLatte\Template {
115
		/** @var ApplicationLatte\Template $template */
116
		$template = $templateFactory->createTemplate();
117
		$template
118
			->getLatte()
119
			->addProvider('uiControl', $linkGenerator);
120
121
		$template->setFile($file);
122
123
		$template->add('', '');
124
		$template->add('', 'iPublikuj:cms!');
125
		$template->add('', '');
126
		$template->add('', '');
127
128
		return $template;
129
	}
130
131
	/**
132
	 * {@inheritdoc}
133
	 */
134
	public function setParameterItem(string $key, $value) : void
135
	{
136
		$this->parameters[$key] = $value;
137
	}
138
139
	/**
140
	 * {@inheritdoc}
141
	 */
142
	public function getMessage() : Mail\Message
143
	{
144
		return $this->message;
145
	}
146
147
	/**
148
	 * {@inheritdoc}
149
	 */
150
	public function getHtmlTemplate() : ApplicationLatte\Template
151
	{
152
		return $this->htmlTemplate;
0 ignored issues
show
Bug Best Practice introduced by Adam Kadlec
The expression return $this->htmlTemplate could return the type null which is incompatible with the type-hinted return Nette\Bridges\ApplicationLatte\Template. Consider adding an additional type-check to rule them out.
Loading history...
153
	}
154
155
	/**
156
	 * {@inheritdoc}
157
	 */
158
	public function getPlainTemplate() : ApplicationLatte\Template
159
	{
160
		return $this->plainTemplate;
0 ignored issues
show
Bug Best Practice introduced by Adam Kadlec
The expression return $this->plainTemplate could return the type null which is incompatible with the type-hinted return Nette\Bridges\ApplicationLatte\Template. Consider adding an additional type-check to rule them out.
Loading history...
161
	}
162
163
	/**
164
	 * {@inheritdoc}
165
	 */
166
	public function send() : void
167
	{
168
		if ($this->htmlTemplate) {
169
			foreach ($this->prepareParameters() as $key => $value) {
170
				$this->htmlTemplate->add($key, $value);
171
			}
172
173
			$this->message->setHtmlBody($this->htmlTemplate->renderToString());
174
		}
175
176
		if ($this->plainTemplate) {
177
			foreach ($this->prepareParameters() as $key => $value) {
178
				$this->plainTemplate->add($key, $value);
179
			}
180
181
			$this->message->setBody($this->plainTemplate->renderToString());
182
		}
183
184
		$this->mailer->send($this->message);
185
	}
186
187
	/**
188
	 * @return mixed[]
189
	 */
190
	private function prepareParameters() : array
191
	{
192
		$values = [];
193
194
		foreach ($this->parameters as $key => $value) {
195
			if ($key === 'layout') {
196
				$layout = $value;
197
198
				if (Utils\Strings::startsWith($layout, '@')) {
199
					$layout = __DIR__ . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS . 'templates' . DS . $layout;
200
				}
201
202
				$layout = Utils\Strings::replace($layout, '#.latte$#', '') . '.latte';
203
204
				if (!file_exists($layout)) {
205
					throw new Framework\Exceptions\InvalidArgumentException(sprintf('Layout file "%s" not found', $layout));
206
				}
207
208
				$values[$key] = $layout;
209
210
			} else {
211
				$values[$key] = $value;
212
			}
213
		}
214
215
		return $values;
216
	}
217
}
218