1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AtlassianConnectBundle\Service; |
||
6 | |||
7 | use AtlassianConnectBundle\Entity\TenantInterface; |
||
8 | use Symfony\Component\HttpFoundation\File\File; |
||
9 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
10 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
||
11 | |||
12 | final class AtlassianRestClient implements AtlassianRestClientInterface |
||
13 | { |
||
14 | private HttpClientInterface $client; |
||
15 | |||
16 | private TokenStorageInterface $tokenStorage; |
||
17 | |||
18 | private ?TenantInterface $tenant; |
||
19 | |||
20 | private ?string $user; |
||
21 | |||
22 | public function __construct(HttpClientInterface $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 | return $this->doRequest('POST', $restUrl, [ |
||
53 | 'headers' => ['Content-Type' => 'application/json'], |
||
54 | 'json' => $json, |
||
55 | ]); |
||
56 | } |
||
57 | |||
58 | public function put(string $restUrl, array $json): string |
||
59 | { |
||
60 | return $this->doRequest('PUT', $restUrl, [ |
||
61 | 'headers' => ['Content-Type' => 'application/json'], |
||
62 | 'json' => $json, |
||
63 | ]); |
||
64 | } |
||
65 | |||
66 | public function delete(string $restUrl): string |
||
67 | { |
||
68 | return $this->doRequest('DELETE', $restUrl, []); |
||
69 | } |
||
70 | |||
71 | public function sendFile(File $file, string $restUrl): string |
||
72 | { |
||
73 | $options = []; |
||
74 | |||
75 | $options['headers']['X-Atlassian-Token'] = 'nocheck'; |
||
76 | $savedFile = $file->move('/tmp/', $file->getFilename()); |
||
77 | |||
78 | $options['body'] = [ |
||
79 | 'file' => fopen($savedFile->getRealPath(), 'r'), |
||
80 | ]; |
||
81 | |||
82 | unlink($savedFile->getRealPath()); |
||
83 | |||
84 | return $this->doRequest('POST', $restUrl, $options); |
||
85 | } |
||
86 | |||
87 | public function doRequest(string $method, string $restUrl, array $options): string |
||
88 | { |
||
89 | $options['tenant'] = $this->getTenant(); |
||
90 | $options['user_id'] = $this->user; |
||
91 | |||
92 | return $this->client->request($method, $this->buildURL($restUrl), $options)->getContent(); |
||
93 | } |
||
94 | |||
95 | private function getTenant(): TenantInterface |
||
96 | { |
||
97 | if ($this->tenant) { |
||
98 | return $this->tenant; |
||
99 | } |
||
100 | |||
101 | $token = $this->tokenStorage->getToken(); |
||
102 | |||
103 | if (!$token || !$user = $token->getUser()) { |
||
104 | throw new \RuntimeException('Could not get tenant from token'); |
||
105 | } |
||
106 | |||
107 | if (!$user instanceof TenantInterface) { |
||
108 | throw new \RuntimeException('Current user is not a Tenant'); |
||
109 | } |
||
110 | |||
111 | return $this->tenant = $user; |
||
112 | } |
||
113 | |||
114 | private function buildURL(string $restUrl): string |
||
115 | { |
||
116 | // Jira return absolute self links, so its more easy to work with get with absolute urls in such cases |
||
117 | if ((0 !== mb_strpos($restUrl, 'http://')) && (0 !== mb_strpos($restUrl, 'https://'))) { |
||
118 | return $this->tenant->getBaseUrl().$restUrl; |
||
0 ignored issues
–
show
|
|||
119 | } |
||
120 | |||
121 | return $restUrl; |
||
122 | } |
||
123 | } |
||
124 |
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.