Completed
Push — master ( 09b728...428bab )
by Alexey
04:32
created

SlackbotTestCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 0
cts 43
cp 0
rs 8.9818
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $slackBotConfig = $slackBot->getConfig();
45
46
        $symfonyStyle = new SymfonyStyle($input, $output);
47
48
        echo PHP_EOL;
49
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
50
        $output->writeln('<bg=blue;options=bold;fg=white>           S L A C K B O T   T E S T           </>');
51
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
52
        echo PHP_EOL;
53
54
        $symfonyStyle->section('SlackBot general settings');
55
56
        $symfonyStyle->table(
57
            ['api url'],
58
            [[$slackBotConfig['api_url']]]
59
        );
60
61
        $symfonyStyle->table(
62
            ['default icon'],
63
            [[$slackBotConfig['default_icon']]]
64
        );
65
66
        $symfonyStyle->table(
67
            ['default recipient'],
68
            [[$slackBotConfig['default_channel']]]
69
        );
70
71
        $symfonyStyle->section('SlackBot quote colors');
72
73
        $symfonyStyle->table(
74
            ['default', 'info', 'warning', 'success', 'danger'],
75
            [
76
                [
77
                    $slackBotConfig['quote_color']['default'],
78
                    $slackBotConfig['quote_color']['info'],
79
                    $slackBotConfig['quote_color']['warning'],
80
                    $slackBotConfig['quote_color']['success'],
81
                    $slackBotConfig['quote_color']['danger']
82
                ]
83
            ]
84
        );
85
86
        $symfonyStyle->section('Sending short message...');
87
88
        if (!$this->sendTestMessage($slackBot)) {
89
            $symfonyStyle->error('Message not sent');
90
            return;
91
        }
92
93
        $symfonyStyle->success('Message sent successfully');
94
    }
95
96
    /**
97
     * @param SlackBot $slackBot
98
     * @return bool
99
     */
100
    private function sendTestMessage(SlackBot $slackBot)
101
    {
102
        $slackMessage = new SlackMessage();
103
104
        $quoteText = [
105
            'This is ' . $this->formatBold('test') . ' message sent by SlackBot',
106
            $this->formatCode([
107
                '<?php',
108
                '$someString = \'Hello world!\';',
109
                'echo $someString;'
110
            ])
111
        ];
112
113
        $slackMessage
114
            ->setIcon('http://cdn.wow-apps.pro/slackbot/slack-bot-icon-48.png')
115
            ->setText('If you read this - SlackBot is working!')
116
            ->setRecipient('general')
117
            ->setSender('WoW-Apps')
118
            ->setShowQuote(true)
119
            ->setQuoteType(SlackBot::QUOTE_SUCCESS)
120
            ->setQuoteText($this->inlineMultilines($quoteText))
121
            ->setQuoteTitle('SlackBot for Symfony 3')
122
            ->setQuoteTitleLink('https://github.com/wow-apps/symfony-slack-bot')
123
        ;
124
125
        return $slackBot->sendMessage($slackMessage);
126
    }
127
}
128