|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\MessengerBundle\Channel; |
|
4
|
|
|
|
|
5
|
|
|
use Sly\NotificationPusher\Adapter\AdapterInterface; |
|
6
|
|
|
use Sly\NotificationPusher\Collection\DeviceCollection; |
|
7
|
|
|
use Sly\NotificationPusher\Model\Device; |
|
8
|
|
|
use Sly\NotificationPusher\Model\Message; |
|
9
|
|
|
use Sly\NotificationPusher\Model\Push; |
|
10
|
|
|
use Sly\NotificationPusher\PushManager; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
use Yokai\MessengerBundle\Delivery; |
|
13
|
|
|
use Yokai\MessengerBundle\Recipient\MobileRecipientInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Yann Eugoné <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class MobileChannel implements ChannelInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var PushManager |
|
22
|
|
|
*/ |
|
23
|
|
|
private $pushManager; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var AdapterInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $adapters; |
|
29
|
|
|
/** |
|
30
|
|
|
* @param PushManager $pushManager |
|
31
|
|
|
* @param AdapterInterface[] $adapters |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(PushManager $pushManager, array $adapters) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->pushManager = $pushManager; |
|
36
|
|
|
$this->adapters = $adapters; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritDoc |
|
41
|
|
|
*/ |
|
42
|
|
|
public function supports($recipient) |
|
43
|
|
|
{ |
|
44
|
|
|
return $recipient instanceof MobileRecipientInterface; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritDoc |
|
49
|
|
|
*/ |
|
50
|
|
|
public function configure(OptionsResolver $resolver) |
|
51
|
|
|
{ |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritDoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function handle(Delivery $delivery) |
|
58
|
|
|
{ |
|
59
|
|
|
/** @var $recipient MobileRecipientInterface */ |
|
60
|
|
|
$recipient = $delivery->getRecipient(); |
|
61
|
|
|
|
|
62
|
|
|
$message = new Message($delivery->getSubject()); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->adapters as $adapter) { |
|
65
|
|
|
$devices = new DeviceCollection(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($recipient->getDevicesTokens() as $token) { |
|
68
|
|
|
if ($adapter->supports($token)) { |
|
69
|
|
|
$devices->add(new Device($token)); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$push = new Push($adapter, $devices, $message); |
|
74
|
|
|
|
|
75
|
|
|
$this->pushManager->add($push); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->pushManager->push(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|