Notification::handleConfiguration()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 19
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\DependencyInjection\Modules;
23
24
use Parthenon\Common\Exception\ParameterNotSetException;
25
use Parthenon\Notification\EmailSenderInterface;
26
use Parthenon\Notification\Sender\MailgunEmailSender;
27
use Parthenon\Notification\Sender\MessengerEmailSender;
28
use Parthenon\Notification\Sender\PostmarkEmailSender;
29
use Parthenon\Notification\Sender\SendGridEmailSender;
30
use Parthenon\Notification\Sender\SymfonyEmailSender;
31
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
32
use Symfony\Component\Config\FileLocator;
33
use Symfony\Component\DependencyInjection\ContainerBuilder;
34
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
35
36
final class Notification implements ModuleConfigurationInterface
37
{
38
    public function addConfig(NodeBuilder $nodeBuilder): void
39
    {
40
        $nodeBuilder
41
            ->arrayNode('notification')
42
                ->children()
43
                    ->booleanNode('enabled')->end()
44
                    ->arrayNode('email')
0 ignored issues
show
Bug introduced by
The method arrayNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

44
                    ->/** @scrutinizer ignore-call */ arrayNode('email')
Loading history...
45
                        ->children()
46
                            ->scalarNode('from_address')->defaultValue('[email protected]')->end()
47
                            ->scalarNode('from_name')->defaultValue('Parthenon')->end()
48
                            ->scalarNode('provider')->end()
49
                            ->booleanNode('send_via_queue')->defaultValue(false)->end()
50
                            ->arrayNode('postmark')
51
                                ->children()
52
                                    ->scalarNode('api_key')->end()
53
                                ->end()
54
                            ->end()
55
                            ->arrayNode('mailgun')
56
                                ->children()
57
                                    ->scalarNode('api_key')->end()
58
                                    ->scalarNode('api_url')->end()
59
                                    ->scalarNode('domain')->end()
60
                                ->end()
61
                            ->end()
62
                            ->arrayNode('sendgrid')
63
                                ->children()
64
                                    ->scalarNode('api_key')->end()
65
                                ->end()
66
                            ->end()
67
                        ->end()
68
                    ->end()
69
                    ->arrayNode('slack')
70
                        ->children()
71
                            ->scalarNode('client_id')->end()
72
                            ->scalarNode('client_secret')->end()
73
                            ->scalarNode('redirect_url')->end()
74
                        ->end()
75
                    ->end()
76
                ->end()
77
            ->end();
78
    }
79
80
    public function handleDefaultParameters(ContainerBuilder $container): void
81
    {
82
        $container->setParameter('parthenon_notification_email_from_address', '[email protected]');
83
        $container->setParameter('parthenon_notification_email_from_name', 'Parthenon');
84
        $container->setParameter('parthenon_notification_email_postmark_apikey', 'please-set-api-key');
85
        $container->setParameter('parthenon_notification_email_mailgun_apikey', 'please-set-api-key');
86
        $container->setParameter('parthenon_notification_email_mailgun_domain', 'please-set-domain');
87
        $container->setParameter('parthenon_notification_email_mailgun_api_url', 'https://api.mailgun.net');
88
        $container->setParameter('parthenon_notification_email_sendgrid_apikey', 'please-set-api-key');
89
        $container->setParameter('parthenon_notification_slack_client_id', '');
90
        $container->setParameter('parthenon_notification_slack_client_secret', '');
91
        $container->setParameter('parthenon_notification_slack_redirect_url', '');
92
    }
93
94
    public function handleConfiguration(array $config, ContainerBuilder $container): void
95
    {
96
        if (!isset($config['notification']) || !isset($config['notification']['enabled']) || false == $config['notification']['enabled']) {
97
            return;
98
        }
99
100
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config'));
101
        $loader->load('services/notification.xml');
102
103
        $config = $this->configureSlack($config, $container);
104
105
        if (!isset($config['notification']['email'])) {
106
            return;
107
        }
108
109
        $senderInterface = $this->configureSendToQueue($config['notification']['email']['send_via_queue'], $container);
110
        $config = $this->configureProvider($config, $container, $senderInterface);
111
        $config = $this->configureFromAddress($config, $container);
112
        $this->configureFromName($config, $container);
113
    }
114
115
    private function configureSlack(array $config, ContainerBuilder $container): array
116
    {
117
        if (isset($config['notification']['slack'])) {
118
            if (isset($config['notification']['slack']['client_id'])) {
119
                $container->setParameter('parthenon_notification_slack_client_id', $config['notification']['slack']['client_id']);
120
            }
121
122
            if (isset($config['notification']['slack']['client_secret'])) {
123
                $container->setParameter('parthenon_notification_slack_client_secret', $config['notification']['slack']['client_secret']);
124
            }
125
126
            if (isset($config['notification']['slack']['redirect_url'])) {
127
                $container->setParameter('parthenon_notification_slack_redirect_url', $config['notification']['slack']['redirect_url']);
128
            }
129
        }
130
131
        return $config;
132
    }
133
134
    private function configureSendToQueue($send_via_queue, ContainerBuilder $container): string
135
    {
136
        if (true === $send_via_queue) {
137
            $senderInterface = 'parthenon.notification.sender.background';
138
139
            $container->setAlias(EmailSenderInterface::class, MessengerEmailSender::class);
140
        } else {
141
            $senderInterface = EmailSenderInterface::class;
142
        }
143
144
        return $senderInterface;
145
    }
146
147
    /**
148
     * @throws ParameterNotSetException
149
     */
150
    private function configureMailGun(array $config, ContainerBuilder $container, string $senderInterface): array
151
    {
152
        if (!isset($config['notification']['email']['mailgun']) || empty($config['notification']['email']['mailgun'])) {
153
            throw new ParameterNotSetException('When the notification.email.provider is Mailgun you need to define mailgun.api_key and mailgun.domain');
154
        }
155
        if (!isset($config['notification']['email']['mailgun']['api_key']) || empty($config['notification']['email']['mailgun']['api_key'])) {
156
            throw new ParameterNotSetException('When the notification.email.provider is Mailgun you need to define mailgun.api_key');
157
        }
158
        if (!isset($config['notification']['email']['mailgun']['domain']) || empty($config['notification']['email']['mailgun']['domain'])) {
159
            throw new ParameterNotSetException('When the notification.email.provider is Mailgun you need to define mailgun.domain');
160
        }
161
        if (isset($config['notification']['email']['mailgun']['api_url']) && !empty($config['notification']['email']['mailgun']['api_url'])) {
162
            $container->setParameter('parthenon_notification_email_mailgun_api_url', $config['notification']['email']['mailgun']['api_url']);
163
        }
164
165
        $container->setParameter('parthenon_notification_email_mailgun_api_key', $config['notification']['email']['mailgun']['api_key']);
166
        $container->setParameter('parthenon_notification_email_mailgun_domain', $config['notification']['email']['mailgun']['domain']);
167
168
        $container->setAlias($senderInterface, MailgunEmailSender::class);
169
170
        return $config;
171
    }
172
173
    /**
174
     * @throws ParameterNotSetException
175
     */
176
    private function configureSendgrid(array $config, ContainerBuilder $container, string $senderInterface): array
177
    {
178
        if (!isset($config['notification']['email']['sendgrid']) || empty($config['notification']['email']['sendgrid'])) {
179
            throw new ParameterNotSetException('When the notification.email.provider is SendGrid you need to define sendgrid.api_key');
180
        }
181
182
        if (!isset($config['notification']['email']['sendgrid']['api_key']) || empty($config['notification']['email']['sendgrid']['api_key'])) {
183
            throw new ParameterNotSetException('When the notification.email.provider is SendGrid you need to define sendgrid.api_key');
184
        }
185
186
        $container->setParameter('parthenon_notification_email_sendgrid_api_key', $config['notification']['email']['sendgrid']['api_key']);
187
188
        $container->setAlias($senderInterface, SendGridEmailSender::class);
189
190
        return $config;
191
    }
192
193
    /**
194
     * @throws ParameterNotSetException
195
     */
196
    private function configurePostmark(array $config, ContainerBuilder $container, string $senderInterface): array
197
    {
198
        if (!isset($config['notification']['email']['postmark']) || empty($config['notification']['email']['postmark'])) {
199
            throw new ParameterNotSetException('When the notification.email.provider is Postmark you need to define postmark.api_key');
200
        }
201
202
        if (!isset($config['notification']['email']['postmark']['api_key']) || empty($config['notification']['email']['postmark']['api_key'])) {
203
            throw new ParameterNotSetException('When the notification.email.provider is Postmark you need to define postmark.api_key');
204
        }
205
206
        $container->setParameter('parthenon_notification_email_postmark_apikey', $config['notification']['email']['postmark']['api_key']);
207
208
        $container->setAlias($senderInterface, PostmarkEmailSender::class);
209
210
        return $config;
211
    }
212
213
    /**
214
     * @throws ParameterNotSetException
215
     */
216
    private function configureProvider(array $config, ContainerBuilder $container, string $senderInterface): array
217
    {
218
        if (isset($config['notification']['email']['provider'])) {
219
            if ('mailgun' === strtolower($config['notification']['email']['provider'])) {
220
                $config = $this->configureMailGun($config, $container, $senderInterface);
221
            } elseif ('sendgrid' === strtolower($config['notification']['email']['provider'])) {
222
                $config = $this->configureSendgrid($config, $container, $senderInterface);
223
            } elseif ('postmark' === strtolower($config['notification']['email']['provider'])) {
224
                $config = $this->configurePostmark($config, $container, $senderInterface);
225
            } elseif ('symfony' === strtolower($config['notification']['email']['provider'])) {
226
                $container->setAlias($senderInterface, SymfonyEmailSender::class);
227
            }
228
        }
229
230
        return $config;
231
    }
232
233
    private function configureFromAddress(array $config, ContainerBuilder $container): array
234
    {
235
        if (isset($config['notification']['email']['from_address'])) {
236
            $container->setParameter('parthenon_notification_email_from_address', $config['notification']['email']['from_address']);
237
        }
238
239
        return $config;
240
    }
241
242
    private function configureFromName(array $config, ContainerBuilder $container): void
243
    {
244
        if (isset($config['notification']['email']['from_name'])) {
245
            $container->setParameter('parthenon_notification_email_from_name', $config['notification']['email']['from_name']);
246
        }
247
    }
248
}
249