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

DefaultChannel::format()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Channel;
4
5
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface;
6
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
7
use IrishDan\NotificationBundle\Exception\MessageFormatException;
8
use IrishDan\NotificationBundle\Formatter\MessageFormatterInterface;
9
use IrishDan\NotificationBundle\Message\MessageInterface;
10
use IrishDan\NotificationBundle\Notification\NotificationInterface;
11
12
13
/**
14
 * Class DefaultChannel
15
 *
16
 * @package NotificationBundle\Channel
17
 */
18
class DefaultChannel implements ChannelInterface
19
{
20
    private $formatter;
21
    private $dispatcher;
22
23
    public function setDispatcher(MessageDispatcherInterface $dispatcher)
24
    {
25
        $this->dispatcher = $dispatcher;
26
    }
27
28
    public function setDataFormatter(MessageFormatterInterface $formatter)
29
    {
30
        $this->formatter = $formatter;
31
    }
32
33
    public function __construct($configured = false, $channel = 'default')
34
    {
35
        $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...
36
        $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...
37
    }
38
39
    public function format(NotificationInterface $notification)
40
    {
41
        try {
42
            // Do the formatting.
43
            $message = $this->formatter->format($notification);
44
            var_dump($message);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($message); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
45
46
            return $message;
47
        } catch (\Exception $e) {
48
            throw new MessageFormatException(
49
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
50
            );
51
        }
52
53
        // Dispatch the message
54
        // $this->dispatch($message);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
    }
56
57
    public function dispatch(MessageInterface $message)
58
    {
59
        // Dispatch the message
60
        try {
61
            return $this->dispatcher->dispatch($message);
62
        } catch (\Exception $e) {
63
            throw new MessageDispatchException(
64
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
65
            );
66
        }
67
    }
68
}
69