SesApiService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 6
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B bind() 6 43 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Swm\Bundle\MailHookBundle\ApiService;
4
5
use Swm\Bundle\MailHookBundle\Hook\DefaultHook;
6
use Swm\Bundle\MailHookBundle\SwmMailHookEvent;
7
8
class SesApiService extends BaseApiService
9
{
10
    private $eventAssoc = [
11
        'permanent_bounce'    => SwmMailHookEvent::MAILHOOK_HARDBOUNCE,
12
        'transient_bounce'    => SwmMailHookEvent::MAILHOOK_SOFTBOUNCE,
13
        'undetermined_bounce' => SwmMailHookEvent::MAILHOOK_SOFTBOUNCE,
14
        'spam_complaint'      => SwmMailHookEvent::MAILHOOK_SPAM,
15
        'delivery'            => SwmMailHookEvent::MAILHOOK_SEND,
16
    ];
17
18
    const SNS_BOUNCE    = 'Bounce';
19
    const SNS_COMPLAINT = 'Complaint';
20
    const SNS_DELIVERY  = 'Delivery';
21
22
    /**
23
     * @throws \Exception
24
     *
25
     * @return array<HookInterface>
26
     */
27
    public function bind()
28
    {
29
        $metaData = json_decode($this->request->getContent(), true);
30
31
        if (isset($metaData['Type']) && $metaData['Type'] == 'SubscriptionConfirmation') {
32
            throw new \Exception('AWS SNS Require a subscription confirmation: '.$metaData['SubscribeURL']);
33
        }
34
35
        if (!isset($metaData['Message'])) {
36
            throw new \Exception('Could not find data');
37
        }
38
39
        $sesMessage = json_decode($metaData['Message'], true);
40
41
        $hooks = [];
42
43
        switch ($sesMessage['notificationType']) {
44
            case self::SNS_BOUNCE:
45
                $event = strtolower($sesMessage['bounce']['bounceType']).'_bounce';
46 View Code Duplication
                foreach ($sesMessage['bounce']['bouncedRecipients'] as $recipient) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                    $hooks[] = new DefaultHook($event, $recipient['emailAddress'], 'ses', $sesMessage, $this->eventAssoc[$event]);
48
                }
49
50
                break;
51
            case self::SNS_COMPLAINT:
52
                $event = 'spam_complaint';
53 View Code Duplication
                foreach ($sesMessage['complaint']['complainedRecipients'] as $recipient) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
                    $hooks[] = new DefaultHook($event, $recipient['emailAddress'], 'ses', $sesMessage, $this->eventAssoc[$event]);
55
                }
56
57
                break;
58
            case self::SNS_DELIVERY:
59
                $event = 'delivery';
60
                foreach ($sesMessage['delivery']['recipients'] as $recipient) {
61
                    $hooks[] = new DefaultHook($event, $recipient, 'ses', $sesMessage, $this->eventAssoc[$event]);
62
                }
63
64
                break;
65
            default:
66
        }
67
68
        return $hooks;
69
    }
70
}
71