Layout   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isNotEmpty() 0 3 2
A bindNetteTemplate() 0 4 2
A setPlain() 0 3 1
A bindMessage() 0 13 5
A setHtml() 0 3 1
A __construct() 0 3 1
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