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 |
|
$response = $this->client->tickets()->update($id, $this->parameters); |
59
|
|
|
|
60
|
1 |
|
event(new ZendeskTicketWasUpdated($response)); |
61
|
|
|
} catch (\Exception $e) { |
62
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage()); |
63
|
|
|
} |
64
|
1 |
|
} |
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->prepareCreateParameters($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'], $this->parameters['type'], $this->parameters['group_id']); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Prepare the parameters before create request send. |
94
|
|
|
* |
95
|
|
|
* @param mixed $notifiable |
96
|
|
|
*/ |
97
|
2 |
|
private function prepareCreateParameters($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
|
|
|
|