Test Failed
Push — master ( bba394...360ac5 )
by Alexey
05:40
created

SlackbotTestCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 131
ccs 0
cts 81
cp 0
rs 10
c 0
b 0
f 0

6 Methods

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