|
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) |
|
|
|
|
|
|
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
|
|
|
|
The
EntityManagermight 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:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.