Completed
Pull Request — master (#15)
by Yann
02:46
created

MobileChannel::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
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
    /**
31
     * @param PushManager        $pushManager
32
     * @param AdapterInterface[] $adapters
33
     */
34 4
    public function __construct(PushManager $pushManager, array $adapters)
35
    {
36 4
        $this->pushManager = $pushManager;
37 4
        $this->adapters = $adapters;
38 4
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43 3
    public function supports($recipient)
44
    {
45 3
        return $recipient instanceof MobileRecipientInterface;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function configure(OptionsResolver $resolver)
52
    {
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58 1
    public function handle(Delivery $delivery)
59
    {
60
        /** @var $recipient MobileRecipientInterface */
61 1
        $recipient = $delivery->getRecipient();
62
63 1
        $message = new Message($delivery->getSubject());
64
65 1
        foreach ($this->adapters as $adapter) {
66 1
            $devices = new DeviceCollection();
67
68 1
            foreach ($recipient->getDevicesTokens() as $token) {
69 1
                if ($adapter->supports($token)) {
70 1
                    $devices->add(new Device($token));
71
                }
72
            }
73
74 1
            $push = new Push($adapter, $devices, $message);
75
76 1
            $this->pushManager->add($push);
77
        }
78
79 1
        $this->pushManager->push();
80 1
    }
81
}
82