Completed
Push — master ( 366e1c...39d38f )
by Yann
06:12
created

MobileChannel::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
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