Passed
Push — master ( 565c41...d8e277 )
by Frank
02:13
created

CommandResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 50
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolveCommand() 0 14 4
A detectCommandClass() 0 6 2
1
<?php
2
/**
3
 * T3Bot.
4
 *
5
 * @author Frank Nägler <[email protected]>
6
 *
7
 * @link http://www.t3bot.de
8
 * @link http://wiki.typo3.org/T3Bot
9
 */
10
namespace T3Bot\Slack;
11
12
use Slack\Payload;
13
use Slack\RealTimeClient;
14
use T3Bot\Commands\AbstractCommand;
15
use T3Bot\Commands\BottyCommand;
16
17
/**
18
 * Class CommandResolver.
19
 */
20
class CommandResolver
21
{
22
    /**
23
     * @var RealTimeClient
24
     */
25
    protected $client;
26
27
    /**
28
     * @var Payload
29
     */
30
    protected $payload;
31
32 8
    public function __construct(Payload $payload, RealTimeClient $client)
33
    {
34 8
        $this->payload = $payload;
35 8
        $this->client = $client;
36 8
    }
37
38
    /**
39
     * @param array|null $configuration
40
     *
41
     * @return bool|AbstractCommand
42
     */
43 8
    public function resolveCommand(array $configuration = null)
44
    {
45 8
        $message = $this->payload->getData()['text'];
46 8
        $commandClass = $this->detectCommandClass($message);
47 8
        if (class_exists($commandClass)) {
48 6
            return new $commandClass($this->payload, $this->client, $configuration);
49
        }
50
51 2
        if (strpos($message, 'botty') !== false || strpos($message, $configuration['slack']['botId']) !== false) {
52 1
            return new BottyCommand($this->payload, $this->client, $configuration);
53
        }
54
55 1
        return false;
56
    }
57
58
    /**
59
     * @param string $message
60
     *
61
     * @return string
62
     */
63 8
    protected function detectCommandClass(string $message) : string
64
    {
65 8
        $delimiter = strpos($message, ':') !== false ? ':' : ' ';
66 8
        $parts = explode($delimiter, $message);
67 8
        return '\\T3Bot\\Commands\\' . ucfirst(strtolower($parts[0])) . 'Command';
68
    }
69
}
70