Completed
Pull Request — master (#23)
by
unknown
09:20
created

SlackWebhookChannel::fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Illuminate\Notifications\Channels;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Notifications\Messages\SlackMessage;
8
use Illuminate\Notifications\Messages\SlackAttachment;
9
10
class SlackWebhookChannel
11
{
12
    /**
13
     * The HTTP client instance.
14
     *
15
     * @var \GuzzleHttp\Client
16
     */
17
    protected $http;
18
19
    /**
20
     * Create a new Slack channel instance.
21
     *
22
     * @param  \GuzzleHttp\Client  $http
23
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
24
     */
25
    public function __construct(HttpClient $http)
26
    {
27
        $this->http = $http;
28
    }
29
30
    /**
31
     * Send the given notification.
32
     *
33
     * @param  mixed  $notifiable
34
     * @param  \Illuminate\Notifications\Notification  $notification
35
     * @return void
36
     */
37
    public function send($notifiable, Notification $notification)
38
    {
39
        if (! $url = $notifiable->routeNotificationFor('slack')) {
40
            return;
41
        }
42
43
        $message = $notification->toSlack($notifiable);
0 ignored issues
show
Bug introduced by
The method toSlack() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
45
        $this->http->post($url, [
46
            'json' => [
47
                'text' => $message->content,
48
                'attachments' => $this->attachments($message),
49
            ],
50
        ]);
51
    }
52
53
    /**
54
     * Format the message's attachments.
55
     *
56
     * @param  \Illuminate\Notifications\Messages\SlackMessage  $message
57
     * @return array
58
     */
59
    protected function attachments(SlackMessage $message)
60
    {
61
        return collect($message->attachments)->map(function ($attachment) use ($message) {
62
            return array_filter([
63
                'color' => $message->color(),
64
                'title' => $attachment->title,
65
                'text' => $attachment->content,
66
                'title_link' => $attachment->url,
67
                'fields' => $this->fields($attachment),
68
            ]);
69
        })->all();
70
    }
71
72
    /**
73
     * Format the attachment's fields.
74
     *
75
     * @param  \Illuminate\Notifications\Messages\SlackAttachment  $attachment
76
     * @return array
77
     */
78
    protected function fields(SlackAttachment $attachment)
79
    {
80
        return collect($attachment->fields)->map(function ($value, $key) {
81
            return ['title' => $key, 'value' => $value, 'short' => true];
82
        })->values()->all();
83
    }
84
}
85