1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Jean Silva <[email protected]> |
4
|
|
|
* @license MIT |
5
|
|
|
*/ |
6
|
|
|
namespace Jeancsil\FlightSpy\Notifier\Slack; |
7
|
|
|
|
8
|
|
|
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters; |
9
|
|
|
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchWriterTrait; |
10
|
|
|
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchRequester; |
11
|
|
|
use Jeancsil\FlightSpy\Notifier\Deal; |
12
|
|
|
use Jeancsil\FlightSpy\Notifier\NotifiableInterface; |
13
|
|
|
use Maknz\Slack\Client; |
14
|
|
|
|
15
|
|
|
class Notifier implements NotifiableInterface |
16
|
|
|
{ |
17
|
|
|
use ElasticSearchWriterTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ElasticSearchRequester |
21
|
|
|
*/ |
22
|
|
|
private $elasticSearchRequester; |
23
|
|
|
/** |
24
|
|
|
* @var Client |
25
|
|
|
*/ |
26
|
|
|
private $slackClient; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $slackUserName; |
31
|
|
|
|
32
|
|
|
public function __construct(Client $slackClient, $slackUserName) |
33
|
|
|
{ |
34
|
|
|
$this->slackClient = $slackClient; |
|
|
|
|
35
|
|
|
$this->slackUserName = $slackUserName; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @inheritdoc */ |
39
|
|
|
public function notify(array $deals, SessionParameters $sessionParameters) |
40
|
|
|
{ |
41
|
|
|
$notifications = $this->createNotifications($sessionParameters, $deals); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string $identifier |
45
|
|
|
* @var SlackNotification $notification |
46
|
|
|
*/ |
47
|
|
|
foreach ($notifications as $identifier => $notification) { |
48
|
|
|
$this->slackClient |
49
|
|
|
->createMessage() |
50
|
|
|
->send($notification->message); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->elasticSearchWriter |
53
|
|
|
->writeOne([ |
54
|
|
|
'identifier' => $identifier, |
55
|
|
|
'notified' => $notification->to |
|
|
|
|
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** @inheritdoc */ |
61
|
|
|
public function wasNotified(Deal $deal, $notifyTo) |
62
|
|
|
{ |
63
|
|
|
return $this->elasticSearchRequester |
64
|
|
|
->wasNotified( |
65
|
|
|
$deal->getIdentifier(), |
66
|
|
|
$notifyTo |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function createNotifications(SessionParameters $parameters, array $deals = []) |
72
|
|
|
{ |
73
|
|
|
$notifications = []; |
74
|
|
|
/** @var Deal $deal */ |
75
|
|
|
foreach ($deals as $deal) { |
76
|
|
|
$to = "@{$this->slackUserName}"; |
|
|
|
|
77
|
|
|
|
78
|
|
|
if ($this->wasNotified($deal, $to)) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$notification = new SlackNotification(); |
|
|
|
|
83
|
|
|
$notification->to = $to; |
84
|
|
|
$message = $deal->getAgentName() . ' '; |
|
|
|
|
85
|
|
|
$message.= $parameters->currency . ' *' . number_format($deal->getPrice()) . '* '; |
|
|
|
|
86
|
|
|
|
87
|
|
|
if ($deal->getDeepLinkUrl()) { |
88
|
|
|
$message .= PHP_EOL . "<{$deal->getDeepLinkUrl()}|Deep Link>"; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$notification->message = $message; |
|
|
|
|
92
|
|
|
$notifications[$deal->getIdentifier()] = $notification; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $notifications; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param ElasticSearchRequester $elasticSearchRequester |
100
|
|
|
*/ |
101
|
|
|
public function setElasticSearchRequester(ElasticSearchRequester $elasticSearchRequester) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$this->elasticSearchRequester = $elasticSearchRequester; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.