Passed
Push — master ( 7f4da8...1c40c3 )
by Antonio Carlos
12:03
created

Slack::send()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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