NotifyService   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 93
Duplicated Lines 30.11 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 5
dl 28
loc 93
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B notify() 0 29 7
B pushToCloud() 28 45 11

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 KI\UserBundle\Service;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use KI\UserBundle\Entity\Notification;
8
use KI\UserBundle\Service\GcmService;
9
10
class NotifyService
11
{
12
    protected $gcmService;
13
    protected $manager;
14
    protected $deviceRepository;
15
    protected $userRepository;
16
17
    public function __construct(GcmService $gcmService, EntityManager $manager, EntityRepository $deviceRepository, EntityRepository $userRepository)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $manager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
18
    {
19
        $this->gcmService       = $gcmService;
20
        $this->manager          = $manager;
21
        $this->deviceRepository = $deviceRepository;
22
        $this->userRepository   = $userRepository;
23
    }
24
25
    // Persiste des objets notification qui seront retrievables
26
    public function notify($reason, $title, $message, $mode = 'to', $recipient = [], $resource = '')
27
    {
28
        $notification = new Notification($reason, $title, $message, $mode, $resource);
29
30
        if ($mode == 'to') {
31
            if (is_array($recipient)) {
32
                foreach ($recipient as $user) {
33
                    $notification->addRecipient($user);
34
                }
35
            } else {
36
                $notification->addRecipient($recipient);
37
            }
38
        } else if ($mode == 'exclude') {
39
            $users = $this->userRepository->findAll();
40
41
            foreach ($users as $user) {
42
                if (!in_array($user, $recipient)) {
43
                    $notification->addRecipient($user);
44
                }
45
            }
46
        }
47
48
        // On stocke la notif pour usage ultérieur
49
        $this->manager->persist($notification);
50
        $this->manager->flush();
51
52
        // On la pousse vers les téléphones
53
        $this->pushToCloud($notification);
54
    }
55
56
    // Envoie une notif vers les divers services de messagerie
57
    private function pushToCloud(Notification $notification)
58
    {
59
        $devices = $this->deviceRepository->findAll();
60
        $sendToAndroid = [];
61
62
        // Si le mode d'envoi est direct, on envoie aux utilisateurs qui ont
63
        // enregistré un ou plusieurs Devices
64
        if ($notification->getMode() == 'to') {
65 View Code Duplication
            foreach ($notification->getRecipients() as $user) {
66
                // Si l'utilisateur a indiqué ne pas vouloir recevoir la notification
67
                if (!$user->getPreferences()[$notification->getReason()]) {
68
                    continue;
69
                }
70
71
                foreach ($user->getDevices() as $device) {
72
                    switch ($device->getType()) {
73
                    case 'Android':
74
                        $sendToAndroid[] = $device->getDevice();
75
                        break;
76
                    }
77
                }
78
            }
79
        }
80
81
        // Si on est en mode exclusion, on parcourt les Devices enregistrés
82
        // et on envoie à ceux qui ne sont pas dans la liste d'exclusion
83
        if ($notification->getMode() == 'exclude') {
84
            $list = $notification->getRecipients();
85 View Code Duplication
            foreach ($devices as $device) {
86
                if (!$list->contains($device->getOwner())) {
87
                    // Si l'utilisateur a indiqué ne pas vouloir recevoir la notification
88
                    if (!$device->getOwner()->getPreferences()[$notification->getReason()]) {
89
                        continue;
90
                    }
91
92
                    switch ($device->getType()) {
93
                    case 'Android':
94
                        $sendToAndroid[] = $device->getDevice();
95
                        break;
96
                    }
97
                }
98
            }
99
        }
100
        $this->gcmService->push($notification, $sendToAndroid);
101
    }
102
}
103