Completed
Push — master ( 13aa34...cd8958 )
by dan
02:19
created

PusherDataFormatter::format()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Formatter;
4
5
use IrishDan\NotificationBundle\Exception\MessageFormatException;
6
use IrishDan\NotificationBundle\Notification\NotificationInterface;
7
use IrishDan\NotificationBundle\Pusherable;
8
use IrishDan\NotificationBundle\PusherManager;
9
10
class PusherDataFormatter extends BaseFormatter implements MessageFormatterInterface
11
{
12
    const CHANNEL = 'pusher';
13
    protected $pusherConfiguration;
14
    protected $pusherManager;
15
16
    public function __construct(PusherManager $pusherManager)
17
    {
18
        $this->pusherManager = $pusherManager;
19
    }
20
21
    public function format(NotificationInterface $notification)
22
    {
23
        $notification->setChannel(self::CHANNEL);
24
        parent::format($notification);
25
26
        /** @var Pusherable $notifiable */
27
        $notifiable = $notification->getNotifiable();
28
        if (!$notifiable instanceof Pusherable) {
29
            throw new MessageFormatException(
30
                'Notifiable must implement Pusherable interface in order to format email message'
31
            );
32
        }
33
34
        // Build the dispatch data array.
35
        $dispatchData = [
36
            'channel' => $this->pusherManager->getUserChannelName($notifiable),
37
            'event'   => $this->pusherManager->getEvent(),
38
        ];
39
40
        $messageData = self::createMessagaData($notification->getDataArray());
41
        $message     = self::createMessage($dispatchData, $messageData, self::CHANNEL);
42
43
        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...
44
    }
45
}