NotificationListener::postPersist()   C
last analyzed

Complexity

Conditions 17
Paths 9

Size

Total Lines 63

Duplication

Lines 12
Ratio 19.05 %

Importance

Changes 0
Metric Value
dl 12
loc 63
rs 5.2166
c 0
b 0
f 0
cc 17
nc 9
nop 1

How to fix   Long Method    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 KI\UserBundle\Listener;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use KI\CoreBundle\Service\CurlService;
7
use KI\UserBundle\Entity\Device;
8
use KI\UserBundle\Entity\Notification;
9
10
class NotificationListener
11
{
12
    protected $gcmKey;
13
    protected $curlService;
14
15
    public function __construct(CurlService $curlService, $gcmKey)
16
    {
17
        $this->curlService = $curlService;
18
        $this->gcmKey = $gcmKey;
19
    }
20
21
    public function postPersist(LifecycleEventArgs $args)
22
    {
23
        $notification = $args->getEntity();
24
25
        if (!$notification instanceof Notification)
26
            return;
27
28
        $manager = $args->getEntityManager();
29
        $repo = $manager->getRepository('KIUserBundle:Device');
30
        $devices = $repo->findAll();
31
        $sendToAndroid = $sendToIOS = $sendToWP = [];
32
33
        // Si le mode d'envoi est direct, on envoie aux utilisateurs qui ont
34
        // enregistré un ou plusieurs Devices
35
        if ($notification->getMode() == 'to') {
36
            foreach ($notification->getRecipients() as $user) {
37
                // Si l'utilisateur a indiqué ne pas vouloir recevoir la notification
38
                if (!$user->getPreferences()[$notification->getReason()])
39
                    continue;
40
41
                foreach ($user->getDevices() as $device) {
42 View Code Duplication
                    if ($device->getType() == 'Android')
43
                        $sendToAndroid[] = $device;
44
                    else if ($device->getType() == 'iOS')
45
                        $sendToIOS[] = $device;
46
                    else if ($device->getType() == 'WP')
47
                        $sendToWP[] = $device;
48
                }
49
            }
50
        }
51
52
        // Si on est en mode exclusion, on parcourt les Devices enregistrés
53
        // et on envoie à ceux qui ne sont pas dans la liste d'exclusion
54
        if ($notification->getMode() == 'exclude') {
55
            $list = $notification->getRecipients();
56
            foreach ($devices as $device) {
57
                if (!$list->contains($device->getOwner())) {
58
                    // Si l'utilisateur a indiqué ne pas vouloir recevoir la notification
59
                    if (!$device->getOwner()->getPreferences()[$notification->getReason()])
60
                        continue;
61
62 View Code Duplication
                    if ($device->getType() == 'Android')
63
                        $sendToAndroid[] = $device->getDevice();
64
                    else if ($device->getType() == 'iOS')
65
                        $sendToIOS[] = $device->getDevice();
66
                    else if ($device->getType() == 'WP')
67
                        $sendToWP[] = $device->getDevice();
68
                }
69
            }
70
        }
71
72
        // Maintenant qu'on a la liste des appareils auxquels envoyer, on envoie
73
        $this->pushAndroid($notification, $sendToAndroid);
74
        $this->pushIOS($notification, $sendToIOS);
75
76
        // Microsoft étant d'immense débiles, on ne peut pas envoyer à plein de
77
        // destinataires en une fois. On est donc obligé de faire autant de
78
        // requêtes vers l'extérieur que de destinataires.
79
        // À corriger quand ils feront preuve d'intelligence chez Microsoft.
80
        foreach ($sendToWP as $device) {
81
            //pushWP
82
        }
83
    }
84
85
    public function pushAndroid(Notification $notification, array $to)
86
    {
87
        $message = [
88
            'title'     => $notification->getTitle(),
89
            'message'   => $notification->getMessage(),
90
            'vibrate'   => 1,
91
            'sound'     => 1,
92
        ];
93
94
        $fields = [
95
            'registration_ids'     => $to,
96
            'data'                => $message
97
        ];
98
99
        $headers = [
100
            'Authorization: key='.$this->gcmKey,
101
            'Content-Type: application/json'
102
        ];
103
104
        $this->curlService->curl('https://android.googleapis.com/gcm/send', null, [
105
            CURLOPT_HEADER     => true,
106
            CURLOPT_POST       => true,
107
            CURLOPT_HTTPHEADER => $headers,
108
            CURLOPT_POSTFIELDS => json_encode($fields)
109
        ]);
110
    }
111
112
    public function pushIOS(Notification $notification, array $to)
0 ignored issues
show
Unused Code introduced by
The parameter $notification is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $to is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
115
    }
116
117
    public function pushWP(Notification $notification, Device $device)
118
    {
119
        $message = '<?xml version="1.0" encoding="utf-8"?>'.
120
                    '<wp:Notification xmlns:wp="WPNotification">'.
121
                    '<wp:Toast>'.
122
                    '<wp:Text1>uPont</wp:Text1>'.
123
                    '<wp:Text2>'.htmlspecialchars($notification->getTitle()).'</wp:Text2>'.
124
                    '</wp:Toast>'.
125
                    '</wp:Notification>';
126
127
        $headers = [
128
            'Content-Type: text/xml',
129
            'Accept: application/*',
130
            'X-NotificationClass: 0',
131
            'X-WindowsPhone-Target:toast'
132
        ];
133
134
        $this->curlService->curl($device->getDevice(), null, [
135
            CURLOPT_HEADER     => true,
136
            CURLOPT_POST       => true,
137
            CURLOPT_HTTPHEADER => $headers,
138
            CURLOPT_POSTFIELDS => $message
139
        ]);
140
    }
141
}
142