MissedBookingEmailNotificationHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A shouldHandle() 0 5 2
A handle() 0 23 2
1
<?php
2
3
namespace JhFlexiTime\NotificationHandler;
4
5
use AcMailer\Service\MailServiceInterface;
6
use JhFlexiTime\Notification\MissingBookingsNotification;
7
use JhHubBase\Notification\NotificationHandlerInterface;
8
use JhHubBase\Notification\NotificationInterface;
9
use JhHubBase\Options\ModuleOptions;
10
use rfreebern\Giphy;
11
use Zend\View\Model\ViewModel;
12
use ZfcUser\Entity\UserInterface;
13
14
/**
15
 * Class MissedBookingEmailNotificationHandler
16
 * @package JhHub\Notification
17
 * @author  Aydin Hassan <[email protected]>
18
 */
19
class MissedBookingEmailNotificationHandler implements NotificationHandlerInterface
20
{
21
22
    /**
23
     * @var MailServiceInterface
24
     */
25
    protected $mailService;
26
27
    /**
28
     * @var ModuleOptions
29
     */
30
    protected $options;
31
32
    /**
33
     * @var Giphy
34
     */
35
    protected $giphyApi;
36
37
    /**
38
     * @param MailServiceInterface $mailService
39
     * @param ModuleOptions        $options
40
     * @param Giphy                $giphyApi
41
     */
42
    public function __construct(MailServiceInterface $mailService, ModuleOptions $options, Giphy $giphyApi)
43
    {
44
        $this->mailService  = $mailService;
45
        $this->options      = $options;
46
        $this->giphyApi     = $giphyApi;
47
    }
48
49
    /**
50
     * @param NotificationInterface $notification
51
     *
52
     * @return bool
53
     */
54
    public function shouldHandle(NotificationInterface $notification)
55
    {
56
        return 'missing-bookings' === $notification->getName()
57
            && $notification instanceof MissingBookingsNotification;
58
    }
59
60
    /**
61
     * @param NotificationInterface $notification
62
     * @param UserInterface         $user
63
     */
64
    public function handle(NotificationInterface $notification, UserInterface $user)
65
    {
66
        $this->mailService->setSubject('Missing Bookings');
67
        $this->mailService->getMessage()->setTo([$user->getEmail()]);
68
69
        $randomGifUrl   = '';
70
        $randomGifData  = $this->giphyApi->random('fail');
71
        if ($randomGifData instanceof \stdClass) {
72
            $randomGifUrl = $randomGifData->data->image_original_url;
73
        }
74
75
        $model = new ViewModel(array(
76
            'user'          => $user,
77
            'missedDates'   => $notification->getMissingBookings(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface JhHubBase\Notification\NotificationInterface as the method getMissingBookings() does only exist in the following implementations of said interface: JhFlexiTime\Notification...ingBookingsNotification.

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...
78
            'datePeriod'    => $notification->getPeriod(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface JhHubBase\Notification\NotificationInterface as the method getPeriod() does only exist in the following implementations of said interface: JhFlexiTime\Notification...ingBookingsNotification.

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...
79
            'appUrl'        => $this->options->getAppUrl(),
80
            'randomFailGif' => $randomGifUrl,
81
        ));
82
        $model->setTemplate('jh-flexi-time/emails/missed-bookings');
83
84
        $this->mailService->setTemplate($model);
85
        $this->mailService->send();
86
    }
87
}
88