Passed
Pull Request — master (#75)
by Nathan
03:50
created

Typo3SlackChannel::process()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 0
dl 0
loc 26
rs 8.5806
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
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
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
        $callSlack = 'callSlack';
89
90
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.1.0', '<')) {
91
            $callSlack = 'callSlackLegacy';
92
        }
93
94
        foreach ($channels as $channel) {
95
            $webhookUrl = $channel->getWebhookUrl();
96
97
            $iconKey = $bot->hasEmojiAvatar()
98
                ? 'icon_emoji'
99
                : 'icon_url';
100
101
            $data = [
102
                'channel' => $channel->getTarget(),
103
                'text' => $this->messageBuilder->getMessage(),
104
                'username' => $bot->getName(),
105
                $iconKey => $bot->getAvatar(),
106
            ];
107
108
            $this->$callSlack($webhookUrl, $data);
109
        }
110
    }
111
112
    /**
113
     * @param string $webhookUrl
114
     * @param array $data
115
     */
116
    protected function callSlack($webhookUrl, array $data)
117
    {
118
        /** @var RequestFactory $factory */
119
        $factory = GeneralUtility::makeInstance(RequestFactory::class);
120
121
        $data = json_encode($data);
122
123
        $factory->request(
124
            $webhookUrl,
125
            'POST',
126
            [
127
                'headers' => [
128
                    'Content-Type' => 'application/json',
129
                    'Content-Length' => strlen($data),
130
                ],
131
                'body' => $data,
132
            ]
133
        );
134
    }
135
136
    /**
137
     * @param string $webhookUrl
138
     * @param array $data
139
     */
140
    protected function callSlackLegacy($webhookUrl, array $data)
141
    {
142
        $data = json_encode($data);
143
144
        $curl = curl_init($webhookUrl);
145
146
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
147
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
148
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
149
150
        curl_setopt(
151
            $curl,
152
            CURLOPT_HTTPHEADER,
153
            [
154
                'Content-Type: application/json',
155
                'Content-Length: ' . strlen($data),
156
            ]
157
        );
158
159
        curl_exec($curl);
160
    }
161
}
162