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

AbstractCommand::buildAttachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AbstractCommand::getDatabaseConnection() 0 8 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\Commands;
11
12
use /* @noinspection PhpInternalEntityUsedInspection */ Doctrine\DBAL\Configuration;
13
use Doctrine\DBAL\Connection;
14
use Doctrine\DBAL\DriverManager;
15
use Slack\Payload;
16
use Slack\RealTimeClient;
17
use T3Bot\Slack\Message;
18
use T3Bot\Traits\ForgerTrait;
19
use T3Bot\Traits\GerritTrait;
20
use T3Bot\Traits\SlackTrait;
21
22
/**
23
 * Class AbstractCommand.
24
 *
25
 * @property string commandName
26
 * @property array helpCommands
27
 */
28
abstract class AbstractCommand
29
{
30
    use SlackTrait, ForgerTrait, GerritTrait;
31
32
    const PROJECT_PHASE_DEVELOPMENT = 'development';
33
    const PROJECT_PHASE_STABILISATION = 'stabilisation';
34
    const PROJECT_PHASE_SOFT_FREEZE = 'soft_freeze';
35
    const PROJECT_PHASE_CODE_FREEZE = 'code_freeze';
36
    const PROJECT_PHASE_FEATURE_FREEZE = 'feature_freeze';
37
38
    /**
39
     * @var string
40
     */
41
    protected $commandName;
42
43
    /**
44
     * @var array
45
     */
46
    protected $helpCommands = [];
47
48
    /**
49
     * @var array
50
     */
51
    protected $params = [];
52
53
    /**
54
     * @var Payload
55
     */
56
    protected $payload;
57
58
    /**
59
     * @var RealTimeClient
60
     */
61
    protected $client;
62
63
    /**
64
     * @var array|null
65
     */
66
    protected $configuration;
67
68
    /**
69
     * @var Connection
70
     */
71
    protected $databaseConnection;
72
73
    /**
74
     * AbstractCommand constructor.
75
     *
76
     * @param Payload $payload
77
     * @param RealTimeClient $client
78
     * @param array|null $configuration
79
     */
80 102
    public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null)
81
    {
82 102
        $this->payload = $payload;
83 102
        $this->client = $client;
84 102
        $this->configuration = $configuration;
85 102
    }
86
87
    /**
88
     *
89
     */
90 46
    public function process()
91
    {
92 46
        $commandParts = explode(':', $this->payload->getData()['text']);
93 46
        $params = [];
94 46
        if (!empty($commandParts[1])) {
95 46
            array_shift($commandParts);
96 46
            $params = explode(' ', preg_replace('/\s+/', ' ', implode(':', $commandParts)));
97
        }
98
99 46
        $command = !empty($params[0]) ? $params[0] : 'help';
100 46
        $this->params = $params;
101 46
        $method = 'process' . ucfirst(strtolower($command));
102 46
        if (method_exists($this, $method)) {
103 45
            return $this->{$method}();
104
        }
105
106 1
        return $this->getHelp();
107
    }
108
109
    /**
110
     * @param Message|string $messageToSent
111
     * @param string $user
112
     * @param string $channel the channel id
113
     */
114 2
    public function sendResponse($messageToSent, $user = null, $channel = null)
115
    {
116 2
        if ($user !== null) {
117
            $this->client->apiCall('im.open', ['user' => $user])
118
                ->then(function (Payload $response) use ($messageToSent) {
119
                    $channel = $response->getData()['channel']['id'];
120
                    $this->postMessage($messageToSent, $channel);
121
                });
122
        } else {
123 2
            $channel = $channel ?? $this->payload->getData()['channel'];
124 2
            $this->postMessage($messageToSent, $channel);
125
        }
126 2
    }
127
128
    /**
129
     * generate help.
130
     *
131
     * @return string
132
     */
133 1
    public function getHelp() : string
134
    {
135 1
        $result = '*HELP*' . chr(10);
136 1
        foreach ($this->helpCommands as $command => $helpText) {
137 1
            $result .= "*{$this->commandName}:{$command}*: {$helpText}" . chr(10);
138
        }
139
140 1
        return $result;
141
    }
