Completed
Push — master ( 11169c...fe5bbf )
by Milan
02:35
created

MailManagerExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
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\DI;
4
5
use h4kuna\MailManager;
6
use Nette\DI as NDI;
7
use Nette\Mail;
8
use Nette\Schema;
9
10
class MailManagerExtension extends NDI\CompilerExtension
11
{
12
13
	/** @var bool */
14
	private $debugMode;
15
16
17
	public function __construct(bool $debugMode = false)
18
	{
19
		$this->debugMode = $debugMode;
20
	}
21
22
23
	public function loadConfiguration()
24
	{
25
		$this->buildMessageFactory((string) $this->config->from, (string) $this->config->returnPath);
26
27
		$this->buildTemplateFactory($this->config->globalVars);
28
29
		$mailer = $this->buildMailer($this->config->debugMode, $this->config->tempDir, $this->config->live);
30
31
		$this->buildLayoutFactory((string) $this->config->templateDir, $this->config->plainMacro);
32
33
		$this->buildMailManager($mailer, (string) $this->config->assetsDir);
34
	}
35
36
37
	public function getConfigSchema(): Schema\Schema
38
	{
39
		return Schema\Expect::structure([
40
			// mail manager
41
			'assetsDir' => Schema\Expect::string(''),
42
43
			// layout
44
			'templateDir' => Schema\Expect::string(''),
45
			'plainMacro' => Schema\Expect::string('{file}-plain'), // plain/%file% or plain-%file%
46
47
			// template factory
48
			'globalVars' => Schema\Expect::array([]),
0 ignored issues
show
Unused Code introduced by
The call to Nette\Schema\Expect::array() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
			'globalVars' => Schema\Expect::/** @scrutinizer ignore-call */ array([]),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
50
			// message
51
			'from' => Schema\Expect::string(''),
52
			'returnPath' => Schema\Expect::string(''),
53
54
			// file mailer
55
			'debugMode' => Schema\Expect::bool($this->debugMode),
56
			'tempDir' => Schema\Expect::string('/tmp'),
57
			'live' => Schema\Expect::string('1 minute')->nullable(),
58
		]);
59
	}
60
61
62
	private function buildMessageFactory(string $from, string $returnPath): void
63
	{
64
		$this->getContainerBuilder()
65
			->addDefinition($this->prefix('messageFactory'))
66
			->setFactory(MailManager\Message\MessageFactory::class)
67
			->setArguments([$from, $returnPath])
68
			->setAutowired(false);
69
	}
70
71
72
	private function buildTemplateFactory(array $globalVars): void
73
	{
74
		$this->getContainerBuilder()
75
			->addDefinition($this->prefix('templateFactory'))
76
			->setFactory(MailManager\Template\TemplateFactory::class)
77
			->addSetup('setVariables', [$globalVars])
78
			->setAutowired(false);
79
	}
80
81
82
	private function buildLayoutFactory(string $templateDir, string $plainMacro): void
83
	{
84
		$this->getContainerBuilder()
85
			->addDefinition($this->prefix('layoutFactory'))
86
			->setFactory(MailManager\Template\LayoutFactory::class)
87
			->setArguments([$this->prefix('@templateFactory')])
88
			->addSetup('setTemplateDir', [$templateDir])
89
			->addSetup('setPlainMacro', [$plainMacro])
90
			->setAutowired(false);
91
	}
92
93
94
	private function buildMailer(bool $debugMode, string $tempDir, ?string $live): string
95
	{
96
		if (!$debugMode) {
97
			try {
98
				$definition = $this->getContainerBuilder()->getDefinitionByType(Mail\IMailer::class);
99
			} catch (NDI\MissingServiceException $e) {
100
				return '';
101
			}
102
			return '@' . $definition->getName();
103
		}
104
		$mailerBuilder = $this->getContainerBuilder()
105
			->addDefinition($this->prefix('fileMailer'))
106
			->setFactory(MailManager\Mailer\FileMailer::class)
107
			->setArguments([$tempDir])
108
			->setAutowired(false);
109
110
		if ($live !== null) {
111
			$mailerBuilder->addSetup('setLive', [$live]);
112
		}
113
		return $this->prefix('@fileMailer');
114
	}
115
116
117
	private function buildMailManager(string $mailer, string $assetsDir): void
118
	{
119
		$mailManager = $this->getContainerBuilder()
120
			->addDefinition($this->prefix('mailManager'))
121
			->setFactory(MailManager\MailManager::class)
122
			->setArguments([$mailer, $this->prefix('@messageFactory'), $this->prefix('@layoutFactory')]);
123
124
		if ($assetsDir !== '') {
125
			$mailManager->addSetup('setAssetsDir', [$assetsDir]);
126
		}
127
	}
128
129
}
130