Completed
Push — master ( 776646...b5fe69 )
by dan
02:02
created

MailDataFormatter::format()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Formatter;
4
5
use IrishDan\NotificationBundle\EmailableInterface;
6
use IrishDan\NotificationBundle\Exception\MessageFormatException;
7
use IrishDan\NotificationBundle\Notification\NotificationInterface;
8
9 View Code Duplication
class MailDataFormatter extends BaseFormatter implements MessageFormatterInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    const CHANNEL = 'mail';
12
    private $mailConfiguration;
13
14
    public function __construct(array $mailConfiguration)
15
    {
16
        $this->mailConfiguration = $mailConfiguration;
17
    }
18
19
    public function format(NotificationInterface $notification)
20
    {
21
        $notification->setChannel(self::CHANNEL);
22
        parent::format($notification);
23
24
        /** @var EmailableInterface $notifiable */
25
        $notifiable = $notification->getNotifiable();
26
        if (!$notifiable instanceof EmailableInterface) {
27
            $this->createFormatterException(EmailableInterface::class, self::CHANNEL);
28
        }
29
30
        // Build the dispatch data array.
31
        $dispatchData = [
32
            'to' => $notifiable->getEmail(),
33
            'from' => empty($this->mailConfiguration['default_sender']) ? '' : $this->mailConfiguration['default_sender'],
34
        ];
35
36
        $messageData = self::createMessagaData($notification->getDataArray());
37
        $message = self::createMessage($dispatchData, $messageData, self::CHANNEL);
38
39
        return $message;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $message; (IrishDan\NotificationBundle\Message\Message) is incompatible with the return type declared by the interface IrishDan\NotificationBun...matterInterface::format of type Nexmo\Message\MessageInterface.

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...
40
    }
41
}