Completed
Pull Request — master (#4)
by dan
14:39
created

NotificationChannelSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSubscribedEvents() 0 6 1
A addAdapter() 0 5 1
A formatMessageFromEvent() 0 28 2
1
<?php
2
3
namespace IrishDan\NotificationBundle\Subscriber;
4
5
use IrishDan\NotificationBundle\Adapter\MessageAdapterInterface;
6
use IrishDan\NotificationBundle\Channel\ChannelInterface;
7
use IrishDan\NotificationBundle\Channel\EventChannel;
8
use IrishDan\NotificationBundle\Event\MessageCreatedEvent;
9
use IrishDan\NotificationBundle\Event\MessageDispatchedEvent;
10
use IrishDan\NotificationBundle\Event\NotificationReadyToFormatEvent;
11
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
12
use IrishDan\NotificationBundle\Message\MessageInterface;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
/**
18
 * Class NotificationChannelSubscriber
19
 *
20
 * @package IrishDan\NotificationBundle\Subscriber
21
 */
22
class NotificationChannelSubscriber implements EventSubscriberInterface
23
{
24
    /**
25
     * An array of all available adapters.
26
     *
27
     * @var array
28
     */
29
    private $adapters = [];
30
    private $eventDispatcher;
31
    private $adapterlessChannel;
32
    private $channelConfigMap = [];
33
34
    public function __construct(EventDispatcherInterface $eventDispatcher, ChannelInterface $adapterlessChannel, array $channelConfigMap = [])
35
    {
36
        $this->eventDispatcher = $eventDispatcher;
37
        $this->adapterlessChannel = $adapterlessChannel;
38
        $this->channelConfigMap = $channelConfigMap;
39
    }
40
41
42
    /**
43
     * @return array
44
     */
45
    public static function getSubscribedEvents()
46
    {
47
        return [
48
            NotificationReadyToFormatEvent::NAME => 'formatMessageFromEvent'
49
        ];
50
    }
51
52
    /**
53
     * @param                         $key
54
     * @param MessageAdapterInterface $adapter
55
     * @param array                   $config
56
     */
57
    public function addAdapter($key, MessageAdapterInterface $adapter, array $config = [])
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        $adapter->setChannelName($key);
60
        $this->adapters[$key] = $adapter;
61
    }
62
63
64
    public function formatMessageFromEvent(NotificationReadyToFormatEvent $event)
65
    {
66
        $notification = $event->getNotification();
67
68
        // get the adapter name from the
69
        // $adapterKey = $message->getChannel();
70
        $adapterKey = $this->channelConfigMap[$notification->getChannel()]['adapter'];
71
        $channelConfiguration =  $this->channelConfigMap[$notification->getChannel()]['config'];
72
73
        // Get the adapter for this channel
74
        // and inject it into the adapterless channel
75
        // disable dispatching to events also...
76
        if (!empty($this->adapters[$adapterKey])) {
77
            $adapter = $this->adapters[$adapterKey];
78
79
            // Below could be refactored into a an abstract class for use in a queue worker or else where
80
            $this->adapterlessChannel->setAdapter($adapter);
81
            $this->adapterlessChannel->setChannelConfiguration($channelConfiguration);
82
83
            $message = $this->adapterlessChannel->formatAndDispatch($notification, false);
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
            // @TODO: Event...
85
86
        } else {
87
            throw new MessageDispatchException(
88
                sprintf('No adapter available with key "%s"', $adapterKey)
89
            );
90
        }
91
    }
92
}
93