Completed
Push — master ( 42b6f8...140d5a )
by Antonio Carlos
06:38
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 4
    public function send($notifiable, $item)
19
    {
20 4
        $message = (new SlackMessage())
21 4
            ->error()
22 4
            ->from(
23 4
                config('firewall.notifications.from.name'),
24 4
                config('firewall.notifications.from.icon_emoji')
25
            )
26 4
            ->content($this->getMessage($item))
27 4
            ->attachment(function ($attachment) use ($item) {
28 4
                $attachment->title(config('firewall.notifications.message.request_count.title'))
29 4
                            ->content(
30 4
                                sprintf(
31 4
                                    config('firewall.notifications.message.request_count.message'),
32 4
                                    $item['requestCount'],
33 4
                                    $item['firstRequestAt']->diffInSeconds(Carbon::now()),
34 4
                                    (string) $item['firstRequestAt']
35
                                )
36
                            );
37 4
            })
38 4
            ->attachment(function ($attachment) use ($item) {
39 4
                $attachment->title($title = config('firewall.notifications.message.uri.title'))
40 4
                           ->content($item['server']['REQUEST_URI']);
41 4
            })
42 4
            ->attachment(function ($attachment) use ($item) {
43 4
                $attachment->title(config('firewall.notifications.message.user_agent.title'))
44 4
                           ->content($item['userAgent']);
45 4
            })
46 4
            ->attachment(function ($attachment) use ($item) {
47 4
                $attachment->title(config('firewall.notifications.message.blacklisted.title'))
48 4
                           ->content($item['isBlacklisted'] ? 'YES' : 'NO');
49 4
            });
50
51 4
        if ($item['geoIp']) {
52 3
            $message->attachment(function ($attachment) use ($item) {
53 3
                $attachment->title(config('firewall.notifications.message.geolocation.title'))
54 3
                           ->fields($this->makeGeolocation($item));
55 3
            });
56
        }
57
58
        return $message;
59
    }
60
}
61