Skip to content

Commit 97beca3

Browse files
committed
Cleanup
1 parent 897b6d0 commit 97beca3

File tree

1 file changed

+64
-42
lines changed

1 file changed

+64
-42
lines changed

src/Telegram.php

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,72 @@
22
/**
33
* PHPCI - Continuous Integration for PHP
44
*
5-
* @copyright Copyright 2014, Block 8 Limited.
6-
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
7-
* @link https://www.phptesting.org/
5+
* @copyright Copyright 2014, Block 8 Limited.
6+
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
7+
* @link https://www.phptesting.org/
88
*/
99

1010
namespace PHPCI\Plugin;
1111

1212
use PHPCI\Builder;
13-
use PHPCI\Helper\Lang;
1413
use PHPCI\Model\Build;
1514
use b8\HttpClient;
1615

1716
/**
1817
* Telegram Plugin
19-
* @author LEXASOFT <lexasoft83@gmail.com>
20-
* @package PHPCI
21-
* @subpackage Plugins
18+
* @author LEXASOFT <lexasoft83@gmail.com>
19+
* @package PHPCI
20+
* @subpackage Plugins
2221
*/
2322
class Telegram implements \PHPCI\Plugin
2423
{
2524
protected $phpci;
2625
protected $build;
27-
protected $api_key;
26+
protected $apiKey;
2827
protected $message;
28+
protected $buildMsg;
2929
protected $recipients;
30-
protected $send_log;
30+
protected $sendLog;
3131

3232
/**
3333
* Standard Constructor
3434
*
3535
* @param Builder $phpci
3636
* @param Build $build
3737
* @param array $options
38+
* @throws \Exception
3839
*/
39-
public function __construct(Builder $phpci, Build $build, array $options = array())
40+
public function __construct(Builder $phpci, Build $build, array $options = [])
4041
{
4142
$this->phpci = $phpci;
4243
$this->build = $build;
44+
4345
if (empty($options['api_key'])) {
4446
throw new \Exception("Not setting telegram api_key");
4547
}
48+
4649
if (empty($options['recipients'])) {
4750
throw new \Exception("Not setting recipients");
4851
}
49-
$this->api_key = $options['api_key'];
50-
$this->message = '[%ICON_BUILD%] [%PROJECT_TITLE%](%PROJECT_URI%) - [Build #%BUILD%](%BUILD_URI%) has finished ' .
52+
53+
$this->apiKey = $options['api_key'];
54+
$this->message = '[%ICON_BUILD%] [%PROJECT_TITLE%](%PROJECT_URI%)' .
55+
' - [Build #%BUILD%](%BUILD_URI%) has finished ' .
5156
'for commit [%SHORT_COMMIT% (%COMMIT_EMAIL%)](%COMMIT_URI%) ' .
5257
'on branch [%BRANCH%](%BRANCH_URI%)';
58+
5359
if (isset($options['message'])) {
5460
$this->message = $options['message'];
5561
}
56-
$this->recipients = array();
62+
63+
$this->recipients = [];
5764
if (is_string($options['recipients'])) {
58-
$this->recipients = array($options['recipients']);
65+
$this->recipients = [$options['recipients']];
5966
} elseif (is_array($options['recipients'])) {
6067
$this->recipients = $options['recipients'];
6168
}
62-
$this->send_log = isset($options['send_log']) && ((bool) $options['send_log'] !== false);
69+
70+
$this->sendLog = isset($options['send_log']) && ((bool) $options['send_log'] !== false);
6371
}
6472

6573
/**
@@ -68,47 +76,61 @@ public function __construct(Builder $phpci, Build $build, array $options = array
6876
*/
6977
public function execute()
7078
{
71-
$build_icon = $this->build->isSuccessful() ? '' : '';
72-
$buildMsg = $this->build->getLog();
73-
$buildMsg = str_replace(array('[0;32m', '[0;31m', '[0m', '/[0m'), array('', '', ''), $buildMsg);
74-
$buildmessages = explode('RUNNING PLUGIN: ', $buildMsg);
75-
$buildMsg = '';
76-
foreach ($buildmessages as $bm) {
77-
$pos = mb_strpos($bm, "\n");
78-
$firstRow = mb_substr($bm, 0, $pos);
79-
//skip long outputs
80-
if (($firstRow == 'slack_notify')||($firstRow == 'php_loc')||($firstRow == 'telegram')) {
81-
continue;
82-
}
83-
$buildMsg .= '*RUNNING PLUGIN: ' . $firstRow . "*\n";
84-
$buildMsg .= $firstRow == 'composer' ? '' : ('```' . mb_substr($bm, $pos) . '```');
85-
}
86-
$message = $this->phpci->interpolate(str_replace(array('%ICON_BUILD%'), array($build_icon), $this->message));
79+
80+
$message = $this->buildMessage();
8781

8882
$http = new HttpClient('https://api.telegram.org');
89-
$http->setHeaders(array('Content-Type: application/json'));
90-
$uri = '/bot'. $this->api_key . '/sendMessage';
83+
$http->setHeaders(['Content-Type: application/json']);
84+
$uri = '/bot'. $this->apiKey . '/sendMessage';
9185

92-
foreach ($this->recipients as $chat_id) {
93-
$params = array(
94-
'chat_id' => $chat_id,
86+
foreach ($this->recipients as $chatId) {
87+
$params = [
88+
'chat_id' => $chatId,
9589
'text' => $message,
9690
'parse_mode' => 'Markdown',
97-
);
91+
];
9892

9993
$http->post($uri, json_encode($params));
10094

101-
if ($this->send_log) {
102-
$params = array(
103-
'chat_id' => $chat_id,
104-
'text' => $buildMsg,
95+
if ($this->sendLog) {
96+
$params = [
97+
'chat_id' => $chatId,
98+
'text' => $this->buildMsg,
10599
'parse_mode' => 'Markdown',
106-
);
100+
];
107101

108102
$http->post($uri, json_encode($params));
109103
}
110104
}
111105

112106
return true;
113107
}
108+
109+
/**
110+
* Build message.
111+
* @return string
112+
*/
113+
private function buildMessage()
114+
{
115+
$this->buildMsg = '';
116+
$buildIcon = $this->build->isSuccessful() ? '' : '';
117+
$buildLog = $this->build->getLog();
118+
$buildLog = str_replace(['[0;32m', '[0;31m', '[0m', '/[0m'], '', $buildLog);
119+
$buildmessages = explode('RUNNING PLUGIN: ', $buildLog);
120+
121+
foreach ($buildmessages as $bm) {
122+
$pos = mb_strpos($bm, "\n");
123+
$firstRow = mb_substr($bm, 0, $pos);
124+
125+
//skip long outputs
126+
if (in_array($firstRow, ['slack_notify', 'php_loc', 'telegram'])) {
127+
continue;
128+
}
129+
130+
$this->buildMsg .= '*RUNNING PLUGIN: ' . $firstRow . "*\n";
131+
$this->buildMsg .= $firstRow == 'composer' ? '' : ('```' . mb_substr($bm, $pos) . '```');
132+
}
133+
134+
return $this->phpci->interpolate(str_replace(['%ICON_BUILD%'], [$buildIcon], $this->message));
135+
}
114136
}

0 commit comments

Comments
 (0)