Test Failed
Push — master ( bd1e8a...fed951 )
by Alexey
12:42 queued 12s
created

WowappsSlackbotTestCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 14

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 14
dl 0
loc 196
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 39 6
A drawHeader() 0 12 1
A drawConfig() 0 30 3
B sendTestMessage() 0 70 1
1
<?php
2
3
/*
4
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony.
5
 * https://github.com/wow-apps/symfony-slack-bot
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * https://github.com/wow-apps/symfony-slack-bot/blob/master/LICENSE
10
 *
11
 * For technical documentation.
12
 * https://wow-apps.github.io/symfony-slack-bot/docs/
13
 *
14
 * Author Alexey Samara <[email protected]>
15
 *
16
 * Copyright 2016 WoW-Apps.
17
 */
18
19
namespace WowApps\SlackBundle\Command;
20
21
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
use WowApps\SlackBundle\DTO\Attachment;
27
use WowApps\SlackBundle\DTO\AttachmentAction;
28
use WowApps\SlackBundle\DTO\AttachmentActionConfirm;
29
use WowApps\SlackBundle\DTO\AttachmentField;
30
use WowApps\SlackBundle\DTO\SlackMessage;
31
use WowApps\SlackBundle\Exception\SlackbotException;
32
use WowApps\SlackBundle\Service\SlackBot;
33
use WowApps\SlackBundle\Service\SlackColor;
34
use WowApps\SlackBundle\Service\SlackEmoji;
35
use WowApps\SlackBundle\Service\SlackMarkdown;
36
use WowApps\SlackBundle\Templating\Template\SlackException;
37
use WowApps\SlackBundle\WowAppsSlackBundle;
38
39
/**
40
 * @author Alexey Samara <[email protected]>
41
 */
42
class WowappsSlackbotTestCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
43
{
44
    const COMMAND_NAME = 'wowapps:slackbot:test';
45
46
    const M_SENDING_SKIPPED = 'Sending test message skipped';
47
    const M_NOT_SENT = 'Message not sent';
48
    const M_SENT = 'Test message sent successfully';
49
50
    /** @var SlackBot */
51
    private $slackBot;
52
53
    /** @var array */
54
    private $config;
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function configure()
60
    {
61
        $this
62
            ->setName(self::COMMAND_NAME)
63
            ->setDescription('Test your settings and try to send message')
64
            ->addOption('include-exception', null, InputOption::VALUE_NONE, 'Send template exception')
65
            ->addOption('skip-sending', null, InputOption::VALUE_NONE, 'Skip sending test message and test exception');
66
    }
67
68
    /**
69
     * @param InputInterface  $input
70
     * @param OutputInterface $output
71
     *
72
     * @return int|void|null
73
     */
74
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        /* Work with container for support of Symfony 3 early versions */
77
        $this->slackBot = $this->getContainer()->get('wowapps.slackbot');
78
        $this->config = $this->slackBot->getConfig();
79
80
        $this->drawHeader($output);
81
82
        $symfonyStyle = new SymfonyStyle($input, $output);
83
        $this->drawConfig($symfonyStyle);
84
85
        if ($input->getOption('skip-sending')) {
86
            $symfonyStyle->note(self::M_SENDING_SKIPPED);
87
88
            return;
89
        }
90
91
        if (!$this->sendTestMessage()) {
92
            $symfonyStyle->error(self::M_NOT_SENT);
93
94
            return;
95
        }
96
97
        $symfonyStyle->success(self::M_SENT);
98
99
        if ($input->getOption('include-exception')) {
100
            try {
101
                throw new SlackbotException(SlackbotException::E_CONVERT_MESSAGE_TO_JSON);
102
            } catch (SlackbotException $exception) {
103
                if (!$this->slackBot->sendTemplate(new SlackException($exception))) {
104
                    $symfonyStyle->error('Template exception not sent');
105
106
                    return;
107
                }
108
109
                $symfonyStyle->success('Template message sent successfully');
110
            }
111
        }
112
    }
113
114
    /**
115
     * @param OutputInterface $output
116
     */
117
    private function drawHeader(OutputInterface $output)
