Completed
Push — master ( 85b241...3d718b )
by Antonio Carlos
05:58 queued 02:30
created

Slack::send()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 3.016

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 29
cts 33
cp 0.8788
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 29
nc 2
nop 2
crap 3.016
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 1
    public function send($notifiable, $item)
19
    {
20 1
        $message = (new SlackMessage())
21 1
            ->error()
22 1
            ->from(
23 1
                config('firewall.notifications.from.name'),
24 1
                config('firewall.notifications.from.icon_emoji')
25
            )
26 1
            ->content($this->getMessage($item))
27 1
            ->attachment(function ($attachment) use ($item) {
28 1
                $attachment->title(config('firewall.notifications.message.request_count.title'))
29 1
                            ->content(
30 1
                                sprintf(
31 1
                                    config('firewall.notifications.message.request_count.message'),
32 1
                                    $item['requestCount'],
33 1
                                    $item['firstRequestAt']->diffInSeconds(Carbon::now()),
34 1
                                    (string) $item['firstRequestAt']
35
                                )
36
                            );
37 1
            })
38 1
            ->attachment(function ($attachment) use ($item) {
39 1
                $attachment->title($title = config('firewall.notifications.message.uri.title'))
40 1
                           ->content($item['server']['REQUEST_URI']);
41 1
            })
42 1
            ->attachment(function ($attachment) use ($item) {
43 1
                $attachment->title(config('firewall.notifications.message.user_agent.title'))
44 1
                           ->content($item['userAgent']);
45 1
            })
46 1
            ->attachment(function ($attachment) use ($item) {
47 1
                $attachment->title(config('firewall.notifications.message.blacklisted.title'))
48 1
                           ->content($item['isBlacklisted'] ? 'YES' : 'NO');
49 1
            });
50
51 1
        if ($item['geoIp']) {
52
            $message->attachment(function ($attachment) use ($item) {
53
                $attachment->title(config('firewall.notifications.message.geolocation.title'))
54
                           ->fields($this->makeGeolocation($item));
55
            });
56
        }
57
58
        return $message;
59
    }
60
}
61