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

Channel   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 36.73 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 36
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setDispatchAsEvent() 0 4 1
A setFormatAsEvent() 0 4 1
A setEventDispatcher() 0 4 1
A formatAndDispatch() 0 26 3
A format() 18 18 3
A dispatch() 18 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace IrishDan\NotificationBundle\Channel;
4
5
use IrishDan\NotificationBundle\Event\MessageCreatedEvent;
6
use IrishDan\NotificationBundle\Event\MessageDispatchedEvent;
7
use IrishDan\NotificationBundle\Event\NotificationReadyToFormatEvent;
8
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
9
use IrishDan\NotificationBundle\Exception\MessageFormatException;
10
use IrishDan\NotificationBundle\Message\MessageInterface;
11
use IrishDan\NotificationBundle\Notification\NotificationInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
15
/**
16
 * Class Channel
17
 *
18
 * @package NotificationBundle\Channel
19
 */
20
class Channel extends BaseChannel implements ChannelInterface
21
{
22
    /**
23
     * @var
24
     */
25
    protected $eventDispatcher;
26
    protected $formatToEvent = false;
27
    protected $dispatchToEvent = false;
28
29
    /**
30
     * @param $dispatchToEvent
31
     */
32
    public function setDispatchAsEvent(bool $dispatchToEvent): void
33
    {
34
        $this->dispatchToEvent = $dispatchToEvent;
35
    }
36
37
    /**
38
     * @param $dispatchToEvent
39
     */
40
    public function setFormatAsEvent(bool $formatToEvent): void
41
    {
42
        $this->formatToEvent = $formatToEvent;
43
    }
44
45
    /**
46
     * @param EventDispatcherInterface $eventDispatcher
47
     */
48
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
49
    {
50
        $this->eventDispatcher = $eventDispatcher;
51
    }
52
53
    public function formatAndDispatch(NotificationInterface $notification, $dispatchReadyEvent = true)
54
    {
55
        // Channels can be configured to format and dispatch either directly..
56
        // or via events..
57
        // Using events allows for the hooking into the formatting and dispatching..
58
        // process, perhaps t offload these process to a queue worker.
59
        if ($this->dispatchToEvent && $dispatchReadyEvent) {
60
            $readyEvent = new NotificationReadyToFormatEvent($notification);
61
            $this->eventDispatcher->dispatch(NotificationReadyToFormatEvent::NAME, $readyEvent);
62
63
            return;
64
        }
65
66
        // Format the message..
67
        // Creates a message object for each recipient
68
        // If twig enabled will use twig to render the message
69
        $message = $this->format($notification);
70
71
        // Dispatch the message..
72
        // Sends the message to the destination..
73
        $this->dispatch($message);
74
75
        $message->setStatus('sent');
76
77
        return $message;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $message; (IrishDan\NotificationBun...essage\MessageInterface) is incompatible with the return type declared by the interface IrishDan\NotificationBun...face::formatAndDispatch of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
78
    }
79
80 View Code Duplication
    public function format(NotificationInterface $notification)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        try {
83
            // Do the formatting.
84
            $message = $this->adapter->format($notification);
85
86
            if (!empty($this->eventDispatcher)) {
87
                $messageEvent = new MessageCreatedEvent($message);
88
                $this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent);
89
            }
90
91
            return $message;
92
        } catch (\Exception $e) {
93
            throw new MessageFormatException(
94
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
95
            );
96
        }
97
    }
98
99 View Code Duplication
    public function dispatch(MessageInterface $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        // Dispatch the message
102
        try {
103
            $sent = $this->adapter->dispatch($message);
104
105
            if ($sent && !empty($this->eventDispatcher)) {
106
                $messageEvent = new MessageDispatchedEvent($message);
107
                $this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $messageEvent);
108
            }
109
110
            return $sent;
111
        } catch (\Exception $e) {
112
            throw new MessageDispatchException(
113
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
114
            );
115
        }
116
    }
117
}
118