142
143
    /**
144
     * build a review message.
145
     *
146
     * @param \stdClass $item the review item
147
     *
148
     * @return Message
149
     */
150 12
    protected function buildReviewMessage($item) : Message
151
    {
152 12
        $created = substr($item->created, 0, 19);
153 12
        $branch = $item->branch;
154 12
        $text = '';
155
156 12
        $message = new Message();
157 12
        $attachment = new Message\Attachment();
158 12
        switch ($this->configuration['projectPhase']) {
159 12
            case self::PROJECT_PHASE_STABILISATION:
160 1
                $attachment->setColor(Message\Attachment::COLOR_WARNING);
161 1
                $attachment->setPretext(':warning: *stabilisation phase*');
162 1
                break;
163 11
            case self::PROJECT_PHASE_SOFT_FREEZE:
164 1
                $attachment->setColor(Message\Attachment::COLOR_DANGER);
165 1
                $attachment->setPretext(':no_entry: *soft merge freeze*');
166 1
                break;
167 10
            case self::PROJECT_PHASE_CODE_FREEZE:
168 1
                $attachment->setColor(Message\Attachment::COLOR_DANGER);
169 1
                $attachment->setPretext(':no_entry: *merge freeze*');
170 1
                break;
171 9
            case self::PROJECT_PHASE_FEATURE_FREEZE:
172 1
                $attachment->setColor(Message\Attachment::COLOR_DANGER);
173 1
                $attachment->setPretext(':no_entry: *FEATURE FREEZE*');
174 1
                break;
175 8
            case self::PROJECT_PHASE_DEVELOPMENT:
176
            default:
177 8
                $attachment->setColor(Message\Attachment::COLOR_NOTICE);
178 8
                break;
179
        }
180 12
        $attachment->setTitle($item->subject);
181 12
        $attachment->setTitleLink('https://review.typo3.org/' . $item->_number);
182
183 12
        $text .= 'Branch: ' . $this->bold($branch) . ' | :calendar: ' . $this->bold($created)
184 12
            . ' | ID: ' . $this->bold($item->_number) . chr(10);
185 12
        $text .= '<https://review.typo3.org/' . $item->_number . '|:arrow_right: Goto Review>';
186
187 12
        $attachment->setText($text);
188 12
        $attachment->setFallback($text);
189 12
        $message->setText(' ');
190 12
        $message->addAttachment($attachment);
191
192 12
        return $message;
193
    }
194
195
    /**
196
     * @param Message|string $messageToSent
197
     * @param string $channel
198
     */
199 2
    protected function postMessage($messageToSent, string $channel)
200
    {
201 2
        if ($messageToSent instanceof Message) {
202 1
            $data = $this->getBaseDataArray($messageToSent->getText(), $channel);
203 1
            $attachments = $messageToSent->getAttachments();
204 1 View Code Duplication
            if (count($attachments)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205 1
                $data['attachments'] = [];
206 1
                foreach ($attachments as $attachment) {
207 1
                    $data['attachments'][] = $this->buildAttachment($attachment);
208
                }
209
            }
210 1
            $message = new \Slack\Message\Message($this->client, $data);
211 1
            $this->client->postMessage($message);
212 1
        } elseif (is_string($messageToSent)) {
213 1
            $data = $this->getBaseDataArray($messageToSent, $channel);
214 1
            $data['as_user'] = true;
215 1
            $this->client->apiCall('chat.postMessage', $data);
216
        }
217 2
    }
218
219
    /**
220
     * @return Connection
221
     *
222
     * @throws \Doctrine\DBAL\DBALException
223
     */
224 11
    protected function getDatabaseConnection() : Connection
225
    {
226 11
        if ($this->databaseConnection === null) {
227 11
            $config = new Configuration();
228 11
            $this->databaseConnection = DriverManager::getConnection($this->configuration['db'], $config);
229
        }
230 11
        return $this->databaseConnection;
231
    }
232
}
233