1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DigitalEquation\Teamwork\Services; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Stream; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use Illuminate\Support\Facades\File; |
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
10
|
|
|
use DigitalEquation\Teamwork\Exceptions\TeamworkHttpException; |
11
|
|
|
use DigitalEquation\Teamwork\Exceptions\TeamworkInboxException; |
12
|
|
|
use DigitalEquation\Teamwork\Exceptions\TeamworkUploadException; |
13
|
|
|
|
14
|
|
|
class Desk |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \GuzzleHttp\Client |
18
|
|
|
*/ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Desk constructor. |
23
|
|
|
* |
24
|
|
|
* @param \GuzzleHttp\Client $client |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Client $client) |
27
|
|
|
{ |
28
|
|
|
$this->client = $client; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return an inbox by name. |
33
|
|
|
* |
34
|
|
|
* @param string $name |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
38
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkInboxException |
39
|
|
|
*/ |
40
|
|
|
public function inbox($name): array |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
/** @var Response $response */ |
44
|
|
|
$response = $this->client->get('inboxes.json'); |
45
|
|
|
/** @var Stream $body */ |
46
|
|
|
$body = $response->getBody(); |
47
|
|
|
$inboxes = json_decode($body->getContents(), true); |
48
|
|
|
|
49
|
|
|
$inbox = collect($inboxes['inboxes'])->first( |
50
|
|
|
function ($inbox) use ($name) { |
51
|
|
|
return $inbox['name'] === $name; |
52
|
|
|
} |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if (! $inbox) { |
56
|
|
|
throw new TeamworkInboxException("No inbox found with the name: $name!", 400); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $inbox; |
60
|
|
|
} catch (ClientException $e) { |
61
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get teamwork desk inboxes. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function inboxes(): array |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
/** @var Response $response */ |
75
|
|
|
$response = $this->client->get('inboxes.json'); |
76
|
|
|
/** @var Stream $body */ |
77
|
|
|
$body = $response->getBody(); |
78
|
|
|
|
79
|
|
|
return json_decode($body->getContents(), true); |
80
|
|
|
} catch (ClientException $e) { |
81
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the current client info. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function me(): array |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
/** @var Response $response */ |
95
|
|
|
$response = $this->client->get('me.json'); |
96
|
|
|
/** @var Stream $body */ |
97
|
|
|
$body = $response->getBody(); |
98
|
|
|
|
99
|
|
|
return json_decode($body->getContents(), true); |
100
|
|
|
} catch (ClientException $e) { |
101
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Update the customer, based on customerId. |
107
|
|
|
* |
108
|
|
|
* @param array $data = ['customerId', 'email', 'firstName', 'lastName', 'phone', 'mobile']; |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
112
|
|
|
*/ |
113
|
|
|
public function postCustomer($data): array |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
/** @var Response $response */ |
117
|
|
|
$response = $this->client->put('customers/' . $data['customerId'] . '.json', [ |
118
|
|
|
'json' => $data, |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
/** @var Stream $body */ |
122
|
|
|
$body = $response->getBody(); |
123
|
|
|
|
124
|
|
|
return json_decode($body->getContents(), true); |
125
|
|
|
} catch (ClientException $e) { |
126
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Upload file to teamwork desk. |
132
|
|
|
* |
133
|
|
|
* @param $userId |
134
|
|
|
* @param $file |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException |
138
|
|
|
* @throws \DigitalEquation\Teamwork\Exceptions\TeamworkUploadException |
139
|
|
|
*/ |
140
|
|
|
public function upload($userId, $file): array |
141
|
|
|
{ |
142
|
|
|
if (empty($file)) { |
143
|
|
|
throw new TeamworkUploadException('No file provided.', 400); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$filename = $file->getClientOriginalName(); |
147
|
|
|
$extension = $file->getClientOriginalExtension(); |
148
|
|
|
$path = sys_get_temp_dir(); |
149
|
|
|
$temp = $file->move($path, $filename); |
150
|
|
|
$stream = fopen($temp->getPathName(), 'r'); |
151
|
|
|
|
152
|
|
|
try { |
153
|
|
|
/** @var Response $response */ |
154
|
|
|
$response = $this->client->post('upload/attachment', [ |
155
|
|
|
'multipart' => [ |
156
|
|
|
[ |
157
|
|
|
'name' => 'file', |
158
|
|
|
'contents' => $stream, |
159
|
|
|
], [ |
160
|
|
|
'name' => 'userId', |
161
|
|
|
'contents' => $userId, |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
]); |
165
|
|
|
/** @var Stream $body */ |
166
|
|
|
$body = $response->getBody(); |
167
|
|
|
$body = json_decode($body->getContents(), true); |
168
|
|
|
|
169
|
|
|
if (! empty($stream)) { |
170
|
|
|
File::delete($temp->getPathName()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return [ |
174
|
|
|
'id' => $body['attachment']['id'], |
175
|
|
|
'url' => $body['attachment']['downloadURL'], |
176
|
|
|
'extension' => $extension, |
177
|
|
|
'name' => $body['attachment']['filename'], |
178
|
|
|
'size' => $body['attachment']['size'], |
179
|
|
|
]; |
180
|
|
|
} catch (ClientException $e) { |
181
|
|
|
throw new TeamworkHttpException($e->getMessage(), 400); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
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.