Completed
Push — master ( 794090...47d2f5 )
by Alexey
03:06
created

SlackbotTestCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 13
cp 0
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\Traits\SlackMessageTrait;
21
22
/**
23
 * Class SlackbotTestCommand
24
 *
25
 * @author Alexey Samara <[email protected]>
26
 * @package WowApps\SlackBundle
27
 */
28
class SlackbotTestCommand extends ContainerAwareCommand
29
{
30
    use SlackMessageTrait;
31
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('wowapps:slackbot:test')
36
            ->setDescription('Test your settings and try to send messages')
37
        ;
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        /** @var SlackBot $slackBot */
43
        $slackBot = $this->getContainer()->get('wowapps.slackbot');
44
        $symfonyStyle = new SymfonyStyle($input, $output);
45
46
        $this->drawHeader($output);
47
48
        $this->drawConfig($symfonyStyle, $slackBot->getConfig());
49
50
        $symfonyStyle->section('Sending short message...');
51
52
        if (!$this->sendTestMessage($slackBot)) {
53
            $symfonyStyle->error('Message not sent');
54
            return;
55
        }
56
57
        $symfonyStyle->success('Message sent successfully');
58
    }
59
60
    /**
61
     * @param OutputInterface $output
62
     */
63
    private function drawHeader(OutputInterface $output)
64
    {
65
        echo PHP_EOL;
66
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
67
        $output->writeln('<bg=blue;options=bold;fg=white>           S L A C K B O T   T E S T           </>');
68
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
69
        echo PHP_EOL;
70
    }
71
72
    private function drawConfig(SymfonyStyle $symfonyStyle, array $slackBotConfig)
73
    {
74
        $symfonyStyle->section('SlackBot general settings');
75
76
        $symfonyStyle->table(
77
            ['api url'],
78
            [[$slackBotConfig['api_url']]]
79
        );
80
81
        $symfonyStyle->table(
82
            ['default icon'],
83
            [[$slackBotConfig['default_icon']]]
84
        );
85
86
        $symfonyStyle->table(
87
            ['default recipient'],
88
            [[$slackBotConfig['default_channel']]]
89
        );
90
91
        $symfonyStyle->section('SlackBot quote colors');
92
93
        $symfonyStyle->table(
94
            ['default', 'info', 'warning', 'success', 'danger'],
95
            [
96
                [
97
                    $slackBotConfig['quote_color']['default'],
98
                    $slackBotConfig['quote_color']['info'],
99
                    $slackBotConfig['quote_color']['warning'],
100
                    $slackBotConfig['quote_color']['success'],
101
                    $slackBotConfig['quote_color']['danger']
102
                ]
103
            ]
104
        );
105
    }
106
107
    /**
108
     * @param SlackBot $slackBot
109
     * @return bool
110
     */
111
    private function sendTestMessage(SlackBot $slackBot)
112
    {
113
        $slackMessage = new SlackMessage();
114
115
        $quoteText = [
116
            sprintf('This is %s message sent by SlackBot', $this->formatBold('test')),
117
            $this->formatCode([
118
                '<?php',
119
                '$someString = \'Hello world!\';',
120
                'echo $someString;'
121
            ])
122
        ];
123
124
        $slackMessage
125
            ->setIcon('http://cdn.wow-apps.pro/slackbot/slack-bot-icon-48.png')
126
            ->setText('If you read this - SlackBot is working!')
127
            ->setRecipient('general')
128
            ->setSender('WoW-Apps')
129
            ->setShowQuote(true)
130
            ->setQuoteType(SlackBot::QUOTE_SUCCESS)
131
            ->setQuoteText($this->inlineMultilines($quoteText))
132
            ->setQuoteTitle('SlackBot for Symfony 3')
133
            ->setQuoteTitleLink('https://github.com/wow-apps/symfony-slack-bot')
134
        ;
135
136
        return $slackBot->sendMessage($slackMessage);
137
    }
138
}
139