Passed
Push — master ( ae08b1...91bc2f )
by Alexey
04:51
created

WowappsSlackbotTestCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 128
ccs 0
cts 83
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 19 2
A drawHeader() 0 8 1
A drawConfig() 0 34 1
A sendTestMessage() 0 31 1
1
<?php
2
/**
3
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3
4
 * https://github.com/wow-apps/symfony-slack-bot
5
 *
6
 * (c) 2016 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\SlackBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
use WowApps\SlackBundle\DTO\SlackMessage;
19
use WowApps\SlackBundle\Service\SlackBot;
20
use WowApps\SlackBundle\Service\SlackEmoji;
21
use WowApps\SlackBundle\Traits\SlackMessageTrait;
22
23
/**
24
 * @author Alexey Samara <[email protected]>
25
 * @package WowApps\SlackBundle
26
 * @see https://github.com/wow-apps/symfony-slack-bot/wiki/1.-Installation#send-test-message
27
 */
28
class WowappsSlackbotTestCommand extends ContainerAwareCommand
29
{
30
    use SlackMessageTrait;
31
32
    /** @var SlackBot */
33
    private $slackBot;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('wowapps:slackbot:test')
42
            ->setDescription('Test your settings and try to send messages');
43
    }
44
45
    /**
46
     * @param InputInterface $input
47
     * @param OutputInterface $output
48
     * @return int|null|void
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        /** Dirty hack for support in Symfony before version 3.4 */
53
        $this->slackBot = $this->getContainer()->get('wowapps.slackbot');
54
55
        $this->drawHeader($output);
56
57
        $symfonyStyle = new SymfonyStyle($input, $output);
58
        $this->drawConfig($symfonyStyle, $this->slackBot->getConfig());
59
60
        $symfonyStyle->section('Sending short message...');
61
62
        if (!$this->sendTestMessage()) {
63
            $symfonyStyle->error('Message not sent');
64
            return;
65
        }
66
67
        $symfonyStyle->success('Message sent successfully');
68
    }
69
70
    /**
71
     * @param OutputInterface $output
72
     */
73
    private function drawHeader(OutputInterface $output)
74
    {
75
        echo PHP_EOL;
76
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
77
        $output->writeln('<bg=blue;options=bold;fg=white>           S L A C K B O T   T E S T           </>');
78
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
79
        echo PHP_EOL;
80
    }
81
82
    /**
83
     * @param SymfonyStyle $symfonyStyle
84
     * @param array $slackBotConfig
85
     */
86
    private function drawConfig(SymfonyStyle $symfonyStyle, array $slackBotConfig)
87
    {
88
        $symfonyStyle->section('SlackBot general settings');
89
90
        $symfonyStyle->table(
91
            ['api url'],
92
            [[$slackBotConfig['api_url']]]
93
        );
94
95
        $symfonyStyle->table(
96
            ['default icon'],
97
            [[$slackBotConfig['default_icon']]]
98
        );
99
100
        $symfonyStyle->table(
101
            ['default recipient'],
102
            [[$slackBotConfig['default_channel']]]
103
        );
104
105
        $symfonyStyle->section('SlackBot quote colors');
106
107
        $symfonyStyle->table(
108
            ['default', 'info', 'warning', 'success', 'danger'],
109
            [
110
                [
111
                    $slackBotConfig['quote_color']['default'],
112
                    $slackBotConfig['quote_color']['info'],
113
                    $slackBotConfig['quote_color']['warning'],
114
                    $slackBotConfig['quote_color']['success'],
115
                    $slackBotConfig['quote_color']['danger']
116
                ]
117
            ]
118
        );
119
    }
120
121
    /**
122
     * @return bool
123
     */
124
    private function sendTestMessage()
125
    {
126
        $slackMessage = new SlackMessage();
127
128
        $quoteText = [
129
            sprintf('This is %s message sent by SlackBot', $this->formatBold('test')),
130
            $this->formatCode([
131
                '<?php',
132
                '$someString = \'Hello world!\';',
133
                'echo $someString;'
134
            ])
135
        ];
136
137
        $reflectionClass = new \ReflectionClass(SlackEmoji::class);
138
        $iconsArray = array_values($reflectionClass->getConstants());
139
        $randomIcon = $iconsArray[rand(0, (count($iconsArray) - 1))];
140
141
        $slackMessage
142
            ->setIconEmoji($randomIcon)
143
            ->setText('If you read this - SlackBot is working!')
144
            ->setRecipient('general')
145
            ->setSender('WoW-Apps')
146
            ->setShowQuote(true)
147
            ->setQuoteType(SlackBot::QUOTE_SUCCESS)
148
            ->setQuoteText($this->inlineMultilines($quoteText))
149
            ->setQuoteTitle('SlackBot for Symfony 3')
150
            ->setQuoteTitleLink('https://github.com/wow-apps/symfony-slack-bot')
151
        ;
152
153
        return $this->slackBot->sendMessage($slackMessage);
154
    }
155
}
156