1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright Humbly Arrogant Ltd 2020-2022. |
7
|
|
|
* |
8
|
|
|
* Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license. |
9
|
|
|
* |
10
|
|
|
* Change Date: TBD ( 3 years after 2.0.0 release ) |
11
|
|
|
* |
12
|
|
|
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Parthenon\DependencyInjection\Modules; |
16
|
|
|
|
17
|
|
|
use Parthenon\Common\Exception\ParameterNotSetException; |
18
|
|
|
use Parthenon\Notification\EmailSenderInterface; |
19
|
|
|
use Parthenon\Notification\Sender\MailgunEmailSender; |
20
|
|
|
use Parthenon\Notification\Sender\MessengerEmailSender; |
21
|
|
|
use Parthenon\Notification\Sender\PostmarkEmailSender; |
22
|
|
|
use Parthenon\Notification\Sender\SendGridEmailSender; |
23
|
|
|
use Parthenon\Notification\Sender\SymfonyEmailSender; |
24
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeBuilder; |
25
|
|
|
use Symfony\Component\Config\FileLocator; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
|
|
27
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
|
|
|
28
|
|
|
|
29
|
|
|
final class Notification implements ModuleConfigurationInterface |
30
|
|
|
{ |
31
|
|
|
public function addConfig(NodeBuilder $nodeBuilder): void |
32
|
|
|
{ |
33
|
|
|
$nodeBuilder |
34
|
|
|
->arrayNode('notification') |
35
|
|
|
->children() |
36
|
|
|
->booleanNode('enabled')->end() |
37
|
|
|
->arrayNode('email') |
|
|
|
|
38
|
|
|
->children() |
39
|
|
|
->scalarNode('from_address')->defaultValue('[email protected]')->end() |
40
|
|
|
->scalarNode('from_name')->defaultValue('Parthenon')->end() |
41
|
|
|
->scalarNode('provider')->end() |
42
|
|
|
->booleanNode('send_via_queue')->defaultValue(false)->end() |
43
|
|
|
->arrayNode('postmark') |
44
|
|
|
->children() |
45
|
|
|
->scalarNode('api_key')->end() |
46
|
|
|
->end() |
47
|
|
|
->end() |
48
|
|
|
->arrayNode('mailgun') |
49
|
|
|
->children() |
50
|
|
|
->scalarNode('api_key')->end() |
51
|
|
|
->scalarNode('api_url')->end() |
52
|
|
|
->scalarNode('domain')->end() |
53
|
|
|
->end() |
54
|
|
|
->end() |
55
|
|
|
->arrayNode('sendgrid') |
56
|
|
|
->children() |
57
|
|
|
->scalarNode('api_key')->end() |
58
|
|
|
->end() |
59
|
|
|
->end() |
60
|
|
|
->end() |
61
|
|
|
->end() |
62
|
|
|
->arrayNode('slack') |
63
|
|
|
->children() |
64
|
|
|
->scalarNode('client_id')->end() |
65
|
|
|
->scalarNode('client_secret')->end() |
66
|
|
|
->scalarNode('redirect_url')->end() |
67
|
|
|
->end() |
68
|
|
|
->end() |
69
|
|
|
->end() |
70
|
|
|
->end(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function handleDefaultParameters(ContainerBuilder $container): void |
74
|
|
|
{ |
75
|
|
|
$container->setParameter('parthenon_notification_email_from_address', '[email protected]'); |
76
|
|
|
$container->setParameter('parthenon_notification_email_from_name', 'Parthenon'); |
77
|
|
|
$container->setParameter('parthenon_notification_email_postmark_apikey', 'please-set-api-key'); |
78
|
|
|
$container->setParameter('parthenon_notification_email_mailgun_apikey', 'please-set-api-key'); |
79
|
|
|
$container->setParameter('parthenon_notification_email_mailgun_domain', 'please-set-domain'); |
80
|
|
|
$container->setParameter('parthenon_notification_email_mailgun_api_url', 'https://api.mailgun.net'); |
81
|
|
|
$container->setParameter('parthenon_notification_email_sendgrid_apikey', 'please-set-api-key'); |
82
|
|
|
$container->setParameter('parthenon_notification_slack_client_id', ''); |
83
|
|
|
$container->setParameter('parthenon_notification_slack_client_secret', ''); |
84
|
|
|
$container->setParameter('parthenon_notification_slack_redirect_url', ''); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function handleConfiguration(array $config, ContainerBuilder $container): void |
88
|
|
|
{ |
89
|
|
|
if (!isset($config['notification']) || !isset($config['notification']['enabled']) || false == $config['notification']['enabled']) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config')); |
94
|
|
|
$loader->load('services/notification.xml'); |
95
|
|
|
|
96
|
|
|
$config = $this->configureSlack($config, $container); |
97
|
|
|
|
98
|
|
|
if (!isset($config['notification']['email'])) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$senderInterface = $this->configureSendToQueue($config['notification']['email']['send_via_queue'], $container); |
103
|
|
|
$config = $this->configureProvider($config, $container, $senderInterface); |
104
|
|
|
$config = $this->configureFromAddress($config, $container); |
105
|
|
|
$this->configureFromName($config, $container); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function configureSlack(array $config, ContainerBuilder $container): array |
109
|
|
|
{ |
110
|
|
|
if (isset($config['notification']['slack'])) { |
111
|
|
|
if (isset($config['notification']['slack']['client_id'])) { |
112
|
|
|
$container->setParameter('parthenon_notification_slack_client_id', $config['notification']['slack']['client_id']); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (isset($config['notification']['slack']['client_secret'])) { |
116
|
|
|
$container->setParameter('parthenon_notification_slack_client_secret', $config['notification']['slack']['client_secret']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (isset($config['notification']['slack']['redirect_url'])) { |
120
|
|
|
$container->setParameter('parthenon_notification_slack_redirect_url', $config['notification']['slack']['redirect_url']); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $config; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $send_via_queue |
129
|
|
|
*/ |
130
|
|
|
private function configureSendToQueue($send_via_queue, ContainerBuilder $container): string |
131
|
|
|
{ |
132
|
|
|
if (true === $send_via_queue) { |
133
|
|
|
$senderInterface = 'parthenon.notification.sender.background'; |
134
|
|
|
|
135
|
|
|
$container->setAlias(EmailSenderInterface::class, MessengerEmailSender::class); |
136
|
|
|
} else { |
137
|
|
|
$senderInterface = EmailSenderInterface::class; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $senderInterface; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @throws ParameterNotSetException |
145
|
|
|
*/ |
146
|
|
|
private function configureMailGun(array $config, ContainerBuilder $container, string $senderInterface): array |
147
|
|
|
{ |
148
|
|
|
if (!isset($config['notification']['email']['mailgun']) || empty($config['notification']['email']['mailgun'])) { |
149
|
|
|
throw new ParameterNotSetException('When the notification.email.provider is Mailgun you need to define mailgun.api_key and mailgun.domain'); |
150
|
|
|
} |
151
|
|
|
if (!isset($config['notification']['email']['mailgun']['api_key']) || empty($config['notification']['email']['mailgun']['api_key'])) { |
152
|
|
|
throw new ParameterNotSetException('When the notification.email.provider is Mailgun you need to define mailgun.api_key'); |
153
|
|
|
} |
154
|
|
|
if (!isset($config['notification']['email']['mailgun']['domain']) || empty($config['notification']['email']['mailgun']['domain'])) { |
155
|
|
|
throw new ParameterNotSetException('When the notification.email.provider is Mailgun you need to define mailgun.domain'); |
156
|
|
|
} |
157
|
|
|
if (isset($config['notification']['email']['mailgun']['api_url']) && !empty($config['notification']['email']['mailgun']['api_url'])) { |
158
|
|
|
$container->setParameter('parthenon_notification_email_mailgun_api_url', $config['notification']['email']['mailgun']['api_url']); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$container->setParameter('parthenon_notification_email_mailgun_api_key', $config['notification']['email']['mailgun']['api_key']); |
162
|
|
|
$container->setParameter('parthenon_notification_email_mailgun_domain', $config['notification']['email']['mailgun']['domain']); |
163
|
|
|
|
164
|
|
|
$container->setAlias($senderInterface, MailgunEmailSender::class); |
165
|
|
|
|
166
|
|
|
return $config; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @throws ParameterNotSetException |
171
|
|
|
*/ |
172
|
|
|
private function configureSendgrid(array $config, ContainerBuilder $container, string $senderInterface): array |
173
|
|
|
{ |
174
|
|
|
if (!isset($config['notification']['email']['sendgrid']) || empty($config['notification']['email']['sendgrid'])) { |
175
|
|
|
throw new ParameterNotSetException('When the notification.email.provider is SendGrid you need to define sendgrid.api_key'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (!isset($config['notification']['email']['sendgrid']['api_key']) || empty($config['notification']['email']['sendgrid']['api_key'])) { |
179
|
|
|
throw new ParameterNotSetException('When the notification.email.provider is SendGrid you need to define sendgrid.api_key'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$container->setParameter('parthenon_notification_email_sendgrid_api_key', $config['notification']['email']['sendgrid']['api_key']); |
183
|
|
|
|
184
|
|
|
$container->setAlias($senderInterface, SendGridEmailSender::class); |
185
|
|
|
|
186
|
|
|
return $config; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @throws ParameterNotSetException |
191
|
|
|
*/ |
192
|
|
|
private function configurePostmark(array $config, ContainerBuilder $container, string $senderInterface): array |
193
|
|
|
{ |
194
|
|
|
if (!isset($config['notification']['email']['postmark']) || empty($config['notification']['email']['postmark'])) { |
195
|
|
|
throw new ParameterNotSetException('When the notification.email.provider is Postmark you need to define postmark.api_key'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (!isset($config['notification']['email']['postmark']['api_key']) || empty($config['notification']['email']['postmark']['api_key'])) { |
199
|
|
|
throw new ParameterNotSetException('When the notification.email.provider is Postmark you need to define postmark.api_key'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$container->setParameter('parthenon_notification_email_sendgrid_api_key', $config['notification']['email']['sendgrid']['api_key']); |
203
|
|
|
|
204
|
|
|
$container->setAlias($senderInterface, PostmarkEmailSender::class); |
205
|
|
|
|
206
|
|
|
return $config; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @throws ParameterNotSetException |
211
|
|
|
*/ |
212
|
|
|
private function configureProvider(array $config, ContainerBuilder $container, string $senderInterface): array |
213
|
|
|
{ |
214
|
|
|
if (isset($config['notification']['email']['provider'])) { |
215
|
|
|
if ('mailgun' === strtolower($config['notification']['email']['provider'])) { |
216
|
|
|
$config = $this->configureMailGun($config, $container, $senderInterface); |
217
|
|
|
} elseif ('sendgrid' === strtolower($config['notification']['email']['provider'])) { |
218
|
|
|
$config = $this->configureSendgrid($config, $container, $senderInterface); |
219
|
|
|
} elseif ('postmark' === strtolower($config['notification']['email']['provider'])) { |
220
|
|
|
$config = $this->configurePostmark($config, $container, $senderInterface); |
221
|
|
|
} elseif ('symfony' === strtolower($config['notification']['email']['provider'])) { |
222
|
|
|
$container->setAlias($senderInterface, SymfonyEmailSender::class); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $config; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
private function configureFromAddress(array $config, ContainerBuilder $container): array |
230
|
|
|
{ |
231
|
|
|
if (isset($config['notification']['email']['from_address'])) { |
232
|
|
|
$container->setParameter('parthenon_notification_email_from_address', $config['notification']['email']['from_address']); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $config; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
private function configureFromName(array $config, ContainerBuilder $container): void |
239
|
|
|
{ |
240
|
|
|
if (isset($config['notification']['email']['from_name'])) { |
241
|
|
|
$container->setParameter('parthenon_notification_email_from_name', $config['notification']['email']['from_name']); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths