Total Complexity | 54 |
Total Lines | 357 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like SparkPostController 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 SparkPostController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class SparkPostController extends Controller |
||
20 | { |
||
21 | protected $eventsCount = 0; |
||
22 | protected $skipCount = 0; |
||
23 | private static $allowed_actions = [ |
||
|
|||
24 | 'incoming', |
||
25 | 'test', |
||
26 | 'configure_inbound_emails' |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * Inject public dependencies into the controller |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private static $dependencies = [ |
||
35 | 'logger' => '%$Psr\Log\LoggerInterface', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * @var Psr\Log\LoggerInterface |
||
40 | */ |
||
41 | public $logger; |
||
42 | |||
43 | public function index(HTTPRequest $req) |
||
48 | ]); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * You can also see /resources/sample.json |
||
53 | * |
||
54 | * @param HTTPRequest $req |
||
55 | */ |
||
56 | public function test(HTTPRequest $req) |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @link https://support.sparkpost.com/customer/portal/articles/2039614-enabling-inbound-email-relaying-relay-webhooks |
||
79 | * @param HTTPRequest $req |
||
80 | * @return string |
||
81 | */ |
||
82 | public function configure_inbound_emails(HTTPRequest $req) |
||
198 | } |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Handle incoming webhook |
||
203 | * |
||
204 | * @link https://developers.sparkpost.com/api/#/reference/webhooks/create-a-webhook |
||
205 | * @link https://www.sparkpost.com/blog/webhooks-beyond-the-basics/ |
||
206 | * @link https://support.sparkpost.com/customer/portal/articles/1976204-webhook-event-reference |
||
207 | * @param HTTPRequest $req |
||
208 | */ |
||
209 | public function incoming(HTTPRequest $req) |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Process data |
||
300 | * |
||
301 | * @param array $payload |
||
302 | * @param string $batchId |
||
303 | */ |
||
304 | protected function processPayload(array $payload, $batchId = null) |
||
305 | { |
||
306 | $this->extend('beforeProcessPayload', $payload, $batchId); |
||
307 | |||
308 | $subaccount = SparkPostHelper::getClient()->getSubaccount(); |
||
309 | |||
310 | foreach ($payload as $r) { |
||
311 | $ev = $r['msys'] ?? null; |
||
312 | |||
313 | if ($ev === null) { |
||
314 | $logLevel = self::config()->log_level ? self::config()->log_level : 7; |
||
315 | $this->getLogger()->log("Invalid payload: " . substr(json_encode($r), 0, 100) . '...', $logLevel); |
||
316 | continue; |
||
317 | } |
||
318 | |||
319 | // This is a test payload |
||
320 | if (empty($ev)) { |
||
321 | continue; |
||
322 | } |
||
323 | |||
324 | $type = key($ev); |
||
325 | if (!isset($ev[$type])) { |
||
326 | $this->getLogger()->warn("Invalid type $type in SparkPost payload"); |
||
327 | continue; |
||
328 | } |
||
329 | $data = $ev[$type]; |
||
330 | |||
331 | // Ignore events not related to the subaccount we are managing |
||
332 | if (!empty($data['subaccount_id']) && $subaccount && $subaccount != $data['subaccount_id']) { |
||
333 | $this->skipCount++; |
||
334 | continue; |
||
335 | } |
||
336 | |||
337 | $this->eventsCount++; |
||
338 | $this->extend('onAnyEvent', $data, $type); |
||
339 | |||
340 | switch ($type) { |
||
341 | //Click, Open |
||
342 | case SparkPostApiClient::TYPE_ENGAGEMENT: |
||
343 | $this->extend('onEngagementEvent', $data, $type); |
||
344 | break; |
||
345 | //Generation Failure, Generation Rejection |
||
346 | case SparkPostApiClient::TYPE_GENERATION: |
||
347 | $this->extend('onGenerationEvent', $data, $type); |
||
348 | break; |
||
349 | //Bounce, Delivery, Injection, SMS Status, Spam Complaint, Out of Band, Policy Rejection, Delay |
||
350 | case SparkPostApiClient::TYPE_MESSAGE: |
||
351 | $this->extend('onMessageEvent', $data, $type); |
||
352 | break; |
||
353 | //Relay Injection, Relay Rejection, Relay Delivery, Relay Temporary Failure, Relay Permanent Failure |
||
354 | case SparkPostApiClient::TYPE_RELAY: |
||
355 | $this->extend('onRelayEvent', $data, $type); |
||
356 | break; |
||
357 | //List Unsubscribe, Link Unsubscribe |
||
358 | case SparkPostApiClient::TYPE_UNSUBSCRIBE: |
||
359 | $this->extend('onUnsubscribeEvent', $data, $type); |
||
360 | break; |
||
361 | } |
||
362 | } |
||
363 | |||
364 | $this->extend('afterProcessPayload', $payload, $batchId); |
||
365 | } |
||
366 | |||
367 | |||
368 | /** |
||
369 | * Get logger |
||
370 | * |
||
371 | * @return Psr\SimpleCache\CacheInterface |
||
372 | */ |
||
373 | public function getLogger() |
||
376 | } |
||
377 | } |
||
378 |