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(), |
|
|
|
|
78
|
|
|
'datePeriod' => $notification->getPeriod(), |
|
|
|
|
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
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: