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

NexmoDataFormatter::format()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Formatter;
4
5
use IrishDan\NotificationBundle\Message\Message;
6
use IrishDan\NotificationBundle\Notification\NotificationInterface;
7
use IrishDan\NotificationBundle\Textable;
8
9
class NexmoDataFormatter extends BaseFormatter implements MessageFormatterInterface
10
{
11
    protected $nexmoConfiguration;
12
13
    public function __construct(array $nexmoConfiguration = [])
14
    {
15
        $this->nexmoConfiguration = $nexmoConfiguration;
16
    }
17
18
    public function format(NotificationInterface $notification)
19
    {
20
        $message          = new Message();
21
        $notificationData = $notification->getDataArray();
0 ignored issues
show
Unused Code introduced by
$notificationData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
23
        // The User/Notifiable must implement Textable interface in order to receive SMSs
24
        $notifiable = $notification->getNotifiable();
25
        if (!$notifiable instanceof Textable) {
26
            throw new \RuntimeException('Notifiable does not implement Texable interface');
27
        }
28
29
        // Build the dispatch data array.
30
        $dispatchData = [
31
            'to'   => $notifiable->getNumber(),
32
            'from' => empty($this->nexmoConfiguration['from']) ? '' : $this->nexmoConfiguration['from'],
33
        ];
34
35
        // Build the message data array.
36
        $messageData = [];
37
        // @TODO: Works but not when body is dynamic??
38
        $messageData['body'] = 'A Hoi hoi! Marcus!';
39
        // if (!empty($this->twig) && $notification->getTemplate()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
40
        //     $messageData['body'] = $this->renderTwigTemplate(
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
41
        //         $notificationData,
42
        //         $notifiable,
43
        //         $notification->getTemplate()
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
44
        //     );
45
        // }
46
47
        $message->setDispatchData($dispatchData);
48
        $message->setMessageData($messageData);
49
50
        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...
51
    }
52
}