Skip to content

Commit 7f530ee

Browse files
committed
Add get commands to Slackbot
1 parent 648f5f3 commit 7f530ee

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

src/Slackbot/AbstractCommandContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function get($key)
1919
$commands = $this->getAll($key);
2020

2121
if (!array_key_exists($key, $commands)) {
22+
/** @noinspection PhpInconsistentReturnPointsInspection */
2223
return;
2324
}
2425

src/Slackbot/Slackbot.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Slackbot
1515
{
1616
private $request;
1717
private $config;
18+
private $commands;
1819

1920
/**
2021
* Slackbot constructor.
@@ -57,10 +58,9 @@ public function setRequest($request)
5758
/**
5859
* @param null $key
5960
*
60-
* @throws \Exception
61-
*
6261
* @return mixed
6362
*/
63+
/** @noinspection PhpInconsistentReturnPointsInspection */
6464
public function getRequest($key = null)
6565
{
6666
if ($key === null) {
@@ -268,4 +268,24 @@ public function setConfig(Config $config)
268268
{
269269
$this->config = $config;
270270
}
271+
272+
/**
273+
* @return array
274+
*/
275+
public function getCommands()
276+
{
277+
if (!isset($this->commands)) {
278+
$this->setCommands((new CommandContainer())->getAll());
279+
}
280+
281+
return $this->commands;
282+
}
283+
284+
/**
285+
* @param array $commands
286+
*/
287+
public function setCommands(array $commands)
288+
{
289+
$this->commands = $commands;
290+
}
271291
}

src/Slackbot/plugin/help/Help.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Slackbot\plugin\help;
44

55
use Slackbot\Command;
6-
use Slackbot\CommandContainer;
76
use Slackbot\plugin\AbstractPlugin;
87
use Slackbot\utility\FormattingUtility;
98

@@ -17,7 +16,7 @@ class Help extends AbstractPlugin
1716
*/
1817
public function index()
1918
{
20-
$allCommands = (new CommandContainer())->getAll();
19+
$allCommands = $this->getSlackbot()->getCommands();
2120

2221
$response = '';
2322
if (!empty($allCommands)) {

src/tests/CommandContainerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function testGet()
2121

2222
$this->assertTrue($commandObject instanceof Command);
2323

24+
/** @noinspection PhpUndefinedMethodInspection */
2425
$this->assertEquals($commandObject->getPlugin(), 'Ping');
2526
}
2627

@@ -30,6 +31,7 @@ public function testGet()
3031
public function testGetAll()
3132
{
3233
$commands = (new CommandContainer())->getAll();
34+
/** @noinspection PhpUndefinedMethodInspection */
3335
$this->assertEquals($commands['ping']->getPlugin(), 'Ping');
3436
}
3537
}

src/tests/CommandTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22

33
namespace Slackbot;
44

5+
/** @noinspection PhpUndefinedClassInspection */
56
class CommandTest extends \PHPUnit_Framework_TestCase
67
{
8+
const PING_KEY = 'ping';
9+
710
public function testGetAction()
811
{
9-
$command = new Command('ping');
12+
$command = new Command(self::PING_KEY);
1013

1114
$this->assertEquals(Command::DEFAULT_ACTION, $command->getAction());
1215
}
16+
17+
public function testGetKey()
18+
{
19+
$command = new Command(self::PING_KEY);
20+
21+
$this->assertEquals(self::PING_KEY, $command->getKey());
22+
}
1323
}

src/tests/ConfigTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
* Class ConfigTest.
99
*/
1010

11-
/** @noinspection PhpUndefinedClassInspection */
12-
1311
/** @noinspection PhpUndefinedClassInspection */
1412
class ConfigTest extends \PHPUnit_Framework_TestCase
1513
{

src/tests/SlackbotTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,15 @@ private function outputOnNoCommand($message)
209209

210210
if (!empty($defaultCommand)) {
211211
$commandObject = (new CommandContainer())->get($defaultCommand);
212+
/** @noinspection PhpUndefinedMethodInspection */
212213
$commandClass = $commandObject->getClass();
213214

214215
/**
215216
* @var AbstractPlugin
216217
*/
217218
$pluginObject = (new $commandClass($slackbot));
218219

220+
/** @noinspection PhpUndefinedMethodInspection */
219221
return $pluginObject->index();
220222
}
221223

0 commit comments

Comments
 (0)