Typo3SlackChannel::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 20
rs 9.8333
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Domain\Channel\Slack;
19
20
use CuyZ\Notiz\Core\Channel\AbstractChannel;
21
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\Service\EntitySlackBotMapper;
22
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\Service\EntitySlackChannelMapper;
23
use CuyZ\Notiz\Domain\Notification\Slack\Application\EntitySlack\Service\EntitySlackMessageBuilder;
24
use CuyZ\Notiz\Domain\Notification\Slack\SlackNotification;
25
use TYPO3\CMS\Core\Http\RequestFactory;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
class Typo3SlackChannel extends AbstractChannel
29
{
30
    /**
31
     * @var array
32
     */
33
    protected static $supportedNotifications = [
34
        SlackNotification::class,
35
    ];
36
37
    /**
38
     * @var EntitySlackMessageBuilder
39
     */
40
    protected $messageBuilder;
41
42
    /**
43
     * @var EntitySlackBotMapper
44
     */
45
    protected $botMapper;
46
47
    /**
48
     * @var EntitySlackChannelMapper
49
     */
50
    protected $channelMapper;
51
52
    /**
53
     * @var SlackNotification
54
     */
55
    protected $notification;
56
57
    /**
58
     * Manual dependency injection.
59
     */
60
    final protected function initialize()
61
    {
62
        $this->messageBuilder = $this->objectManager->get(
63
            EntitySlackMessageBuilder::class,
64
            $this->payload
65
        );
66
67
        $this->botMapper = $this->objectManager->get(
68
            EntitySlackBotMapper::class,
69
            $this->payload
70
        );
71
72
        $this->channelMapper = $this->objectManager->get(
73
            EntitySlackChannelMapper::class,
74
            $this->payload
75
        );
76
77
        $this->notification = $this->payload->getNotification();
78
    }
79
80
    /**
81
     * Send the Slack notification.
82
     */
83
    protected function process()
84
    {
85
        $bot = $this->botMapper->getBot();
86
        $channels = $this->channelMapper->getChannels();
87
88
        foreach ($channels as $channel) {
89
            $webhookUrl = $channel->getWebhookUrl();
90
91
            $iconKey = $bot->hasEmojiAvatar()
92
                ? 'icon_emoji'
93
                : 'icon_url';
94
95
            $data = [
96
                'channel' => $channel->getTarget(),
97
                'text' => $this->messageBuilder->getMessage(),
98
                'username' => $bot->getName(),
99
                $iconKey => $bot->getAvatar(),
100
            ];
101
102
            $this->callSlack($webhookUrl, $data);
103
        }
104
    }
105
106
    /**
107
     * @param string $webhookUrl
108
     * @param array $data
109
     */
110
    protected function callSlack(string $webhookUrl, array $data)
111
    {
112
        /** @var RequestFactory $factory */
113
        $factory = GeneralUtility::makeInstance(RequestFactory::class);
114
115
        $data = json_encode($data);
116
117
        $factory->request(
118
            $webhookUrl,
119
            'POST',
120
            [
121
                'headers' => [
122
                    'Content-Type' => 'application/json',
123
                    'Content-Length' => strlen($data),
124
                ],
125
                'body' => $data,
126
            ]
127
        );
128
    }
129
}
130