1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DigitalEquation\Teamwork\Services; |
4
|
|
|
|
5
|
|
|
use DigitalEquation\Teamwork\Exceptions\TeamworkHttpException; |
6
|
|
|
use DigitalEquation\Teamwork\Exceptions\TeamworkParameterException; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use GuzzleHttp\Psr7\Stream; |
11
|
|
|
|
12
|
|
|
class Tickets |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \GuzzleHttp\Client |
16
|
|
|
*/ |
17
|
|
|
private $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Tickets constructor. |
21
|
|
|
* |
22
|
|
|
* @param \GuzzleHttp\Client $client |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Client $client) |
25
|
|
|
{ |
26
|
|
|
$this->client = $client; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get tickets priorities. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function priorities(): string |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
/** @var Response $response */ |
39
|
|
|
$response = $this->client->get('ticketpriorities.json'); |
40
|
|
|
/** @var Stream $body */ |
41
|
|
|
$body = $response->getBody(); |
42
|
|
|
|
43
|
|
|
return $body->getContents(); |
44
|
|
|
} catch (ClientException $e) { |
45
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get a list of tickets for a customer. |
51
|
|
|
* |
52
|
|
|
* @param int $customerId |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function customer($customerId): string |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
/** @var Response $response */ |
61
|
|
|
$response = $this->client->get(sprintf('customers/%s/previoustickets.json', $customerId)); |
62
|
|
|
/** @var Stream $body */ |
63
|
|
|
$body = $response->getBody(); |
64
|
|
|
|
65
|
|
|
return $body->getContents(); |
66
|
|
|
} catch (ClientException $e) { |
67
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Send a ticket to teamwork desk. |
73
|
|
|
* |
74
|
|
|
* @param array $data |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function post($data): string |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
/** @var Response $response */ |
83
|
|
|
$response = $this->client->post('tickets.json', [ |
84
|
|
|
'form_params' => $data, |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
/** @var Stream $body */ |
88
|
|
|
$body = $response->getBody(); |
89
|
|
|
|
90
|
|
|
return $body->getContents(); |
91
|
|
|
} catch (ClientException $e) { |
92
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Post a reply to a ticket. |
98
|
|
|
* |
99
|
|
|
* @param array $data |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
103
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkParameterException |
104
|
|
|
*/ |
105
|
|
|
public function reply(array $data): string |
106
|
|
|
{ |
107
|
|
|
if (empty($data['ticketId'])) { |
108
|
|
|
throw new TeamworkParameterException('The `reply` method expects the passed array param to contain `ticketId`', 400); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
try { |
112
|
|
|
/** @var Response $response */ |
113
|
|
|
$response = $this->client->post(sprintf('tickets/%s.json', $data['ticketId']), [ |
114
|
|
|
'form_params' => $data, |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
/** @var Stream $body */ |
118
|
|
|
$body = $response->getBody(); |
119
|
|
|
|
120
|
|
|
return $body->getContents(); |
121
|
|
|
} catch (ClientException $e) { |
122
|
|
|
throw new TeamworkHttpException($e->getMessage()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get ticket by id. |
128
|
|
|
* |
129
|
|
|
* @param int $ticketId |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function ticket($ticketId): string |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
try { |
137
|
|
|
/** @var Response $response */ |
138
|
|
|
$response = $this->client->get(sprintf('tickets/%s.json', $ticketId)); |
139
|
|
|
/** @var Stream $body */ |
140
|
|
|
$body = $response->getBody(); |
141
|
|
|
|
142
|
|
|
return $body->getContents(); |
143
|
|
|
} catch (ClientException $e) { |
144
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.