Completed
Pull Request — master (#5)
by Antoine
01:47
created

SesApiService::bind()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 43
Code Lines 26

Duplication

Lines 6
Ratio 13.95 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 43
rs 4.8196
cc 10
eloc 26
nc 7
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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($params['Type']) && $metaData['Type'] == 'SubscriptionConfirmation') {
0 ignored issues
show
Bug introduced by
The variable $params seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
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