MobileChannel::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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\NotificationRecipientInterface;
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 5
    public function __construct(PushManager $pushManager, array $adapters)
35
    {
36 5
        $this->pushManager = $pushManager;
37 5
        $this->adapters = $adapters;
38 5
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 4
    public function supports($recipient)
44
    {
45 4
        return $recipient instanceof NotificationRecipientInterface;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 1
    public function configure(OptionsResolver $resolver)
52
    {
53
        $resolver
54 1
            ->setDefault('mobile_data', [])
55
        ;
56 1
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 1
    public function handle(Delivery $delivery)
62
    {
63
        /** @var $recipient NotificationRecipientInterface */
64 1
        $recipient = $delivery->getRecipient();
65
66 1
        $options = $delivery->getOptions();
67 1
        $options = array_intersect_key($delivery->getParameters(), array_flip($options['mobile_data']));
68
69 1
        $message = new Message($delivery->getSubject(), $options);
70
71 1
        foreach ($this->adapters as $adapter) {
72 1
            $devices = new DeviceCollection();
73
74 1
            foreach ($recipient->getDevicesTokens() as $token) {
75 1
                if ($adapter->supports($token)) {
76 1
                    $devices->add(new Device($token));
77
                }
78
            }
79
80 1
            $push = new Push($adapter, $devices, $message);
81
82 1
            $this->pushManager->add($push);
83
        }
84
85 1
        $this->pushManager->push();
86 1
    }
87
}
88