Completed
Pull Request — master (#4)
by Tim
05:37
created

IntercomChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace FtwSoft\NotificationChannels\Intercom;
4
5
use Intercom\IntercomClient;
6
use Illuminate\Notifications\Notification;
7
use GuzzleHttp\Exception\BadResponseException;
8
use Illuminate\Notifications\Events\NotificationFailed;
9
use FtwSoft\NotificationChannels\Intercom\Exceptions\RequestException;
10
use FtwSoft\NotificationChannels\Intercom\Contracts\IntercomNotification;
11
use FtwSoft\NotificationChannels\Intercom\Exceptions\InvalidArgumentException;
12
use FtwSoft\NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException;
13
14
/**
15
 * Class IntercomNotificationChannel.
16
 */
17
class IntercomChannel
18
{
19
    /**
20
     * @var \Intercom\IntercomClient
21
     */
22
    private $client;
23
24
    /**
25
     * IntercomNotificationChannel constructor.
26
     *
27
     * @param \Intercom\IntercomClient $client
28
     */
29 8
    public function __construct(IntercomClient $client)
30
    {
31 8
        $this->client = $client;
32 8
    }
33
34
    /**
35
     * Send the given notification via Intercom API.
36
     *
37
     * @param mixed                                  $notifiable
38
     * @param \Illuminate\Notifications\Notification $notification
39
     *
40
     * @throws \FtwSoft\NotificationChannels\Intercom\Exceptions\RequestException              When server responses with a bad HTTP
41
     *                                                                                         code
42
     * @throws \FtwSoft\NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException When message is not
43
     *                                                                                         filled correctly
44
     * @throws \GuzzleHttp\Exception\GuzzleException                                           Other Guzzle uncatched exceptions
45
     *
46
     * @return void
47
     *
48
     * @see https://developers.intercom.com/intercom-api-reference/reference#admin-initiated-conversation
49
     */
50 6
    public function send($notifiable, Notification $notification): void
51
    {
52
        try {
53 6
            if (! $notification instanceof IntercomNotification) {
54 1
                throw new InvalidArgumentException(
55 1
                    sprintf('The notification must implement %s interface', IntercomNotification::class)
56
                );
57
            }
58
59 5
            $message = $notification->toIntercom($notifiable);
60
61 5
            if (! $message->toIsGiven()) {
62 2
                if (! $to = $notifiable->routeNotificationFor('intercom')) {
63 1
                    throw new MessageIsNotCompleteException($message, 'Recipient is not provided');
64
                }
65
66 1
                $message->to($to);
67
            }
68
69 4
            if (! $message->isValid()) {
70 1
                throw new MessageIsNotCompleteException(
71 1
                    $message,
72 1
                    'The message is not valid. Please check that you have filled required params'
73
                );
74
            }
75
76 3
            $this->client->messages->create(
77 3
                $message->toArray()
78
            );
79 4
        } catch (BadResponseException $exception) {
80 1
            event(new NotificationFailed($notifiable, $notification, $this, [
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            /** @scrutinizer ignore-call */ 
81
            event(new NotificationFailed($notifiable, $notification, $this, [
Loading history...
Bug introduced by
$this of type FtwSoft\NotificationChan...ntercom\IntercomChannel is incompatible with the type string expected by parameter $channel of Illuminate\Notifications...onFailed::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            event(new NotificationFailed($notifiable, $notification, /** @scrutinizer ignore-type */ $this, [
Loading history...
81
                    'message' => $message,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
82
                    'error' => $exception->getMessage()
83
                ])
84
            );
85
86
            throw new RequestException($exception, $exception->getMessage(), $exception->getCode());
87
        }
88 2
    }
89
90
    /**
91
     * @return \Intercom\IntercomClient
92
     */
93 1
    public function getClient(): IntercomClient
94
    {
95 1
        return $this->client;
96
    }
97
}
98