Passed
Pull Request — master (#75)
by Nathan
04:06
created

Typo3SlackChannel::process()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 5
nop 0
dl 0
loc 24
rs 8.6845
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
        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
            if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.1.0', '<')) {
103
                // Guzzle is bundled with TYPO3 since version 8.1.0
104
                $this->callSlackLegacy($webhookUrl, $data);
105
            } else {
106
                $this->callSlack($webhookUrl, $data);
107
            }
108
        }
109
    }
110
111
    /**
112
     * @param string $webhookUrl
113
     * @param array $data
114
     */
115
    protected function callSlack($webhookUrl, array $data)
116
    {
117
        /** @var RequestFactory $factory */
118
        $factory = GeneralUtility::makeInstance(RequestFactory::class);
119
120
        $data = json_encode($data);
121
122
        $factory->request(
123
            $webhookUrl,
124
            'POST',
125
            [
126
                'headers' => [
127
                    'Content-Type' => 'application/json',
128
                    'Content-Length' => strlen($data),
129
                ],
130
                'body' => $data,
131
            ]
132
        );
133
    }
134
135
    /**
136
     * @param string $webhookUrl
137
     * @param array $data
138
     */
139
    protected function callSlackLegacy($webhookUrl, array $data)
140
    {
141
        $data = json_encode($data);
142
143
        $ch = curl_init($webhookUrl);
144
145
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
146
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
147
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
148
        curl_setopt($ch, CURLOPT_HTTPHEADER,
149
            [
150
                'Content-Type: application/json',
151
                'Content-Length: ' . strlen($data),
152
            ]
153
        );
154
155
        curl_exec($ch);
156
    }
157
}
158