Completed
Pull Request — master (#3)
by Abdullah
06:13
created

ZendeskChannel::prepareParameter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8.7414

Importance

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