Completed
Push — master ( 40fe4a...13aa34 )
by dan
01:54
created

BaseFormatter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 61
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setTemplating() 0 4 1
A setDispatcher() 0 4 1
A renderTwigTemplate() 0 10 1
A format() 0 13 3
A createMessage() 0 10 1
A createMessagaData() 0 9 3
1
<?php
2
3
namespace IrishDan\NotificationBundle\Formatter;
4
5
use IrishDan\NotificationBundle\Message\Message;
6
use IrishDan\NotificationBundle\Notification\NotificationInterface;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
9
abstract class BaseFormatter
10
{
11
    protected $twig;
12
    protected $eventDispatcher;
13
14
    public function setTemplating(\Twig_Environment $twig)
15
    {
16
        $this->twig = $twig;
17
    }
18
19
    public function setDispatcher(EventDispatcherInterface $eventDispatcher)
20
    {
21
        $this->eventDispatcher = $eventDispatcher;
22
    }
23
24
    protected function renderTwigTemplate($data, $user, $template)
25
    {
26
        return $this->twig->render(
27
            $template,
28
            [
29
                'data' => $data,
30
                'user' => $user,
31
            ]
32
        );
33
    }
34
35
    public function format(NotificationInterface $notification)
36
    {
37
        if (!empty($this->twig) && $notification->getTemplate()) {
38
            $data         = $notification->getDataArray();
39
            $data['body'] = $this->renderTwigTemplate(
40
                $data,
41
                $notification->getNotifiable(),
42
                $notification->getTemplate()
43
            );
44
45
            $notification->setDataArray($data);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface IrishDan\NotificationBun...n\NotificationInterface as the method setDataArray() does only exist in the following implementations of said interface: IrishDan\NotificationBun...cation\TestNotification.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
46
        }
47
    }
48
49
    static protected function createMessage($dispatchData, $messageData, $channel = 'default')
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
50
    {
51
        $message = new Message();
52
53
        $message->setChannel($channel);
54
        $message->setDispatchData($dispatchData);
55
        $message->setMessageData($messageData);
56
57
        return $message;
58
    }
59
60
    static protected function createMessagaData(array $notificationData)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
61
    {
62
        // Build the message data array.
63
        $messageData          = [];
64
        $messageData['body']  = empty($notificationData['body']) ? '' : $notificationData['body'];
65
        $messageData['title'] = empty($notificationData['title']) ? '' : $notificationData['title'];
66
67
        return $messageData;
68
    }
69
}