Skip to content

Commit 57b299c

Browse files
Applied PR feedback
1 parent 7017116 commit 57b299c

File tree

2 files changed

+194
-5
lines changed

2 files changed

+194
-5
lines changed

Slack.php

Lines changed: 142 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function registerEvents()
5858
'ScheduledReports.sendReport' => 'sendReport',
5959
'Template.reportParametersScheduledReports' => 'templateReportParametersScheduledReports',
6060
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
61-
'CustomAlerts.validateReportParameters' => 'validateCustomAlertReportParameters',
61+
'CustomAlerts.validateReportParameters' => 'validateCustomAlertReportParameters',
6262
'CustomAlerts.sendNewAlerts' => 'sendNewAlerts',
6363
];
6464
}
@@ -76,6 +76,16 @@ public function getClientSideTranslationKeys(&$translationKeys)
7676
$translationKeys[] = 'Slack_SlackEnterYourSlackChannelIdHelpText';
7777
}
7878

79+
/**
80+
*
81+
* Validates the Schedule Report for Slack reportType
82+
*
83+
* @param $parameters
84+
* @param $reportType
85+
* @return void
86+
* @throws \Piwik\Exception\DI\DependencyException
87+
* @throws \Piwik\Exception\DI\NotFoundException
88+
*/
7989
public function validateReportParameters(&$parameters, $reportType)
8090
{
8191
if (!self::isSlackEvent($reportType)) {
@@ -108,6 +118,15 @@ public function validateReportParameters(&$parameters, $reportType)
108118
}
109119
}
110120

