-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMailer.php
143 lines (122 loc) · 3.67 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace Remp\MailerModule\Models\Mailer;
use Nette\Utils\Strings;
use Remp\MailerModule\Models\Config\Config;
use Remp\MailerModule\Models\Config\ConfigNotExistsException;
use Remp\MailerModule\Repositories\ConfigsRepository;
abstract class Mailer implements \Nette\Mail\Mailer
{
public const ALIAS = "";
protected array $options = [];
protected ?string $code = null;
public function __construct(
private Config $config,
private ConfigsRepository $configsRepository,
?string $code = null,
) {
$this->code = $code;
$this->buildConfig();
}
public function getMailerAlias(): string
{
return self::buildAlias($this::ALIAS, $this->code);
}
public static function buildAlias($alias, $code)
{
$mailerAlias = str_replace('-', '_', Strings::webalize($alias));
if (isset($code)) {
$mailerAlias .= '_' . $code;
}
return $mailerAlias;
}
public function getIdentifier(): string
{
$array = explode('\\', get_class($this));
$label = end($array);
if (isset($this->code)) {
$label .= '_' . $this->code;
}
return $label;
}
public function getConfigs(): array
{
return $this->options;
}
/**
* Returns single config value
*
* @param string $config
* @return string|null
*/
public function getConfig(string $config): ?string
{
return $this->options[$config]['value'] ?? null;
}
protected function buildConfig(): void
{
foreach ($this->options as $name => $definition) {
$configName = $this->getConfigFieldName($name);
try {
$this->options[$name]['value'] = $this->config->get($configName);
} catch (ConfigNotExistsException $e) {
$this->configsRepository->add(
$configName,
$definition['label'],
null,
$definition['description'] ?? null,
Config::TYPE_STRING
);
$this->config->refresh(true);
$this->options[$name] = [
'label' => $definition['label'],
'required' => $definition['required'],
'value' => null,
];
}
}
}
private function getConfigFieldName(string $name): string
{
return $this->getMailerAlias() . '_' . $name;
}
public function isConfigured(): bool
{
foreach ($this->getRequiredOptions() as $option) {
if (!isset($option['value'])) {
return false;
}
}
return true;
}
public function getRequiredOptions(): array
{
return array_filter($this->options, function ($option) {
return $option['required'];
});
}
/**
* If Mailer implementation supports template parameters (e.g. within batch email sending)
* you can replace the real values of params with names of template variables which will
* be used to inject the values by Mailer service.
*
* Return value is ordered as [transformed params for twig,
* altered params for mailer header X-Mailer-Template-Params]
*
* @param array $params
* @return array
*/
public function transformTemplateParams(array $params): array
{
return [$params, $params];
}
/**
* supportsBatch returns flag, whether the selected Mailer supports batch sending
*
* @return bool
*/
public function supportsBatch(): bool
{
return false;
}
}