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

ZendeskChannel::prepareParameter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

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