Test Failed
Push — master ( bd1e8a...fed951 )
by Alexey
12:42 queued 12s
created

SlackException::getButtons()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony.
5
 * https://github.com/wow-apps/symfony-slack-bot
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * https://github.com/wow-apps/symfony-slack-bot/blob/master/LICENSE
10
 *
11
 * For technical documentation.
12
 * https://wow-apps.github.io/symfony-slack-bot/docs/
13
 *
14
 * Author Alexey Samara <[email protected]>
15
 *
16
 * Copyright 2016 WoW-Apps.
17
 */
18
19
namespace WowApps\SlackBundle\Templating\Template;
20
21
use WowApps\SlackBundle\DTO\Attachment;
22
use WowApps\SlackBundle\DTO\AttachmentAction;
23
use WowApps\SlackBundle\DTO\AttachmentField;
24
use WowApps\SlackBundle\DTO\SlackMessage;
25
use WowApps\SlackBundle\Service\SlackColor;
26
use WowApps\SlackBundle\Service\SlackEmoji;
27
use WowApps\SlackBundle\Service\SlackMarkdown;
28
use WowApps\SlackBundle\Templating\SlackTemplateInterface;
29
30
class SlackException implements SlackTemplateInterface
31
{
32
    /** @var \Exception */
33
    private $payload;
34
35
    /** @var bool */
36
    private $includeTrace;
37
38
    /** @var bool */
39
    private $searchButtons;
40
41
    /** @var array */
42
    private $searchSources = [
43
        SlackEmoji::OBJECTS__MAG . ' Search on Google' => 'https://www.google.com/search?q=%s',
44
        SlackEmoji::OBJECTS__MAG . ' Search on StackOverflow' => 'https://stackoverflow.com/search?q=%s',
45
        SlackEmoji::OBJECTS__MAG . ' Search on Symfony.com' => 'https://symfony.com/search?q=%s',
46
    ];
47
48
    /**
49
     * ExceptionTemplate constructor.
50
     *
51
     * @param \Exception $payload
52
     * @param bool       $includeTrace
53
     * @param bool       $searchButtons
54
     */
55
    public function __construct(\Exception $payload, bool $includeTrace = true, bool $searchButtons = true)
56
    {
57
        $this->payload = $payload;
58
        $this->includeTrace = $includeTrace;
59
        $this->searchButtons = $searchButtons;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getConfigIndex(): string
66
    {
67
        return 'exception';
68
    }
69
70
    /**
71
     * @return SlackMessage
72
     */
73
    public function getMessage(): SlackMessage
74
    {
75
        $slackMessage = new SlackMessage();
76
77
        $slackMessage
78
            ->setMarkdown(true)
79
            ->setText($this->payload->getMessage());
80
81
        $slackMessage->appendAttachment($this->getExceptionBody());
82
83
        if ($this->includeTrace) {
84
            $slackMessage->appendAttachment($this->getExceptionTrace());
85
        }
86
87
        return $slackMessage;
88
    }
89
90
    /**
91
     * @return Attachment
92
     */
93
    private function getExceptionBody(): Attachment
94
    {
95
        $exceptionBody = new Attachment();
96
        $exceptionBody
97
            ->setColor(SlackColor::COLOR_DANGER)
98
            ->setFields([
99
                new AttachmentField(
100
                    'File:',
101
                    SlackMarkdown::italic($this->payload->getFile())
102
                ),
103
                new AttachmentField(
104
                    'Line number:',
105
                    $this->payload->getLine(),
106
                    true
107
                ),
108
                new AttachmentField(
109
                    'Exception Code #:',
110
                    $this->payload->getCode(),
111
                    true
112
                ),
113
            ]);
114
115
        if ($this->searchButtons) {
116
            $exceptionBody->setActions($this->getButtons());
117
        }
118
119
        return $exceptionBody;
120
    }
121
122
    /**
123
     * @return Attachment
124
     */
125
    private function getExceptionTrace(): Attachment
126
    {
127
        $exceptionTrace = new Attachment();
128
129
        $exceptionTrace
130
            ->setColor(SlackColor::COLOR_DEFAULT)
131
            ->setPretext(SlackMarkdown::bold('Trace:'))
132
            ->setText(SlackMarkdown::multilines($this->getPayloadTrace()));
133
134
        return $exceptionTrace;
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    private function getPayloadTrace(): array
141
    {
142
        $trace = [];
143
144
        foreach ($this->payload->getTrace() as $traceStepNumber => $traceStep) {
145
            $trace[] = sprintf(
146
                '[%d] %s:%d',
147
                ++$traceStepNumber,
148
                SlackMarkdown::italic($traceStep['file']),
149
                $traceStep['line']
150
            );
151
            $trace[] = SlackMarkdown::inlineCode(
152
                $traceStep['class'] . $traceStep['type'] . $traceStep['function'] . '()'
153
            );
154
            $trace[] = SlackMarkdown::newLine();
155
        }
156
157
        return $trace;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    private function getButtons(): array
164
    {
165
        $buttons = [];
166
167
        $exceptionMessage = $this->payload->getMessage();
168
169
        if (preg_match('/\\n/i', $exceptionMessage)) {
170
            $exceptionMessageArray = explode("\n", $exceptionMessage);
171
            $exceptionMessage = $exceptionMessageArray[0];
172
        }
173
174
        $searchText = urlencode($exceptionMessage);
175
176
        foreach ($this->searchSources as $buttonName => $buttonUrl) {
177
            $buttons[] = new AttachmentAction(
178
                $buttonName,
179
                sprintf($buttonUrl, $searchText)
180
            );
181
        }
182
183
        return $buttons;
184
    }
185
}
186