Passed
Push — master ( ea520a...3ac64b )
by Bukashk0zzz
03:30 queued 13s
created

AtlassianRestClient::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
     * @param TenantInterface|null       $tenant
29
     * @param TokenStorageInterface|null $tokenStorage
30
     */
31
    public function __construct(?TenantInterface $tenant, ?TokenStorageInterface $tokenStorage = null)
32
    {
33
        $this->setTenant($tenant, $tokenStorage);
34
        $this->client = $this->createClient();
35
    }
36
37
    /**
38
     * @param UploadedFile $file
39
     * @param string       $restUrl
40
     *
41
     * @return string
42
     */
43
    public function sendFile(UploadedFile $file, string $restUrl): string
44
    {
45
        $options['headers']['X-Atlassian-Token'] = 'nocheck';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
46
        $savedFile = $file->move('/tmp/', $file->getClientOriginalName());
47
48
        $options['body'] = [
49
            'file' => \fopen($savedFile->getRealPath(), 'rb'),
50
        ];
51
52
        \unlink($savedFile->getRealPath());
53
54
        return $this->client->post($this->buildURL($restUrl), $options)->getBody()->getContents();
55
    }
56
57
    /**
58
     * @param string  $restUrl
59
     * @param mixed[] $json
60
     *
61
     * @return string
62
     */
63
    public function put(string $restUrl, array $json): string
64
    {
65
        $options['headers']['Content-Type'] = 'application/json';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
66
        $options['json'] = $json;
67
68
        return $this->client->put($this->buildURL($restUrl), $options)->getBody()->getContents();
69
    }
70
71
    /**
72
     * @param string  $restUrl
73
     * @param mixed[] $json
74
     *
75
     * @return string
76
     */
77
    public function post(string $restUrl, array $json): string
78
    {
79
        $options['headers']['Content-Type'] = 'application/json';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
80
        $options['json'] = $json;
81
82
        return $this->client->post($this->buildURL($restUrl), $options)->getBody()->getContents();
83
    }
84
85
    /**
86
     * @param string $restUrl
87
     *
88
     * @return string
89
     */
90
    public function get(string $restUrl): string
91
    {
92
        return $this->client->get($this->buildURL($restUrl))->getBody()->getContents();
93
    }
94
95
    /**
96
     * @param string $restUrl
97
     *
98
     * @return string
99
     */
100
    public function delete(string $restUrl): string
101
    {
102
        return $this->client->delete($this->buildURL($restUrl))->getBody()->getContents();
103
    }
104
105
    /**
106
     * @param string $restUrl
107
     *
108
     * @return string
109
     */
110
    private function buildURL(string $restUrl): string
111
    {
112
        // Jira return absolute self links, so its more easy to work with get with absolute urls in such cases
113
        if ((\mb_strpos($restUrl, 'http://') !== 0) && (\mb_strpos($restUrl, 'https://') !== 0)) {
114
            return $this->tenant->getBaseUrl().$restUrl;
115
        }
116
117
        return $restUrl;
118
    }
119
120
    /**
121
     * Create a HTTP client
122
     *
123
     * @return Client
124
     */
125
    private function createClient(): Client
126
    {
127
        $stack = new HandlerStack();
128
        $stack->setHandler(new CurlHandler());
129
        $stack->push(GuzzleJWTMiddleware::authTokenMiddleware(
130
            $this->tenant->getAddonKey(),
131
            $this->tenant->getSharedSecret()
132
        ));
133
134
        return new Client(['handler' => $stack]);
135
    }
136
137
    /**
138
     * @param TenantInterface|null       $tenant
139
     * @param null|TokenStorageInterface $tokenStorage
140
     *
141
     * @return void
142
     */
143
    private function setTenant(?TenantInterface $tenant, ?TokenStorageInterface $tokenStorage): void
144
    {
145
        if ($tenant !== null) {
146
            $this->tenant = $tenant;
147
        } elseif ($tokenStorage !== null) {
148
            $token = $tokenStorage->getToken();
149
150
            if ($token !== null) {
151
                $tenant = $token->getUser();
152
153
                if ($tenant instanceof TenantInterface) {
154
                    $this->tenant = $tenant;
155
                }
156
            }
157
        } else {
158
            throw new \RuntimeException('Can\'t get tenant');
159
        }
160
    }
161
}
162