-
Notifications
You must be signed in to change notification settings - Fork 452
/
SenderConfiguration.php
104 lines (89 loc) · 2.81 KB
/
SenderConfiguration.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
<?php
declare(strict_types=1);
namespace Concrete\Core\Mail;
use RuntimeException;
defined('C5_EXECUTE') or die('Access Denied.');
/**
* This class holds the configurable addresses for outgoing emails.
* Packages can add their own addresses in the package controller on_start method with some code like this:
* <pre><code>$app->extend(
* SenderConfiguration::class,
* static function(SenderConfiguration $configuration): SenderConfiguration {
* return $configuration->addEntry(
* (new SenderConfiguration\Entry(t('Name of the option'), 'email.configuration.key'))
* ->setPackageHandle('your_package_handle')
* );
* }
* );
* </code></pre>.
*/
final class SenderConfiguration
{
/**
* @var \Concrete\Core\Mail\SenderConfiguration\Entry[]
*/
private $entries = [];
/**
* @var string
*/
private $allConfigurationKeys = [];
/**
* @param \Concrete\Core\Mail\SenderConfiguration\Entry[] $entries
*
* @return $this
*/
public function addEntries(iterable $entries): self
{
foreach ($entries as $entry) {
$this->addEntry($entry);
}
return $this;
}
/**
* @throws \RuntimeException in case of duplicated configuration keys
*
* @return $this
*/
public function addEntry(SenderConfiguration\Entry $entry): self
{
$prefix = $entry->getPackageHandle();
if ($prefix !== '') {
$prefix = "{$prefix}::";
}
$key = $prefix . $entry->getEmailKey();
if (in_array($key, $this->allConfigurationKeys, true)) {
throw new RuntimeException(t('The configuration key %s has been already specified.', $key));
}
$this->allConfigurationKeys[] = $key;
if ($entry->getNameKey() !== '') {
$key = $prefix . $entry->getNameKey();
if (in_array($key, $this->allConfigurationKeys, true)) {
throw new RuntimeException(t('The configuration key %s has been already specified.', $key));
}
$this->allConfigurationKeys[] = $key;
}
$this->entries[] = $entry;
return $this;
}
/**
* @return \Concrete\Core\Mail\SenderConfiguration\Entry[]
*/
public function getEntries(): array
{
$this->sortEntries();
return $this->entries;
}
private function sortEntries(): void
{
usort(
$this->entries,
static function (SenderConfiguration\Entry $a, SenderConfiguration\Entry $b): int {
$delta = $b->getPriority() - $a->getPriority();
if ($delta === 0) {
$delta = mb_strtolower($a->getName()) <=> mb_strtolower($b->getName());
}
return $delta;
}
);
}
}