CommandResolver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 53
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolveCommand() 0 17 5
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
0 ignored issues
show
Documentation introduced by
Should the return type not be false|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
42
     */
43 8
    public function resolveCommand(array $configuration = null)
44
    {
45 8
        $message = $this->payload->getData()['text'] ?? '';
46 8
        if ($message === '') {
47
            return false;
48
        }
49 8
        $commandClass = $this->detectCommandClass($message);
50 8
        if (class_exists($commandClass)) {
51 6
            return new $commandClass($this->payload, $this->client, $configuration);
52
        }
53
54 2
        if (strpos($message, 'botty') !== false || strpos($message, $configuration['slack']['botId']) !== false) {
55 1
            return new BottyCommand($this->payload, $this->client, $configuration);
56
        }
57
58 1
        return false;
59
    }
60
61
    /**
62
     * @param string $message
63
     *
64
     * @return string
65
     */
66 8
    protected function detectCommandClass(string $message) : string
67
    {
68 8
        $delimiter = strpos($message, ':') !== false ? ':' : ' ';
69 8
        $parts = explode($delimiter, $message);
70 8
        return '\\T3Bot\\Commands\\' . ucfirst(strtolower($parts[0])) . 'Command';
71
    }
72
}
73