WorkplaceChannel::send()   B
last analyzed

Complexity

Conditions 6
Paths 13

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.6829

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 11
cts 15
cp 0.7332
rs 8.8497
c 0
b 0
f 0
cc 6
nc 13
nop 2
crap 6.6829
1
<?php
2
3
namespace NotificationChannels\Workplace;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\ClientException;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Workplace\Exceptions\CouldNotSendNotification;
9
10
class WorkplaceChannel
11
{
12
    const FORMATTING_MARKDOWN = 'MARKDOWN';
13
14
    /** @var ClientInterface Http client. */
15
    protected $httpClient;
16
17 3
    public function __construct(ClientInterface $httpClient)
18
    {
19 3
        $this->httpClient = $httpClient;
20 3
    }
21
22
    /**
23
     * Send the given notification.
24
     *
25
     * @param mixed $notifiable
26
     * @param \Illuminate\Notifications\Notification $notification
27
     *
28
     * @throws \NotificationChannels\Workplace\Exceptions\CouldNotSendNotification
29
     */
30 3
    public function send($notifiable, Notification $notification)
31
    {
32 3
        if (! $to = $notifiable->routeNotificationFor('workplace', $notification)) {
33 1
            throw CouldNotSendNotification::endpointNotProvided();
34
        }
35
36 2
        $message = $notification->toWorkplace($notifiable);
0 ignored issues
show
Bug introduced by
The method toWorkplace() 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...
37
38 2
        if (is_string($message)) {
39 1
            $message = new WorkplaceMessage($message);
40
        }
41
42
        $body = [
43 2
            'message' => $message->getContent(),
44 2
            'formatting' => ($message->isMarkdown() ? static::FORMATTING_MARKDOWN : null),
45
        ];
46
47
        try {
48 2
            return $this->httpClient->post(
49 2
                $to,
50 2
                ['json' => $body]
51
            );
52
        } catch (ClientException $exception) {
53
            throw CouldNotSendNotification::workplaceRespondedWithAnError($exception);
54
        } catch (\Exception $exception) {
55
            throw CouldNotSendNotification::couldNotCommunicateWithWorkplace($exception);
56
        }
57
    }
58
}
59