Passed
Push — master ( 3d0d87...075b69 )
by Antonio Carlos
09:13
created

Slack::send()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 33
cts 33
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 29
nc 2
nop 2
crap 3
1
<?php
2
3
namespace PragmaRX\Firewall\Notifications\Channels;
4
5
use Carbon\Carbon;
6
use Illuminate\Notifications\Messages\SlackMessage;
7
8
class Slack extends BaseChannel implements Contract
9
{
10
    /**
11
     * Send a message.
12
     *
13
     * @param $notifiable
14
     * @param $item
15
     *
16
     * @return \Illuminate\Notifications\Messages\SlackMessage
17
     */
18 2
    public function send($notifiable, $item)
19
    {
20 2
        $message = (new SlackMessage())
21 2
            ->error()
22 2
            ->from(
23 2
                config('firewall.notifications.from.name'),
24 2
                config('firewall.notifications.from.icon_emoji')
25
            )
26 2
            ->content($this->getMessage($item))
27 2
            ->attachment(function ($attachment) use ($item) {
28 2
                $attachment->title(config('firewall.notifications.message.request_count.title'))
29 2
                            ->content(
30 2
                                sprintf(
31 2
                                    config('firewall.notifications.message.request_count.message'),
32 2
                                    $item['requestCount'],
33 2
                                    $item['firstRequestAt']->diffInSeconds(Carbon::now()),
34 2
                                    (string) $item['firstRequestAt']
35
                                )
36
                            );
37 2
            })
38 2
            ->attachment(function ($attachment) use ($item) {
39 2
                $attachment->title($title = config('firewall.notifications.message.uri.title'))
40 2
                           ->content($item['server']['REQUEST_URI']);
41 2
            })
42 2
            ->attachment(function ($attachment) use ($item) {
43 2
                $attachment->title(config('firewall.notifications.message.user_agent.title'))
44 2
                           ->content($item['userAgent']);
45 2
            })
46 2
            ->attachment(function ($attachment) use ($item) {
47 2
                $attachment->title(config('firewall.notifications.message.blacklisted.title'))
48 2
                           ->content($item['isBlacklisted'] ? 'YES' : 'NO');
49 2
            });
50
51 2
        if ($item['geoIp']) {
52 2
            $message->attachment(function ($attachment) use ($item) {
53 2
                $attachment->title(config('firewall.notifications.message.geolocation.title'))
54 2
                           ->fields($this->makeGeolocation($item));
55 2
            });
56
        }
57
58
        return $message;
59
    }
60
}
61