Completed
Pull Request — master (#14)
by Abdullah
02:46 queued 53s
created

ZendeskChannel::createNewTicket()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 2
eloc 7
nc 3
nop 1
crap 2.0932
1
<?php
2
3
namespace NotificationChannels\Zendesk;
4
5
use Illuminate\Support\Arr;
6
use Zendesk\API\HttpClient;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Zendesk\Events\ZendeskTicketWasCreated;
9
use NotificationChannels\Zendesk\Events\ZendeskTicketWasUpdated;
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 3
    public function __construct(HttpClient $client)
22
    {
23 3
        $this->client = $client;
24 3
    }
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 3
    public function send($notifiable, Notification $notification)
36
    {
37 3
        $this->parameters = $notification->toZendesk($notifiable)->toArray();
38
39 3
        $id = $this->parameters['ticket'];
40
41 3
        if (! is_null($id)) {
42 1
            return $this->updateTicket($id);
43
        }
44
45 2
        return $this->createNewTicket($notifiable);
46
    }
47
48
    /**
49
     * Send update ticket request.
50
     *
51
     * @param int $id
52
     */
53 1 View Code Duplication
    private function updateTicket($id)
54
    {
55 1
        $this->prepareUpdateParameters();
56
57
        try {
58 1
            $this->client->tickets()->update($id, $this->parameters);
59
60 1
            event(new ZendeskTicketWasUpdated($response));
0 ignored issues
show
Bug introduced by
The variable $response does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61 1
        } catch (\Exception $e) {
62 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
63
        }
64
    }
65
66
    /**
67
     * Send create ticket request.
68
     *
69
     * @param mixed $notifiable
70
     */
71 2 View Code Duplication
    private function createNewTicket($notifiable)
72
    {
73 2
        $this->prepareCreateParameter($notifiable);
74
75
        try {
76 2
            $response = $this->client->tickets()->create($this->parameters);
77
78 2
            event(new ZendeskTicketWasCreated($response));
79
        } catch (\Exception $e) {
80
            throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
81
        }
82 2
    }
83
84
    /**
85
     * Prepare the parameters before update request send.
86
     */
87 1
    public function prepareUpdateParameters()
88
    {
89 1
        unset($this->parameters['subject'], $this->parameters['requester'], $this->parameters['description'], $this->parameters['ticket']);
90 1
    }
91
92
    /**
93
     * Prepare the parameters before create request send.
94
     *
95
     * @param mixed $notifiable
96
     */
97 2
    private function prepareCreateParameter($notifiable)
98
    {
99
        // Check if the requester data is not set
100 2
        if (! isset($this->parameters['requester']['name']) || $this->parameters['requester']['name'] === '') {
101 1
            $routing = collect($notifiable->routeNotificationFor('Zendesk'));
102 1
            if (! Arr::has($routing, ['name', 'email'])) {
103
                return;
104
            }
105
106 1
            $this->parameters['requester']['name'] = $routing['name'];
107 1
            $this->parameters['requester']['email'] = $routing['email'];
108
        }
109
110 2
        unset($this->parameters['ticket']);
111 2
    }
112
}
113