Passed
Push — master ( 37b1fa...d5fc37 )
by Antonio Carlos
04:45 queued 02:03
created

Slack   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 69
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B send() 0 41 3
A makeGeolocation() 0 9 1
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
9
{
10
    /**
11
     * Make a geolocation model for the item.
12
     *
13
     * @param $item
14
     *
15
     * @return array
16
     */
17
    public function makeGeolocation($item)
18
    {
19
        return collect([
20
            config('firewall.notifications.message.geolocation.field_latitude')     => $item['geoIp']['latitude'],
21
            config('firewall.notifications.message.geolocation.field_longitude')    => $item['geoIp']['longitude'],
22
            config('firewall.notifications.message.geolocation.field_country_code') => $item['geoIp']['country_code'],
23
            config('firewall.notifications.message.geolocation.field_country_name') => $item['geoIp']['country_name'],
24
            config('firewall.notifications.message.geolocation.field_city')         => $item['geoIp']['city'],
25
        ])->filter()->toArray();
26
    }
27
28
    /**
29
     * Send a message.
30
     *
31
     * @param $notifiable
32
     * @param $item
33
     *
34
     * @return \Illuminate\Notifications\Messages\SlackMessage
35
     */
36
    public function send($notifiable, $item)
37
    {
38
        $message = (new SlackMessage())
39
            ->error()
40
            ->from(
41
                config('firewall.notifications.from.name'),
42
                config('firewall.notifications.from.icon_emoji')
43
            )
44
            ->content($this->getMessage($item))
45
            ->attachment(function ($attachment) use ($item) {
46
                $attachment->title(config('firewall.notifications.message.request_count.title'))
47
                            ->content(
48
                                sprintf(
49
                                    config('firewall.notifications.message.request_count.message'),
50
                                    $item['requestCount'],
51
                                    $item['firstRequestAt']->diffInSeconds(Carbon::now()),
52
                                    (string) $item['firstRequestAt']
53
                                )
54
                            );
55
            })
56
            ->attachment(function ($attachment) use ($item) {
57
                $attachment->title($title = config('firewall.notifications.message.uri.title'))
58
                           ->content($item['server']['REQUEST_URI']);
59
            })
60
            ->attachment(function ($attachment) use ($item) {
61
                $attachment->title(config('firewall.notifications.message.user_agent.title'))
62
                           ->content($item['userAgent']);
63
            })
64
            ->attachment(function ($attachment) use ($item) {
65
                $attachment->title(config('firewall.notifications.message.blacklisted.title'))
66
                           ->content($item['isBlacklisted'] ? 'YES' : 'NO');
67
            });
68
69
        if ($item['geoIp']) {
70
            $message->attachment(function ($attachment) use ($item) {
71
                $attachment->title(config('firewall.notifications.message.geolocation.title'))
72
                           ->fields($this->makeGeolocation($item));
73
            });
74
        }
75
76
        return $message;
77
    }
78
}
79