Completed
Pull Request — master (#3)
by Abdullah
04:06 queued 01:32
created

ZendeskChannel::send()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 13
cp 0.9231
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 2
crap 5.0113
1
<?php
2
3
namespace NotificationChannels\Zendesk;
4
5
use Zendesk\API\Client;
6
use Illuminate\Support\Arr;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification;
9
10
class ZendeskChannel
11
{
12
    /** @var Client */
13
    protected $client;
14
15
    /** @param Client $client */
16 3
    public function __construct(Client $client)
17
    {
18 3
        $this->client = $client;
19 3
    }
20
21
    /**
22
     * Send the given notification.
23
     *
24
     * @param mixed $notifiable
25
     * @param \Illuminate\Notifications\Notification $notification
26
     *
27
     * @throws \NotificationChannels\Zendesk\Exceptions\InvalidConfiguration
28
     * @throws \NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification
29
     */
30 3
    public function send($notifiable, Notification $notification)
31
    {
32 3
        $zendeskParameters = $notification->toZendesk($notifiable)->toArray();
1 ignored issue
show
Bug introduced by
The method toZendesk() 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...
33
34 3
        if (! isset($zendeskParameters['requester']['name']) || $zendeskParameters['requester']['name'] === '') {
35 1
            $routing = collect($notifiable->routeNotificationFor('Zendesk'));
36 1
            if (! Arr::has($routing, ['name', 'email'])) {
37
                return;
38
            }
39
40 1
            $zendeskParameters['requester']['name'] = $routing['name'];
41 1
            $zendeskParameters['requester']['email'] = $routing['email'];
42 1
        }
43
44 3
        $response = $this->client->tickets()->create($zendeskParameters);
45
46 3
        if ($response->getStatusCode() !== 200) {
47 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
48
        }
49 2
    }
50
}
51