Completed
Push — master ( 0e7751...1494d2 )
by dan
02:09
created

DatabaseMessageAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 5
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 23 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
    const CHANNEL = 'database';
14
15
    protected $databaseNotificationManager;
16
17
    public function __construct(DatabaseNotificationManager $databaseNotificationManager)
18
    {
19
        $this->databaseNotificationManager = $databaseNotificationManager;
20
    }
21
22
    /**
23
     * Generates a message object
24
     *
25
     * @param NotificationInterface $notification
26
     * @return \IrishDan\NotificationBundle\Message\Message
27
     */
28
    public function format(NotificationInterface $notification)
29
    {
30
        $notification->setChannel(self::CHANNEL);
31
        parent::format($notification);
32
33
        /** @var DatabaseNotifiableInterface $notifiable */
34
        $notifiable = $notification->getNotifiable();
35
        if (!$notifiable instanceof DatabaseNotifiableInterface) {
36
            $this->createFormatterException(DatabaseNotifiableInterface::class, self::CHANNEL);
37
        }
38
39
        // Build the dispatch data array.
40
        $dispatchData = [
41
            'id' => $notifiable->getIdentifier(),
42
            'notifiable' => $notifiable,
43
            'uuid' => $notification->getUuid(),
44
            'type' => get_class($notification),
45
        ];
46
47
        $messageData = self::createMessagaData($notification->getDataArray());
48
49
        return self::createMessage($dispatchData, $messageData, self::CHANNEL);
50
    }
51
52
    public function dispatch(MessageInterface $message)
53
    {
54
        $dispatchData = $message->getDispatchData();
55
        $messageData = $message->getMessageData();
56
        $data = $dispatchData + $messageData;
57
58
        $databaseNotification = $this->databaseNotificationManager->createDatabaseNotification($data);
59
        if ($databaseNotification) {
60
            return true;
61
        }
62
63
        return false;
64
    }
65
}