1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AtlassianConnectBundle\Service; |
6
|
|
|
|
7
|
|
|
use AtlassianConnectBundle\Entity\TenantInterface; |
8
|
|
|
use GuzzleHttp\ClientInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
11
|
|
|
|
12
|
|
|
final class AtlassianRestClient implements AtlassianRestClientInterface |
13
|
|
|
{ |
14
|
|
|
private ClientInterface $client; |
15
|
|
|
|
16
|
|
|
private TokenStorageInterface $tokenStorage; |
17
|
|
|
|
18
|
|
|
private ?TenantInterface $tenant; |
19
|
|
|
|
20
|
|
|
private ?string $user; |
21
|
|
|
|
22
|
|
|
public function __construct(ClientInterface $client, TokenStorageInterface $tokenStorage) |
23
|
|
|
{ |
24
|
|
|
$this->client = $client; |
25
|
|
|
$this->tokenStorage = $tokenStorage; |
26
|
|
|
|
27
|
|
|
$this->tenant = null; |
28
|
|
|
$this->user = null; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setTenant(TenantInterface $tenant): self |
32
|
|
|
{ |
33
|
|
|
$this->tenant = $tenant; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function actAsUser(string $userId): self |
39
|
|
|
{ |
40
|
|
|
$this->user = $userId; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function get(string $restUrl): string |
46
|
|
|
{ |
47
|
|
|
return $this->doRequest('GET', $restUrl, []); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function post(string $restUrl, array $json): string |
51
|
|
|
{ |
52
|
|
|
$options = []; |
53
|
|
|
|
54
|
|
|
$options['headers']['Content-Type'] = 'application/json'; |
55
|
|
|
$options['json'] = $json; |
56
|
|
|
|
57
|
|
|
return $this->doRequest('POST', $restUrl, $options); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function put(string $restUrl, array $json): string |
61
|
|
|
{ |
62
|
|
|
$options = []; |
63
|
|
|
|
64
|
|
|
$options['headers']['Content-Type'] = 'application/json'; |
65
|
|
|
$options['json'] = $json; |
66
|
|
|
|
67
|
|
|
return $this->doRequest('PUT', $restUrl, $options); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function delete(string $restUrl): string |
71
|
|
|
{ |
72
|
|
|
return $this->doRequest('DELETE', $restUrl, []); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function sendFile(File $file, string $restUrl): string |
76
|
|
|
{ |
77
|
|
|
$options = []; |
78
|
|
|
|
79
|
|
|
$options['headers']['X-Atlassian-Token'] = 'nocheck'; |
80
|
|
|
$savedFile = $file->move('/tmp/', $file->getFilename()); |
81
|
|
|
|
82
|
|
|
$options['body'] = [ |
83
|
|
|
'file' => fopen($savedFile->getRealPath(), 'r'), |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
unlink($savedFile->getRealPath()); |
87
|
|
|
|
88
|
|
|
return $this->doRequest('POST', $restUrl, $options); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function doRequest(string $method, string $restUrl, array $options): string |
92
|
|
|
{ |
93
|
|
|
$options['tenant'] = $this->getTenant(); |
94
|
|
|
$options['user_id'] = $this->user; |
95
|
|
|
|
96
|
|
|
return $this->client->request($method, $this->buildURL($restUrl), $options)->getBody()->getContents(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getTenant(): TenantInterface |
100
|
|
|
{ |
101
|
|
|
if ($this->tenant) { |
102
|
|
|
return $this->tenant; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$token = $this->tokenStorage->getToken(); |
106
|
|
|
|
107
|
|
|
if (!$token || !$user = $token->getUser()) { |
108
|
|
|
throw new \RuntimeException('Could not get tenant from token'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!$user instanceof TenantInterface) { |
112
|
|
|
throw new \RuntimeException('Current user is not a Tenant'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->tenant = $user; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function buildURL(string $restUrl): string |
119
|
|
|
{ |
120
|
|
|
// Jira return absolute self links, so its more easy to work with get with absolute urls in such cases |
121
|
|
|
if ((0 !== mb_strpos($restUrl, 'http://')) && (0 !== mb_strpos($restUrl, 'https://'))) { |
122
|
|
|
return $this->tenant->getBaseUrl().$restUrl; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $restUrl; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.