|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Jean Silva <[email protected]> |
|
4
|
|
|
* @license MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Jeancsil\FlightSpy\Notifier\Email; |
|
7
|
|
|
|
|
8
|
|
|
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters; |
|
9
|
|
|
use Jeancsil\FlightSpy\Service\Currency\PriceFormatter; |
|
10
|
|
|
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchWriterTrait; |
|
11
|
|
|
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchRequester; |
|
12
|
|
|
use Jeancsil\FlightSpy\Notifier\Deal; |
|
13
|
|
|
use Jeancsil\FlightSpy\Notifier\NotifiableInterface; |
|
14
|
|
|
use Postmark\PostmarkClient; |
|
15
|
|
|
|
|
16
|
|
|
class Notifier implements NotifiableInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use ElasticSearchWriterTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ElasticSearchRequester |
|
22
|
|
|
*/ |
|
23
|
|
|
private $elasticSearchRequester; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var PriceFormatter |
|
26
|
|
|
*/ |
|
27
|
|
|
private $priceFormatter; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var PostmarkClient |
|
30
|
|
|
*/ |
|
31
|
|
|
private $mailer; |
|
32
|
|
|
private $html; |
|
33
|
|
|
private $from; |
|
34
|
|
|
private $to; |
|
35
|
|
|
private $subject; |
|
36
|
|
|
|
|
37
|
|
|
private $tableLines = []; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(PostmarkClient $mailer, $html, $from, $to, $subject) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->mailer = $mailer; |
|
|
|
|
|
|
42
|
|
|
$this->from = $from; |
|
|
|
|
|
|
43
|
|
|
$this->to = $to; |
|
|
|
|
|
|
44
|
|
|
$this->subject = $subject; |
|
45
|
|
|
|
|
46
|
|
|
$this->initializeHtmlTemplate($html); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @inheritdoc */ |
|
50
|
|
|
public function notify(array $deals, SessionParameters $sessionParameters) |
|
51
|
|
|
{ |
|
52
|
|
|
$notifications = $this->createNotifications($sessionParameters, $deals); |
|
53
|
|
|
|
|
54
|
|
|
if (empty($this->tableLines)) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string $identifier |
|
60
|
|
|
* @var EmailNotification $notification |
|
61
|
|
|
*/ |
|
62
|
|
|
foreach ($notifications as $identifier => $notification) { |
|
63
|
|
|
$this->elasticSearchWriter |
|
64
|
|
|
->writeOne([ |
|
65
|
|
|
'identifier' => $identifier, |
|
66
|
|
|
'notified' => $this->to |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->mailer->sendEmail( |
|
71
|
|
|
$this->from, |
|
72
|
|
|
$this->to, |
|
73
|
|
|
$this->subject, |
|
74
|
|
|
str_replace('<!--NewLine-->', implode('', $this->tableLines), $this->html) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** @inheritdoc */ |
|
79
|
|
|
public function wasNotified(Deal $deal, $notifyTo) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->elasticSearchRequester |
|
82
|
|
|
->wasNotified( |
|
83
|
|
|
$deal->getIdentifier(), |
|
84
|
|
|
$notifyTo |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** @inheritdoc */ |
|
90
|
|
|
public function createNotifications(SessionParameters $parameters, array $deals = []) |
|
91
|
|
|
{ |
|
92
|
|
|
$notifications = []; |
|
93
|
|
|
/** @var Deal $deal */ |
|
94
|
|
|
foreach ($deals as $deal) { |
|
95
|
|
|
if ($this->wasNotified($deal, $this->to)) { |
|
96
|
|
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->tableLines[] = $this->createTableLine( |
|
100
|
|
|
$deal->getAgentName(), |
|
101
|
|
|
$this->priceFormatter->format($deal->getPrice(), $parameters->currency), |
|
102
|
|
|
$deal->getDeepLinkUrl() |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$notifications[$deal->getIdentifier()] = new EmailNotification(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $notifications; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param ElasticSearchRequester $elasticSearchRequester |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setElasticSearchRequester(ElasticSearchRequester $elasticSearchRequester) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$this->elasticSearchRequester = $elasticSearchRequester; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param PriceFormatter $priceFormatter |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setPriceFormatter(PriceFormatter $priceFormatter) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->priceFormatter = $priceFormatter; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $htmlTemplate |
|
129
|
|
|
*/ |
|
130
|
|
|
private function initializeHtmlTemplate($htmlTemplate) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->html = file_get_contents($htmlTemplate); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param $agentName |
|
137
|
|
|
* @param $price |
|
138
|
|
|
* @param $deepLink |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
private function createTableLine($agentName, $price, $deepLink) |
|
142
|
|
|
{ |
|
143
|
|
|
$deepLinkHtml = ''; |
|
144
|
|
|
|
|
145
|
|
|
if ($deepLink) { |
|
146
|
|
|
$deepLinkHtml = '<a href="' . $deepLink . '" |
|
147
|
|
|
target="_blank" |
|
148
|
|
|
style="text-decoration:underline;background-color:#ffffff; |
|
149
|
|
|
border:solid 1px #3498db;border-radius:5px; |
|
150
|
|
|
box-sizing:border-box;color:#3498db; |
|
151
|
|
|
cursor:pointer;font-size:12px; |
|
152
|
|
|
margin:0;padding:3px 3px; |
|
153
|
|
|
text-decoration:none;text-transform:capitalize; |
|
154
|
|
|
background-color:#3498db;border-color:#3498db;color:#ffffff;">Book</a>'; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return sprintf(' |
|
158
|
|
|
<tr> |
|
159
|
|
|
<td style="font-family:sans-serif;font-size:14px;vertical-align:top;padding-bottom:15px;">%s</td> |
|
160
|
|
|
<td style="font-family:sans-serif;font-size:14px;vertical-align:top;padding-bottom:15px;">%s</td> |
|
161
|
|
|
<td style="font-family:sans-serif;font-size:14px;vertical-align:top;padding-bottom:15px;">%s</td> |
|
162
|
|
|
</tr>', |
|
163
|
|
|
$agentName, |
|
164
|
|
|
$price, |
|
165
|
|
|
$deepLinkHtml |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.