Completed
Pull Request — master (#3)
by Abdullah
11:01 queued 03:09
created

ZendeskChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace NotificationChannels\Zendesk;
4
5
use Zendesk\API\Client;
6
use Illuminate\Support\Arr;
7
use Zendesk\API\HttpClient;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification;
10
11
class ZendeskChannel
12
{
13
    /** @var HttpClient */
14
    protected $client;
15
16
    /** @var array */
17
    protected $parameters;
18
19
    /** @param HttpClient $client */
20 1
    public function __construct(HttpClient $client)
21
    {
22 1
        $this->client = $client;
23 1
    }
24
25
    /**
26
     * Send the given notification.
27
     *
28
     * @param mixed $notifiable
29
     * @param \Illuminate\Notifications\Notification $notification
30
     *
31
     * @throws \NotificationChannels\Zendesk\Exceptions\InvalidConfiguration
32
     * @throws \NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification
33
     */
34 1
    public function send($notifiable, Notification $notification)
35
    {
36 1
        $this->parameters = $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...
37
38 1
        $this->prepareParameter($notifiable);
39
40
        try {
41 1
            $this->client->tickets()->create($this->parameters);
42 1
        } catch (\Exception $e) {
43
            throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
44
        }
45 1
    }
46
47
    /**
48
     * Prepare the parameters before to be send.
49
     *
50
     * @param mixed $notifiable
51
     */
52 1
    private function prepareParameter($notifiable)
53
    {
54
        // Check if the requester data is not set
55 1
        if (! isset($this->parameters['requester']['name']) || $this->parameters['requester']['name'] === '') {
56
            $routing = collect($notifiable->routeNotificationFor('Zendesk'));
57
            if (! Arr::has($routing, ['name', 'email'])) {
58
                return;
59
            }
60
61
            $this->parameters['requester']['name'] = $routing['name'];
62
            $this->parameters['requester']['email'] = $routing['email'];
63
        }
64 1
    }
65
}
66