Layout::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\MailManager\Template;
4
5
use Nette\Application\UI;
6
use Nette\Mail;
7
8
class Layout
9
{
10
11
	/** @var string|UI\ITemplate|null */
12
	private $html;
13
14
	/** @var string|UI\ITemplate|null */
15
	private $plain;
16
17
18
	public function __construct($plain = null)
19
	{
20
		$this->setPlain($plain);
21
	}
22
23
24
	/**
25
	 * @param string|UI\ITemplate|null $html
26
	 */
27
	public function setHtml($html): void
28
	{
29
		$this->html = $html;
30
	}
31
32
33
	/**
34
	 * @param string|UI\ITemplate|null $plain
35
	 */
36
	public function setPlain($plain): void
37
	{
38
		$this->plain = $plain;
39
	}
40
41
42
	public function bindMessage(Mail\Message $message, array $data = [], string $assetsDir = ''): void
43
	{
44
		if (self::isNotEmpty($this->plain)) {
45
			if ($this->plain instanceof UI\ITemplate) {
46
				self::bindNetteTemplate($this->plain, $data);
47
			}
48
			$message->setBody((string) $this->plain);
49
		}
50
		if (self::isNotEmpty($this->html)) {
51
			if ($this->html instanceof UI\ITemplate) {
52
				self::bindNetteTemplate($this->html, $data);
53
			}
54
			$message->setHtmlBody((string) $this->html, $assetsDir);
55
		}
56
	}
57
58
59
	/**
60
	 * Add variable to template
61
	 */
62
	private static function bindNetteTemplate(UI\ITemplate $template, array $data): void
63
	{
64
		foreach ($data as $key => $value) {
65
			$template->{$key} = $value;
66
		}
67
	}
68
69
70
	private static function isNotEmpty($variable): bool
71
	{
72
		return $variable !== null && $variable !== '';
73
	}
74
75
}
76