Total Complexity | 43 |
Total Lines | 210 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Notification often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Notification, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | final class Notification implements ModuleConfigurationInterface |
||
37 | { |
||
38 | public function addConfig(NodeBuilder $nodeBuilder): void |
||
78 | } |
||
79 | |||
80 | public function handleDefaultParameters(ContainerBuilder $container): void |
||
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 |
||
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 |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @throws ParameterNotSetException |
||
215 | */ |
||
216 | private function configureProvider(array $config, ContainerBuilder $container, string $senderInterface): array |
||
231 | } |
||
232 | |||
233 | private function configureFromAddress(array $config, ContainerBuilder $container): array |
||
240 | } |
||
241 | |||
242 | private function configureFromName(array $config, ContainerBuilder $container): void |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 |