Completed
Push — master ( e3a73f...40fe4a )
by dan
02:29
created

MailDataFormatter::format()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 9
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Formatter;
4
5
use IrishDan\NotificationBundle\Emailable;
6
use IrishDan\NotificationBundle\Exception\MessageFormatException;
7
use IrishDan\NotificationBundle\Message\Message;
8
use IrishDan\NotificationBundle\Notification\NotificationInterface;
9
10
class MailDataFormatter extends BaseFormatter implements MessageFormatterInterface
11
{
12
    const CHANNEL = 'mail';
13
    private $mailConfiguration;
14
15
    public function __construct(array $mailConfiguration)
16
    {
17
        $this->mailConfiguration = $mailConfiguration;
18
    }
19
20
    public function format(NotificationInterface $notification)
21
    {
22
        $notification->setChannel(self::CHANNEL);
23
        parent::format($notification);
24
25
        // Build the message.
26
        $message          = new Message();
27
        $notificationData = $notification->getDataArray();
28
29
        /** @var Emailable $notifiable */
30
        $notifiable = $notification->getNotifiable();
31
        if (!$notifiable instanceof Emailable) {
32
            throw new MessageFormatException(
33
                'Notifiable must implement Emailable interface in order to format email message'
34
            );
35
        }
36
37
        // Set the channel key
38
        $message->setChannel(self::CHANNEL);
39
40
        // Build the dispatch data array.
41
        $dispatchData = [
42
            'to'   => $notifiable->getEmail(),
43
            'from' => empty($this->mailConfiguration['default_sender']) ? '' : $this->mailConfiguration['default_sender'],
44
        ];
45
46
        // Build the message data array.
47
        $messageData          = [];
48
        $messageData['body']  = empty($notificationData['body']) ? '' : $notificationData['body'];
49
        $messageData['title'] = empty($notificationData['title']) ? '' : $notificationData['title'];
50
51
        $message->setDispatchData($dispatchData);
52
        $message->setMessageData($messageData);
53
54
        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...
55
    }
56
}