Test Setup Failed
Push — master ( 2fd16d...50202e )
by Alexey
09:14
created

SlackbotTestCommand::sendTestMessage()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1
1
<?php
2
3
namespace Wowapps\SlackBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Style\SymfonyStyle;
9
use Wowapps\SlackBundle\DTO\SlackMessage;
10
use Wowapps\SlackBundle\Service\SlackBot;
11
use Wowapps\SlackBundle\Traits\SlackMessageTrait;
12
13
class SlackbotTestCommand extends ContainerAwareCommand
14
{
15
    use SlackMessageTrait;
16
17
    protected function configure()
18
    {
19
        $this
20
            ->setName('slackbot:test')
21
            ->setDescription('Test your settings and try to send messages')
22
        ;
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        /** @var SlackBot $slackBot */
28
        $slackBot = $this->getContainer()->get('wowapps.slackbot');
29
        $slackBotConfig = $slackBot->getConfig();
30
31
        $symfonyStyle = new SymfonyStyle($input, $output);
32
33
        echo PHP_EOL;
34
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
35
        $output->writeln('<bg=blue;options=bold;fg=white>           S L A C K B O T   T E S T           </>');
36
        $output->writeln('<bg=blue;options=bold;fg=white>                                               </>');
37
        echo PHP_EOL;
38
39
        $symfonyStyle->section('SlackBot general settings');
40
41
        $symfonyStyle->table(
42
            ['api url'],
43
            [[$slackBotConfig['api_url']]]
44
        );
45
46
        $symfonyStyle->table(
47
            ['default icon'],
48
            [[$slackBotConfig['default_icon']]]
49
        );
50
51
        $symfonyStyle->table(
52
            ['default recipient'],
53
            [[$slackBotConfig['default_channel']]]
54
        );
55
56
        $symfonyStyle->section('SlackBot quote colors');
57
58
        $symfonyStyle->table(
59
            ['default', 'info', 'warning', 'success', 'danger'],
60
            [
61
                [
62
                    $slackBotConfig['quote_color']['default'],
63
                    $slackBotConfig['quote_color']['info'],
64
                    $slackBotConfig['quote_color']['warning'],
65
                    $slackBotConfig['quote_color']['success'],
66
                    $slackBotConfig['quote_color']['danger']
67
                ]
68
            ]
69
        );
70
71
        $symfonyStyle->section('Sending short message...');
72
73
        if ($this->sendTestMessage($slackBot)) {
74
            $symfonyStyle->success('Message sent successfully');
75
        } else {
76
            $symfonyStyle->error('Message not sent');
77
        }
78
    }
79
80
    /**
81
     * @param SlackBot $slackBot
82
     * @return bool
83
     */
84
    private function sendTestMessage(SlackBot $slackBot)
85
    {
86
        $slackMessage = new SlackMessage();
87
88
        $quoteText = [
89
            'This is ' . $this->formatBold('test') . ' message sent by SlackBot',
90
            $this->formatCode([
91
                '<?php',
92
                '$someString = \'Hello world!\';',
93
                'echo $someString;'
94
            ])
95
        ];
96
97
        $slackMessage
98
            ->setIcon('http://cdn.wow-apps.pro/slackbot/slack-bot-icon-48.png')
99
            ->setText('If you read this - SlackBot is working!')
100
            ->setRecipient('general')
101
            ->setSender('WoW-Apps')
102
            ->setShowQuote(true)
103
            ->setQuoteType(SlackBot::QUOTE_SUCCESS)
104
            ->setQuoteText($this->inlineMultilines($quoteText))
105
            ->setQuoteTitle('SlackBot for Symfony 3')
106
            ->setQuoteTitleLink('https://github.com/wow-apps/symfony-slack-bot')
107
        ;
108
109
        return $slackBot->sendMessage($slackMessage);
110
    }
111
}
112