Skip to content

Commit

Permalink
[TASK] CGL fix and PHP 7 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoBlack committed Jul 22, 2017
1 parent 5207f36 commit 7ee272c
Show file tree
Hide file tree
Showing 33 changed files with 281 additions and 298 deletions.
39 changes: 19 additions & 20 deletions Classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* @link http://www.t3bot.de
* @link http://wiki.typo3.org/T3Bot
*/

namespace T3Bot\Commands;

use /* @noinspection PhpInternalEntityUsedInspection */ Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Slack\Message\Attachment;
use Slack\Payload;
Expand Down Expand Up @@ -41,12 +41,12 @@ abstract class AbstractCommand
/**
* @var array
*/
protected $helpCommands = array();
protected $helpCommands = [];

/**
* @var array
*/
protected $params = array();
protected $params = [];

/**
* @var Payload
Expand Down Expand Up @@ -76,24 +76,24 @@ public function __construct(Payload $payload, RealTimeClient $client)
public function process()
{
$commandParts = explode(':', $this->payload->getData()['text']);
$params = array();
$params = [];
if (!empty($commandParts[1])) {
array_shift($commandParts);
$params = explode(' ', preg_replace('/\s+/', ' ', implode(':', $commandParts)));
}

$command = !empty($params[0]) ? $params[0] : 'help';
$this->params = $params;
$method = 'process'.ucfirst(strtolower($command));
$method = 'process' . ucfirst(strtolower($command));
if (method_exists($this, $method)) {
return $this->{$method}();
} else {
return $this->getHelp();
}

return $this->getHelp();
}

/**
* @param \T3Bot\Slack\Message|string $messageToSent
* @param Message|string $messageToSent
* @param string $user
* @param string $channel the channel id
*/
Expand All @@ -113,7 +113,7 @@ public function sendResponse($messageToSent, $user = null, $channel = null)
$data['channel'] = $channel;
$attachments = $messageToSent->getAttachments();
if (count($attachments)) {
$data['attachments'] = array();
$data['attachments'] = [];
}
/** @var \T3Bot\Slack\Message\Attachment $attachment */
foreach ($attachments as $attachment) {
Expand Down Expand Up @@ -145,7 +145,7 @@ public function sendResponse($messageToSent, $user = null, $channel = null)
}
});
} else {
$channel = $channel !== null ? $channel : $this->payload->getData()['channel'];
$channel = $channel ?? $this->payload->getData()['channel'];
if ($messageToSent instanceof Message) {
$data['unfurl_links'] = false;
$data['unfurl_media'] = false;
Expand All @@ -154,7 +154,7 @@ public function sendResponse($messageToSent, $user = null, $channel = null)
$data['channel'] = $channel;
$attachments = $messageToSent->getAttachments();
if (count($attachments)) {
$data['attachments'] = array();
$data['attachments'] = [];
}
/** @var \T3Bot\Slack\Message\Attachment $attachment */
foreach ($attachments as $attachment) {
Expand Down Expand Up @@ -191,7 +191,7 @@ public function sendResponse($messageToSent, $user = null, $channel = null)
*
* @return string
*/
public function getHelp()
public function getHelp() : string
{
$result = "*HELP*\n";
foreach ($this->helpCommands as $command => $helpText) {
Expand All @@ -208,7 +208,7 @@ public function getHelp()
*
* @return Message
*/
protected function buildReviewMessage($item)
protected function buildReviewMessage($item) : Message
{
$created = substr($item->created, 0, 19);
$branch = $item->branch;
Expand Down Expand Up @@ -239,11 +239,11 @@ protected function buildReviewMessage($item)
break;
}
$attachment->setTitle($item->subject);
$attachment->setTitleLink('https://review.typo3.org/'.$item->_number);
$attachment->setTitleLink('https://review.typo3.org/' . $item->_number);

$text .= 'Branch: '.$this->bold($branch).' | :calendar: '.$this->bold($created)
.' | ID: '.$this->bold($item->_number)."\n";
$text .= '<https://review.typo3.org/'.$item->_number.'|:arrow_right: Goto Review>';
$text .= 'Branch: ' . $this->bold($branch) . ' | :calendar: ' . $this->bold($created)
. ' | ID: ' . $this->bold($item->_number) . "\n";
$text .= '<https://review.typo3.org/' . $item->_number . '|:arrow_right: Goto Review>';

$attachment->setText($text);
$attachment->setFallback($text);
Expand All @@ -254,14 +254,13 @@ protected function buildReviewMessage($item)
}

/**
* @return \Doctrine\DBAL\Connection
* @return Connection
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function getDatabaseConnection()
protected function getDatabaseConnection() : Connection
{
if (empty($GLOBALS['DB'])) {
/* @noinspection PhpInternalEntityUsedInspection */
$config = new Configuration();
$GLOBALS['DB'] = DriverManager::getConnection($GLOBALS['config']['db'], $config);
}
Expand Down
63 changes: 31 additions & 32 deletions Classes/Commands/BeerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @link http://www.t3bot.de
* @link http://wiki.typo3.org/T3Bot
*/

namespace T3Bot\Commands;

/**
Expand Down Expand Up @@ -38,9 +37,9 @@ class BeerCommand extends AbstractCommand
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function processAll()
protected function processAll() : string
{
return 'Yeah, '.$this->getBeerCountAll().' :t3beer: spend to all people';
return 'Yeah, ' . $this->getBeerCountAll() . ' :t3beer: spend to all people';
}

/**
Expand All @@ -50,15 +49,15 @@ protected function processAll()
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function processTop10()
protected function processTop10() : string
{
$rows = $this->getBeerTop10();
$text = array('*Yeah, here are the TOP 10*');
$text = ['*Yeah, here are the TOP 10*'];
foreach ($rows as $row) {
$text[] = '<@'.$row['username'].'> has received '.$row['cnt'].' :t3beer:';
$text[] = '<@' . $row['username'] . '> has received ' . $row['cnt'] . ' :t3beer:';
}

return implode("\n", $text);
return implode(chr(10), $text);
}

/**
Expand All @@ -68,18 +67,17 @@ protected function processTop10()
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function processStats()
protected function processStats() : string
{
$params = $this->params;
array_shift($params);
$username = trim($params[0]);
if (strpos($username, '<') === 0 && $username[1] === '@') {
$username = str_replace(['<', '>', '@'], '', $username);

return '<@'.$username.'> has received '.$this->getBeerCountByUsername($username).' :t3beer: so far';
} else {
return '*Sorry, a username must start with a @-sign:*';
return '<@' . $username . '> has received ' . $this->getBeerCountByUsername($username) . ' :t3beer: so far';
}
return '*Sorry, a username must start with a @-sign:*';
}

/**
Expand All @@ -89,7 +87,7 @@ protected function processStats()
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function processFor()
protected function processFor() : string
{
$from_user = $this->payload->getData()['user'];
$params = $this->params;
Expand All @@ -102,24 +100,25 @@ protected function processFor()
$username, $from_user
]
);
if (!empty($record)) {
if ($record[0]['tstamp'] + 86400 < time()) {
$data = [
'to_user' => $username,
'from_user' => $from_user,
'tstamp' => time()
];
$this->getDatabaseConnection()->insert('beers', $data);
return 'Yeah, one more :t3beer: for <@'.$username.'>'.chr(10).'<@'.$username.'> has received '
. $this->getBeerCountByUsername($username).' :t3beer: so far';
} else {
return 'You spend one :t3beer: to <@' . $username . '> within in last 24 hours. Too much beer is unhealthy ;)';
}
$addBeer = false;
if (empty($record)) {
$addBeer = true;
} elseif ($record[0]['tstamp'] + 86400 < time()) {
$addBeer = true;
}

} else {
return '*Sorry, a username must start with a @-sign:*';
if ($addBeer) {
$data = [
'to_user' => $username,
'from_user' => $from_user,
'tstamp' => time()
];
$this->getDatabaseConnection()->insert('beers', $data);
return 'Yeah, one more :t3beer: for <@' . $username . '>' . chr(10) . '<@' . $username . '> has received '
. $this->getBeerCountByUsername($username) . ' :t3beer: so far';
}
return 'You spend one :t3beer: to <@' . $username . '> within in last 24 hours. Too much beer is unhealthy ;)';
}
return '*Sorry, a username must start with a @-sign:*';
}

/**
Expand All @@ -129,18 +128,18 @@ protected function processFor()
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function getBeerCountByUsername($username)
protected function getBeerCountByUsername($username) : int
{
return count($this->getDatabaseConnection()
->fetchAll('SELECT * FROM beers WHERE to_user = ?', array($username)));
->fetchAll('SELECT * FROM beers WHERE to_user = ?', [$username]));
}

/**
* @return int
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function getBeerCountAll()
protected function getBeerCountAll() : int
{
return count($this->getDatabaseConnection()
->fetchAll('SELECT * FROM beers'));
Expand All @@ -151,7 +150,7 @@ protected function getBeerCountAll()
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function getBeerTop10()
protected function getBeerTop10() : array
{
return $this->getDatabaseConnection()->fetchAll(
'SELECT count(*) as cnt, to_user as username FROM beers GROUP BY to_user ORDER BY cnt DESC LIMIT 10'
Expand Down
49 changes: 24 additions & 25 deletions Classes/Commands/BottyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @link http://www.t3bot.de
* @link http://wiki.typo3.org/T3Bot
*/

namespace T3Bot\Commands;

/**
Expand All @@ -29,14 +28,14 @@ public function process()
public function processMessage()
{
$message = strtolower($this->payload->getData()['text']);
$username = '<@'.$this->payload->getData()['user'].'>';
$username = '<@' . $this->payload->getData()['user'] . '>';

if (strpos($message, 'help') !== false) {
$links = array(
$links = [
'My Homepage' => 'http://www.t3bot.de',
'Github' => 'https://github.com/NeoBlack/T3Bot',
'Help for Commands' => 'http://wiki.typo3.org/T3Bot',
);
];

$result = [];
foreach ($links as $text => $link) {
Expand All @@ -46,31 +45,31 @@ public function processMessage()
return implode(' | ', $result);
}

$cats = array(':smiley_cat:', ':smile_cat:', ':heart_eyes_cat:', ':kissing_cat:', ':smirk_cat:', ':scream_cat:',
':crying_cat_face:', ':joy_cat:' ,':pouting_cat:', );
$cats = [':smiley_cat:', ':smile_cat:', ':heart_eyes_cat:', ':kissing_cat:', ':smirk_cat:', ':scream_cat:',
':crying_cat_face:', ':joy_cat:' , ':pouting_cat:', ];

$responses = array(
$responses = [
'daddy' => 'My daddy is Frank Nägler aka <@neoblack>',
'n8' => 'Good night '.$username.'! :sleeping:',
'nacht' => 'Good night '.$username.'! :sleeping:',
'night' => 'Good night '.$username.'! :sleeping:',
'hello' => 'Hello '.$username.', nice to see you!',
'hallo' => 'Hello '.$username.', nice to see you!',
'ciao' => 'Bye, bye '.$username.', cu later alligator! :wave:',
'cu' => 'Bye, bye '.$username.', cu later alligator! :wave:',
'thx' => 'You are welcome '.$username.'!',
'thank' => 'You are welcome '.$username.'!',
'drink' => 'Coffee or beer '.$username.'?',
'coffee' => 'Here is a :coffee: for you '.$username.'!',
'beer' => 'Here is a :t3beer: for you '.$username.'!',
'coke' => 'Coke is unhealthy '.$username.'!',
'cola' => 'Coke is unhealthy '.$username.'!',
'cookie' => 'Here is a :cookie: for you '.$username.'!',
'n8' => 'Good night ' . $username . '! :sleeping:',
'nacht' => 'Good night ' . $username . '! :sleeping:',
'night' => 'Good night ' . $username . '! :sleeping:',
'hello' => 'Hello ' . $username . ', nice to see you!',
'hallo' => 'Hello ' . $username . ', nice to see you!',
'ciao' => 'Bye, bye ' . $username . ', cu later alligator! :wave:',
'cu' => 'Bye, bye ' . $username . ', cu later alligator! :wave:',
'thx' => 'You are welcome ' . $username . '!',
'thank' => 'You are welcome ' . $username . '!',
'drink' => 'Coffee or beer ' . $username . '?',
'coffee' => 'Here is a :coffee: for you ' . $username . '!',
'beer' => 'Here is a :t3beer: for you ' . $username . '!',
'coke' => 'Coke is unhealthy ' . $username . '!',
'cola' => 'Coke is unhealthy ' . $username . '!',
'cookie' => 'Here is a :cookie: for you ' . $username . '!',
'typo3' => ':typo3: TYPO3 CMS is the best open source CMS of the world!',
'dark' => 'sure, we have cookies :cookie:',
'cat' => 'ok, here is some cat content '.$cats[array_rand($cats)],
'love' => 'I love you too, '.$username.':kiss:',
);
'cat' => 'ok, here is some cat content ' . $cats[array_rand($cats)],
'love' => 'I love you too, ' . $username . ':kiss:',
];
$messageToSend = '';
foreach ($responses as $keyword => $response) {
if (strpos($message, $keyword) !== false) {
Expand Down
1 change: 0 additions & 1 deletion Classes/Commands/ChannelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @link http://www.t3bot.de
* @link http://wiki.typo3.org/T3Bot
*/

namespace T3Bot\Commands;

use T3Bot\Slack\Message;
Expand Down
4 changes: 1 addition & 3 deletions Classes/Commands/ForgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @link http://www.t3bot.de
* @link http://wiki.typo3.org/T3Bot
*/

namespace T3Bot\Commands;

/**
Expand Down Expand Up @@ -48,8 +47,7 @@ protected function processShow()
$result = $this->queryForge("issues/{$issueNumber}");
if ($result) {
return $this->buildIssueMessage($result->issue);
} else {
return 'Sorry not found!';
}
return 'Sorry not found!';
}
}
Loading

0 comments on commit 7ee272c

Please sign in to comment.