PusherMessageAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 22 2
A dispatch() 0 14 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Adapter;
4
5
use IrishDan\NotificationBundle\Message\MessageInterface;
6
use IrishDan\NotificationBundle\Notification\NotificationInterface;
7
use IrishDan\NotificationBundle\PusherableInterface;
8
use IrishDan\NotificationBundle\PusherManager;
9
10
/**
11
 * Class PusherMessageAdapter
12
 *
13
 * @package IrishDan\NotificationBundle\Adapter
14
 */
15
class PusherMessageAdapter extends BaseMessageAdapter implements MessageAdapterInterface
16
{
17
    protected $pusherManager;
18
19
    public function __construct(PusherManager $pusherManager)
20
    {
21
        $this->pusherManager = $pusherManager;
22
    }
23
24
    /**
25
     * Generates a message object
26
     *
27
     * @param NotificationInterface $notification
28
     * @return \IrishDan\NotificationBundle\Message\Message
29
     */
30
    public function format(NotificationInterface $notification)
31
    {
32
        $this->pusherManager->setConfig($this->configuration);
33
34
        parent::format($notification);
35
36
        /** @var PusherableInterface $notifiable */
37
        $notifiable = $notification->getNotifiable();
38
        if (!$notifiable instanceof PusherableInterface) {
39
            $this->createFormatterException(PusherableInterface::class, $this->channelName);
40
        }
41
42
        // Build the dispatch data array.
43
        $dispatchData = [
44
            'channel' => $this->pusherManager->getUserChannelName($notifiable),
45
            'event' => $this->pusherManager->getEvent(),
46
        ];
47
48
        $messageData = self::createMessagaData($notification->getDataArray());
49
50
        return self::createMessage($dispatchData, $messageData, $this->channelName);
51
    }
52
53
    public function dispatch(MessageInterface $message)
54
    {
55
        // Get the dispatch and message data from the message.
56
        $dispatchData = $message->getDispatchData();
57
        $messageData = $message->getMessageData();
58
59
        $pusher = $this->pusherManager->getPusherClient();
60
61
        $channel = [
62
            $dispatchData['channel'],
63
        ];
64
65
        return !empty($pusher->trigger($channel, $dispatchData['event'], $messageData));
66
    }
67
}