121+
/**
122+
*
123+
* Get report metadata for Slack scheduled report
124+
*
125+
* @param $availableReportMetadata
126+
* @param $reportType
127+
* @param $idSite
128+
* @return void
129+
*/
111130
public function getReportMetadata(&$availableReportMetadata, $reportType, $idSite)
112131
{
113132
if (!self::isSlackEvent($reportType)) {
@@ -121,25 +140,58 @@ public function getReportMetadata(&$availableReportMetadata, $reportType, $idSit
121140
);
122141
}
123142

143+
/**
144+
*
145+
* Adds Slack as a reportType in Schedule Reports
146+
*
147+
* @param $reportTypes
148+
* @return void
149+
*/
124150
public function getReportTypes(&$reportTypes)
125151
{
126152
$reportTypes = array_merge($reportTypes, self::$managedReportTypes);
127153
}
128154

155+
/**
156+
*
157+
* Adds allowed reportTypes for Slack, e.g. PDF, CSV and TSV
158+
*
159+
* @param $reportFormats
160+
* @param $reportType
161+
* @return void
162+
*/
129163
public function getReportFormats(&$reportFormats, $reportType)
130164
{
131165
if (self::isSlackEvent($reportType)) {
132166
$reportFormats = array_merge($reportFormats, self::$managedReportFormats);
133167
}
134168
}
135169

170+
/**
171+
*
172+
* Adds report parameter for Slack, e.g. SlackChannelID
173+
*
174+
* @param $availableParameters
175+
* @param $reportType
176+
* @return void
177+
*/
136178
public function getReportParameters(&$availableParameters, $reportType)
137179
{
138180
if (self::isSlackEvent($reportType)) {
139181
$availableParameters = self::$availableParameters;
140182
}
141183
}
142184

185+
/**
186+
*
187+
* Process the Schedule report for reportType Slack
188+
*
189+
* @param $processedReports
190+
* @param $reportType
191+
* @param $outputType
192+
* @param $report
193+
* @return void
194+
*/
143195
public function processReports(&$processedReports, $reportType, $outputType, $report)
144196
{
145197
if (!self::isSlackEvent($reportType)) {
@@ -153,6 +205,17 @@ public function processReports(&$processedReports, $reportType, $outputType, $re
153205
);
154206
}
155207

208+
/**
209+
*
210+
* Sets the rendered instance based on reportFormat for Slack
211+
*
212+
* @param $reportRenderer
213+
* @param $reportType
214+
* @param $outputType
215+
* @param $report
216+
* @return void
217+
* @throws \Exception
218+
*/
156219
public function getRendererInstance(&$reportRenderer, $reportType, $outputType, $report)
157220
{
158221
if (!self::isSlackEvent($reportType)) {
@@ -164,13 +227,30 @@ public function getRendererInstance(&$reportRenderer, $reportType, $outputType,
164227
$reportRenderer = ReportRenderer::factory($reportFormat);
165228
}
166229

230+
/**
231+
*
232+
* To allow multiple reports in a single file
233+
*
234+
* @param $allowMultipleReports
235+
* @param $reportType
236+
* @return void
237+
*/
167238
public function allowMultipleReports(&$allowMultipleReports, $reportType)
168239
{
169240
if (self::isSlackEvent($reportType)) {
170241
$allowMultipleReports = true;
171242
}
172243
}
173244

245+
/**
246+
*
247+
* Displays the recipients in the list of Schedule Reports
248+
*
249+
* @param $recipients
250+
* @param $reportType
251+
* @param $report
252+
* @return void
253+
*/
174254
public function getReportRecipients(&$recipients, $reportType, $report)
175255
{
176256
if (!self::isSlackEvent($reportType) || empty($report['parameters'][self::SLACK_CHANNEL_ID_PARAMETER])) {
@@ -181,6 +261,9 @@ public function getReportRecipients(&$recipients, $reportType, $report)
181261
}
182262

183263
/**
264+
*
265+
* Code to send a Schedule Report via Slack
266+
*
184267
* @param $reportType
185268
* @param $report
186269
* @param $contents
@@ -189,8 +272,11 @@ public function getReportRecipients(&$recipients, $reportType, $report)
189272
* @param $reportSubject
190273
* @param $reportTitle
191274
* @param $additionalFiles
192-
* @param Period|null $period
275+
* @param $period
193276
* @param $force
277+
* @return void
278+
* @throws \Piwik\Exception\DI\DependencyException
279+
* @throws \Piwik\Exception\DI\NotFoundException
194280
*/
195281
public function sendReport(
196282
$reportType,
@@ -203,7 +289,8 @@ public function sendReport(
203289
$additionalFiles,
204290
$period,
205291
$force
206-
) {
292+
)
293+
{
207294
if (!self::isSlackEvent($reportType)) {
208295
return;
209296
}
@@ -232,6 +319,16 @@ public function sendReport(
232319
$scheduleReportSlack->send();
233320
}
234321

322+
/**
323+
*
324+
* Add the view template for Slack report parameters
325+
*
326+
* @param $out
327+
* @param $context
328+
* @return void
329+
* @throws \Piwik\Exception\DI\DependencyException
330+
* @throws \Piwik\Exception\DI\NotFoundException
331+
*/
235332
public function templateReportParametersScheduledReports(&$out, $context = '')
236333
{
237334
if (Piwik::isUserIsAnonymous()) {
@@ -277,17 +374,35 @@ public function uninstall()
277374
return;
278375
}
279376

377+
/**
378+
*
379+
* Validation check for CustomAlert report parameters
380+
*
381+
* @param $parameters
382+
* @param $alertMedium
383+
* @return void
384+
* @throws \Exception
385+
*/
280386
public function validateCustomAlertReportParameters($parameters, $alertMedium)
281387
{
282388
if ($alertMedium === self::SLACK_TYPE && empty($parameters[self::SLACK_CHANNEL_ID_PARAMETER])) {
283389
throw new \Exception(Piwik::translate('Slack_SlackChannelIdRequiredErrorMessage'));
284390
}
285391
}
286392

393+
/**
394+
*
395+
* Code to send CustomAlerts via Slack
396+
*
397+
* @param $triggeredAlerts
398+
* @return void
399+
* @throws \Piwik\Exception\DI\DependencyException
400+
* @throws \Piwik\Exception\DI\NotFoundException
401+
*/
287402
public function sendNewAlerts($triggeredAlerts): void
288403
{
289404
if (!empty($triggeredAlerts)) {
290-
$enrichTriggerAlerts = new EnrichTriggeredAlerts();
405+
$enrichTriggerAlerts = StaticContainer::get(EnrichTriggeredAlerts::class);
291406
$triggeredAlerts = $enrichTriggerAlerts->enrichTriggeredAlerts($triggeredAlerts);
292407
$settings = StaticContainer::get(SystemSettings::class);
293408
$token = $settings->slackOauthToken->getValue();
@@ -305,6 +420,13 @@ public function sendNewAlerts($triggeredAlerts): void
305420
}
306421
}
307422

423+
/**
424+
*
425+
* Group alerts by slackChannelID to reduce number of network calls for multiple alerts
426+
*
427+
* @param array $alerts
428+
* @return array
429+
*/
308430
private function groupAlertsByChannelId(array $alerts): array
309431
{
310432
$groupedAlerts = [];
@@ -321,11 +443,27 @@ private function groupAlertsByChannelId(array $alerts): array
321443
return $groupedAlerts;
322444
}
323445

446+
/**
447+
*
448+
* Returns the alert message to send via Slack
449+
*
450+
* @param array $alert
451+
* @param string $metric
452+
* @param string $reportName
453+
* @return string
454+
*/
324455
public function getAlertMessage(array $alert, string $metric, string $reportName): string
325456
{
326457
return Piwik::translate('Slack_SlackAlertContent', [$alert['name'], $alert['siteName'], $metric, $reportName, $this->transformAlertCondition($alert)]);
327458
}
328459

460+
/**
461+
*
462+
* Transform the alert condition to text
463+
*
464+
* @param array $alert
465+
* @return string
466+
*/
329467
private function transformAlertCondition(array $alert): string
330468
{
331469
switch ($alert['metric_condition']) {

SlackApi.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class SlackApi
4141
public function __construct(
4242
#[\SensitiveParameter]
4343
string $token
44-
) {
44+
)
45+
{
4546
$this->token = $token;
4647
$this->logger = StaticContainer::get(LoggerInterface::class);
4748
}
@@ -65,6 +66,14 @@ public function uploadFile(string $subject, string $fileName, string $fileConten
6566
return false;
6667
}
6768

69+
/**
70+
*
71+
* Get the URL to upload the file
72+
*
73+
* @param string $fileName
74+
* @param int $fileLength
75+
* @return string
76+
*/
6877
public function getUploadURLExternal(string $fileName, int $fileLength): string
6978
{
7079
try {
@@ -93,6 +102,14 @@ public function getUploadURLExternal(string $fileName, int $fileLength): string
93102
return '';
94103
}
95104

105+
/**
106+
*
107+
* Upload the file contents to the URL received from getUploadURLExternal method
108+
*
109+
* @param string $uploadURL
110+
* @param string $fileContents
111+
* @return bool
112+
*/
96113
public function sendFile(string $uploadURL, string $fileContents): bool
97114
{
98115
try {
@@ -111,6 +128,15 @@ public function sendFile(string $uploadURL, string $fileContents): bool
111128
return strtolower($response) === ('ok - ' . strlen($fileContents));
112129
}
113130

131+
132+
/**
133+
*
134+
* Post the uploaded file to a channel
135+
*
136+
* @param string $channel
137+
* @param string $subject
138+
* @return bool
139+
*/
114140
public function completeUploadExternal(string $channel, string $subject): bool
115141
{
116142
try {
@@ -135,8 +161,21 @@ public function completeUploadExternal(string $channel, string $subject): bool
135161
return !empty($data['ok']);
136162
}
137163

164+
/**
165+
*
166+
* Send a text message to a Slack channel
167+
*
168+
* @param string $message
169+
* @param string $channel
170+
* @return bool
171+
*/
138172
public function sendMessage(string $message, string $channel): bool
139173
{
174+
if (empty($message) || empty($channel)) {
175+
$this->logger->debug('Empty message or channel for sending message');
176+
return false;
177+
}
178+
140179
try {
141180
$response = $this->sendHttpRequest(
142181
self::SLACK_POST_MESSAGE_URL,
@@ -158,6 +197,18 @@ public function sendMessage(string $message, string $channel): bool
158197
return !empty($data['ok']);
159198
}
160199

200+
/**
201+
*
202+
* Wrapper to send HTTP request
203+
*
204+
* @param string $url
205+
* @param int $timeout
206+
* @param array $requestBody
207+
* @param array $additionalHeaders
208+
* @param $requestBodyAsString
209+
* @return array|bool|int[]|string
210+
* @throws \Exception
211+
*/
161212
public function sendHttpRequest(string $url, int $timeout, array $requestBody, array $additionalHeaders = [], $requestBodyAsString = false)
162213
{
163214
if ($requestBodyAsString && !empty($requestBody[0])) {

0 commit comments

Comments
 (0)