Completed
Push — master ( df9f00...ccd20f )
by dan
01:37
created

EventChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace IrishDan\NotificationBundle\Channel;
4
5
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface;
6
use IrishDan\NotificationBundle\Event\MessageCreatedEvent;
7
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
8
use IrishDan\NotificationBundle\Exception\MessageFormatException;
9
use IrishDan\NotificationBundle\Formatter\MessageFormatterInterface;
10
use IrishDan\NotificationBundle\Message\MessageInterface;
11
use IrishDan\NotificationBundle\Notification\NotificationInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
/**
15
 * Class DefaultChannel
16
 *
17
 * @package NotificationBundle\Channel
18
 */
19
class EventChannel implements ChannelInterface
20
{
21
    private $formatter;
22
    private $dispatchers = [];
23
    private $eventDispatcher;
24
25
    public function setDispatchers($dispatcherKey, MessageDispatcherInterface $dispatcher)
26
    {
27
        $this->dispatchers[$dispatcherKey] = $dispatcher;
28
    }
29
30
    public function setDataFormatter(MessageFormatterInterface $formatter)
31
    {
32
        $this->formatter = $formatter;
33
    }
34
35
    public function __construct($configured = false, $channel = 'default', EventDispatcherInterface $eventDispatcher)
36
    {
37
        $this->configured      = $configured;
0 ignored issues
show
Bug introduced by
The property configured does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
        $this->channel         = $channel;
0 ignored issues
show
Bug introduced by
The property channel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
        $this->eventDispatcher = $eventDispatcher;
40
    }
41
42
    public function format(NotificationInterface $notification)
43
    {
44
        try {
45
            // Do the formatting.
46
            $message = $this->formatter->format($notification);
47
        } catch (\Exception $e) {
48
            throw new MessageFormatException();
49
        }
50
51
        // Dispatch the message event
52
        $messageEvent = new MessageCreatedEvent($message);
53
        $this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent);
0 ignored issues
show
Documentation introduced by
$messageEvent is of type object<IrishDan\Notifica...nt\MessageCreatedEvent>, but the function expects a null|object<Symfony\Comp...\EventDispatcher\Event>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
    }
55
56
    public function dispatchFromEvent(MessageCreatedEvent $event)
57
    {
58
        $message = $event->getMessage();
59
        $this->dispatch($message);
60
    }
61
62
    public function dispatch(MessageInterface $message)
63
    {
64
        // @TODO: need to figure out which channel message was supposed to be dispatched on.
65
        $dispatcherKey = $message->getChannel();
0 ignored issues
show
Unused Code introduced by
$dispatcherKey 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...
66
67
        // Dispatch the message
68
        try {
0 ignored issues
show
Unused Code introduced by
This try statement is empty and can be removed.

This check looks for try blocks that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

If there is nothing in the try then the catch block can never be executed either. Thus, these try statements can be removed completely.

Loading history...
69
70
        } catch (\Exception $exception) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $excep...eDispatchException(); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
71
            throw new MessageDispatchException();
72
        }
73
    }
74
}
75