1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace AtlassianConnectBundle\Service; |
4
|
|
|
|
5
|
|
|
use AtlassianConnectBundle\Entity\TenantInterface; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Handler\CurlHandler; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AtlassianRestClient |
14
|
|
|
*/ |
15
|
|
|
class AtlassianRestClient |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var TenantInterface |
19
|
|
|
*/ |
20
|
|
|
private $tenant; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
private $user; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param TenantInterface|null $tenant |
34
|
|
|
* @param TokenStorageInterface|null $tokenStorage |
35
|
|
|
*/ |
36
|
|
|
public function __construct(?TenantInterface $tenant, ?TokenStorageInterface $tokenStorage = null) |
37
|
|
|
{ |
38
|
|
|
$this->setTenant($tenant, $tokenStorage); |
39
|
|
|
$this->client = $this->createClient(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param UploadedFile $file |
44
|
|
|
* @param string $restUrl |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function sendFile(UploadedFile $file, string $restUrl): string |
49
|
|
|
{ |
50
|
|
|
$options['headers']['X-Atlassian-Token'] = 'nocheck'; |
|
|
|
|
51
|
|
|
$savedFile = $file->move('/tmp/', $file->getClientOriginalName()); |
52
|
|
|
|
53
|
|
|
$options['body'] = [ |
54
|
|
|
'file' => \fopen($savedFile->getRealPath(), 'rb'), |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
\unlink($savedFile->getRealPath()); |
58
|
|
|
|
59
|
|
|
return $this->client->post($this->buildURL($restUrl), $options)->getBody()->getContents(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $restUrl |
64
|
|
|
* @param mixed[] $json |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function put(string $restUrl, array $json): string |
69
|
|
|
{ |
70
|
|
|
$options['headers']['Content-Type'] = 'application/json'; |
|
|
|
|
71
|
|
|
$options['json'] = $json; |
72
|
|
|
|
73
|
|
|
return $this->client->put($this->buildURL($restUrl), $options)->getBody()->getContents(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $restUrl |
78
|
|
|
* @param mixed[] $json |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function post(string $restUrl, array $json): string |
83
|
|
|
{ |
84
|
|
|
$options['headers']['Content-Type'] = 'application/json'; |
|
|
|
|
85
|
|
|
$options['json'] = $json; |
86
|
|
|
|
87
|
|
|
return $this->client->post($this->buildURL($restUrl), $options)->getBody()->getContents(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $restUrl |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function get(string $restUrl): string |
96
|
|
|
{ |
97
|
|
|
return $this->client->get($this->buildURL($restUrl))->getBody()->getContents(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $restUrl |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function delete(string $restUrl): string |
106
|
|
|
{ |
107
|
|
|
return $this->client->delete($this->buildURL($restUrl))->getBody()->getContents(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string|null $user |
112
|
|
|
* |
113
|
|
|
* @return AtlassianRestClient |
114
|
|
|
*/ |
115
|
|
|
public function setUser(?string $user): AtlassianRestClient |
116
|
|
|
{ |
117
|
|
|
$this->user = $user; |
118
|
|
|
$this->createClient(); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $restUrl |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
private function buildURL(string $restUrl): string |
129
|
|
|
{ |
130
|
|
|
// Jira return absolute self links, so its more easy to work with get with absolute urls in such cases |
131
|
|
|
if ((\mb_strpos($restUrl, 'http://') !== 0) && (\mb_strpos($restUrl, 'https://') !== 0)) { |
132
|
|
|
return $this->tenant->getBaseUrl().$restUrl; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $restUrl; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Create a HTTP client |
140
|
|
|
* |
141
|
|
|
* @return Client |
142
|
|
|
*/ |
143
|
|
|
private function createClient(): Client |
144
|
|
|
{ |
145
|
|
|
$stack = new HandlerStack(); |
146
|
|
|
$stack->setHandler(new CurlHandler()); |
147
|
|
|
$stack->push(GuzzleJWTMiddleware::authTokenMiddleware( |
148
|
|
|
$this->tenant->getAddonKey(), |
149
|
|
|
$this->tenant->getSharedSecret(), |
150
|
|
|
$this->user |
151
|
|
|
)); |
152
|
|
|
|
153
|
|
|
return new Client(['handler' => $stack]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param TenantInterface|null $tenant |
158
|
|
|
* @param null|TokenStorageInterface $tokenStorage |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
private function setTenant(?TenantInterface $tenant, ?TokenStorageInterface $tokenStorage): void |
163
|
|
|
{ |
164
|
|
|
if ($tenant !== null) { |
165
|
|
|
$this->tenant = $tenant; |
166
|
|
|
} elseif ($tokenStorage !== null) { |
167
|
|
|
$token = $tokenStorage->getToken(); |
168
|
|
|
|
169
|
|
|
if ($token !== null) { |
170
|
|
|
$tenant = $token->getUser(); |
171
|
|
|
|
172
|
|
|
if ($tenant instanceof TenantInterface) { |
173
|
|
|
$this->tenant = $tenant; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} else { |
177
|
|
|
throw new \RuntimeException('Can\'t get tenant'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|