Passed
Push — master ( 3f897c...b8a081 )
by Frank
02:12
created

WebHookController::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
nc 5
nop 2
dl 0
loc 32
ccs 21
cts 21
cp 1
crap 5
rs 8.439
c 1
b 0
f 0
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\Controller;
11
12
use T3Bot\Slack\Message;
13
14
/**
15
 * Class WebHookController.
16
 */
17
class WebHookController extends AbstractHookController
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $colorMap = [
23
        'info' => Message\Attachment::COLOR_INFO,
24
        'ok' => Message\Attachment::COLOR_GOOD,
25
        'warning' => Message\Attachment::COLOR_WARNING,
26
        'danger' => Message\Attachment::COLOR_DANGER,
27
        'notice' => Message\Attachment::COLOR_NOTICE,
28
    ];
29
30
    /**
31
     * public method to start processing the request.
32
     *
33
     * @param string $hook
34
     * @param string $input
35
     *
36
     * Input example:
37
     * {
38
     *   "securityToken": "a valid security token",
39
     *   "color": [info|ok|warning|danger|notice|#HEXCODE],
40
     *   "title": "Title of the message"
41
     *   "text": "Text of the message"
42
     * }
43
     *
44
     * @throws \Doctrine\DBAL\DBALException
45
     */
46 9
    public function process($hook, $input = 'php://input')
47
    {
48 9
        if (empty($this->configuration['webhook'][$hook])) {
49 1
            return;
50
        }
51 8
        $hookConfiguration = $this->configuration['webhook'][$hook];
52
53 8
        $entityBody = file_get_contents($input);
54 8
        $json = json_decode($entityBody);
55
56 8
        if ($hookConfiguration['securityToken'] !== $json->securityToken) {
57 1
            return;
58
        }
59
60 7
        $color = $this->colorMap[$json->color] ?? $json->color;
61
62 7
        $message = new Message();
63 7
        $message->setText(' ');
64 7
        $attachment = new Message\Attachment();
65
66 7
        $attachment->setColor($color);
67 7
        $attachment->setTitle($json->title);
68 7
        $attachment->setText($json->text);
69 7
        $attachment->setFallback($json->text);
70 7
        $message->addAttachment($attachment);
71
72 7
        if (is_array($hookConfiguration['channels'])) {
73 7
            foreach ($hookConfiguration['channels'] as $channel) {
74 7
                $this->postToSlack($message, $channel);
75
            }
76
        }
77 7
    }
78
}
79