Passed
Push — master ( e31cd2...e6253a )
by Romain
03:51
created

Typo3SlackChannel::callSlackLegacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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