DatabaseMessageAdapter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 5
dl 0
loc 52
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 13 2
1
<?php
2
3
namespace IrishDan\NotificationBundle\Adapter;
4
5
6
use IrishDan\NotificationBundle\DatabaseNotifiableInterface;
7
use IrishDan\NotificationBundle\DatabaseNotificationManager;
8
use IrishDan\NotificationBundle\Message\MessageInterface;
9
use IrishDan\NotificationBundle\Notification\NotificationInterface;
10
11
class DatabaseMessageAdapter extends BaseMessageAdapter implements MessageAdapterInterface
12
{
13
    protected $databaseNotificationManager;
14
15
    public function __construct(DatabaseNotificationManager $databaseNotificationManager)
16
    {
17
        $this->databaseNotificationManager = $databaseNotificationManager;
18
    }
19
20
    /**
21
     * Generates a message object
22
     *
23
     * @param NotificationInterface $notification
24
     * @return \IrishDan\NotificationBundle\Message\Message
25
     */
26
    public function format(NotificationInterface $notification)
27
    {
28
        parent::format($notification);
29
30
        /** @var DatabaseNotifiableInterface $notifiable */
31
        $notifiable = $notification->getNotifiable();
32
        if (!$notifiable instanceof DatabaseNotifiableInterface) {
33
            $this->createFormatterException(DatabaseNotifiableInterface::class, $this->channelName);
34
        }
35
36
        // Build the dispatch data array.
37
        $dispatchData = [
38
            'id' => $notifiable->getIdentifier(),
39
            'notifiable' => $notifiable,
40
            'uuid' => $notification->getUuid(),
41
            'type' => get_class($notification),
42
        ];
43
44
        $messageData = self::createMessagaData($notification->getDataArray());
45
46
        return self::createMessage($dispatchData, $messageData, $this->channelName);
47
    }
48
49
    public function dispatch(MessageInterface $message)
50
    {
51
        $dispatchData = $message->getDispatchData();
52
        $messageData = $message->getMessageData();
53
        $data = $dispatchData + $messageData;
54
55
        $databaseNotification = $this->databaseNotificationManager->createDatabaseNotification($data);
56
        if ($databaseNotification) {
57
            return true;
58
        }
59
60
        return false;
61
    }
62
}