118
    {
119
        $output->writeln([
0 ignored issues
show
Documentation introduced by
array(PHP_EOL, '<bg=blac...ndle::CURRENT_VERSION)) is of type array<integer,string,{"0..."string","3":"string"}>, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
            PHP_EOL,
121
            '<bg=black;options=bold;fg=white>                                                       </>',
122
            '<bg=black;options=bold;fg=white>           S Y M F O N Y   S L A C K   B O T           </>',
123
            sprintf(
124
                '<bg=black;fg=white>                     version %s                     </>',
125
                WowAppsSlackBundle::CURRENT_VERSION
126
            ),
127
        ]);
128
    }
129
130
    /**
131
     * @param SymfonyStyle $symfonyStyle
132
     */
133
    private function drawConfig(SymfonyStyle $symfonyStyle)
134
    {
135
        $symfonyStyle->section('SlackBot configuration');
136
137
        $tBody = [
138
            ['Slack API url', $this->config['api_url']],
139
            ['Default icon url', $this->config['default_icon_url']],
140
            ['Default channel', $this->config['default_channel']],
141
            ['Default bot\'s name', $this->config['default_username']],
142
            ['Colors:', ''],
143
            ['   default', $this->config['colors']['default']],
144
            ['   info', $this->config['colors']['info']],
145
            ['   warning', $this->config['colors']['warning']],
146
            ['   success', $this->config['colors']['success']],
147
            ['   danger', $this->config['colors']['danger']],
148
            ['Templates configuration:', ''],
149
        ];
150
151
        foreach ($this->config['templates'] as $templateName => $templateConfig) {
152
            $tBody[] = ['   ' . $templateName . ':', ''];
153
            foreach ($templateConfig as $key => $value) {
154
                $tBody[] = ['      ' . $key, $value];
155
            }
156
        }
157
158
        $symfonyStyle->table(
159
            ['Parameter', 'Value'],
160
            $tBody
161
        );
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    private function sendTestMessage()
168
    {
169
        $slackMessage = new SlackMessage(
170
            'Simple Symfony 3 and 4 Bundle for sending customizable messages to Slack via '
171
            . SlackMarkdown::link('incoming webhooks', 'https://api.slack.com/incoming-webhooks')
172
        );
173
174
        $attachment = new Attachment();
175
        $attachment
176
            ->setColor(SlackColor::COLOR_DEFAULT)
177
            ->setAuthorName('WoW-Apps')
178
            ->setAuthorLink('https://wow-apps.pro/')
179
            ->setAuthorIconUrl('https://wow-apps.pro/img/favicon.png')
180
            ->setTitle('Symfony Slack Bot')
181
            ->setTitleLink('https://github.com/wow-apps/symfony-slack-bot')
182
            ->setText(
183
                'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut '
184
                    . 'labore et dolore magna aliqua. Morbi non arcu risus quis. At ultrices mi tempus imperdiet. '
185
                    . 'Suspendisse in est ante in nibh mauris cursus mattis molestie.'
186
            )
187
            ->setFooter('Alexey Samara')
188
            ->setFooterIconUrl('https://avatars2.githubusercontent.com/u/2779949?s=460&v=4')
189
            ->setTimestamp(time());
190
191
        $slackMessage->appendAttachment($attachment);
192
193
        $attachment = new Attachment();
194
        $attachment
195
            ->setColor(SlackColor::COLOR_INFO)
196
            ->appendField(new AttachmentField('Version:', WowAppsSlackBundle::CURRENT_VERSION, true))
197
            ->appendField(new AttachmentField('Build status:', 'passed', true))
198
            ->appendField(
199
                new AttachmentField(
200
                    'Run in terminal to install:',
201
                    SlackMarkdown::code(['composer require wow-apps/symfony-slack-bot']),
202
                    false
203
                )
204
            )
205
            ->appendAction(
206
                new AttachmentAction(
207
                    SlackEmoji::PEOPLE__FEMALE_TECHNOLOGIST . ' View documentation',
208
                    'https://wow-apps.github.io/symfony-slack-bot/docs'
209
                )
210
            )
211
            ->appendAction(
212
                new AttachmentAction(
213
                    SlackEmoji::PEOPLE__SMILEY_CAT . ' View source on GitHub',
214
                    'https://github.com/wow-apps/symfony-slack-bot'
215
                )
216
            )
217
            ->appendAction(
218
                new AttachmentAction(
219
                    SlackEmoji::PEOPLE__GHOST . ' Button with confirmation',
220
                    'https://cdn.shopify.com/s/files/1/1034/8911/products/'
221
                    . 's_8422_9TLbAj9PUhSRVCVAKCz7sHZcVYdpGyBlack.png?v=1473238696',
222
                    AttachmentAction::STYLE_PRIMARY,
223
                    new AttachmentActionConfirm(
224
                        true,
225
                        'Open funny image',
226
                        'Are you sure, you want to continue?',
227
                        'I\'m sure',
228
                        'No, turn me back'
229
                    )
230
                )
231
            );
232
233
        $slackMessage->appendAttachment($attachment);
234
235
        return $this->slackBot->send($slackMessage);
236
    }
237
}
238