Completed
Push — master ( 4f8e9e...aed332 )
by Andrey
02:56
created

IntercomChannel::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace NotificationChannels\Intercom;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use GuzzleHttp\Exception\GuzzleException;
7
use Http\Client\Exception as HttpClientException;
8
use Illuminate\Notifications\Notification;
9
use Intercom\IntercomClient;
10
use NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException;
11
use NotificationChannels\Intercom\Exceptions\RequestException;
12
13
/**
14
 * Class IntercomNotificationChannel.
15
 */
16
class IntercomChannel
17
{
18
    /**
19
     * @var IntercomClient
20
     */
21
    private $client;
22
23
    /**
24
     * @param IntercomClient $client
25
     */
26 6
    public function __construct(IntercomClient $client)
27
    {
28 6
        $this->client = $client;
29 6
    }
30
31
    /**
32
     * Send the given notification via Intercom API.
33
     *
34
     * @param mixed        $notifiable
35
     * @param Notification $notification
36
     *
37
     * @return void
38
     *
39
     * @throws MessageIsNotCompleteException When message is not filled correctly
40
     * @throws GuzzleException               Other Guzzle uncatched exceptions
41
     * @throws HttpClientException           Other HTTP uncatched exceptions
42
     * @throws RequestException              When server responses with a bad HTTP code
43
     *
44
     * @see https://developers.intercom.com/intercom-api-reference/reference#admin-initiated-conversation
45
     */
46 5
    public function send($notifiable, Notification $notification): void
47
    {
48
        try {
49 5
            $this->sendNotification($notifiable, $notification);
50 3
        } catch (BadResponseException $exception) {
51 1
            throw new RequestException($exception, $exception->getMessage(), $exception->getCode());
52
        }
53 2
    }
54
55
    /**
56
     * @return IntercomClient
57
     */
58
    public function getClient(): IntercomClient
59
    {
60
        return $this->client;
61
    }
62
63
    /**
64
     * @param mixed        $notifiable
65
     * @param Notification $notification
66
     *
67
     * @throws MessageIsNotCompleteException
68
     * @throws GuzzleException
69
     * @throws HttpClientException
70
     */
71 5
    protected function sendNotification($notifiable, Notification $notification): void
72
    {
73
        /** @var IntercomMessage $message */
74 5
        $message = $notification->toIntercom($notifiable);
0 ignored issues
show
Bug introduced by
The method toIntercom() 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...
75 5
        if (! $message->toIsGiven()) {
76 2
            if (false === $to = $notifiable->routeNotificationFor('intercom')) {
77 1
                throw new MessageIsNotCompleteException($message, 'Recipient is not provided');
78
            }
79
80 1
            $message->to($to);
81
        }
82
83 4
        if (! $message->isValid()) {
84 1
            throw new MessageIsNotCompleteException(
85 1
                $message,
86 1
                'The message is not valid. Please check that you have filled required params'
87
            );
88
        }
89
90 3
        $this->client->messages->create(
91 3
            $message->toArray()
92
        );
93 2
    }
94
}
95