Passed
Pull Request — master (#71)
by Matthieu
05:17 queued 01:13
created

AuthenticatedAtlassianClient::withOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Service;
6
7
use AtlassianConnectBundle\Entity\TenantInterface;
8
use Symfony\Contracts\HttpClient\HttpClientInterface;
9
use Symfony\Contracts\HttpClient\ResponseInterface;
10
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
11
12
final class AuthenticatedAtlassianClient implements HttpClientInterface
13
{
14
    private HttpClientInterface $client;
15
16
    public function __construct(HttpClientInterface $client)
17
    {
18
        $this->client = $client;
19
    }
20
21
    public function request(string $method, string $url, array $options = []): ResponseInterface
22
    {
23
        /** @var TenantInterface $tenant */
24
        $tenant = $options['tenant'];
25
        $userId = $options['user_id'];
26
        unset($options['tenant']);
27
        unset($options['user_id']);
28
29
        if (!$userId) {
30
            $options['headers']['Authorization'] = 'JWT '.JWTGenerator::generate($url, $method, $tenant->getAddonKey(), $tenant->getSharedSecret());
0 ignored issues
show
Bug introduced by
It seems like $tenant->getSharedSecret() can also be of type null; however, parameter $secret of AtlassianConnectBundle\S...WTGenerator::generate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            $options['headers']['Authorization'] = 'JWT '.JWTGenerator::generate($url, $method, $tenant->getAddonKey(), /** @scrutinizer ignore-type */ $tenant->getSharedSecret());
Loading history...
Bug introduced by
It seems like $tenant->getAddonKey() can also be of type null; however, parameter $issuer of AtlassianConnectBundle\S...WTGenerator::generate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            $options['headers']['Authorization'] = 'JWT '.JWTGenerator::generate($url, $method, /** @scrutinizer ignore-type */ $tenant->getAddonKey(), $tenant->getSharedSecret());
Loading history...
31
32
            return $this->client->request($method, $url, $options);
33
        }
34
35
        if (!$tenant->getOauthClientId()) {
36
            throw new \RuntimeException('Tenant is not set up as oath application. Install the app with "ACT_AS_USER" scope.');
37
        }
38
39
        $result = $this->client->request('POST', 'https://oauth-2-authorization-server.services.atlassian.com/oauth2/token', [
40
            'body' => [
41
                'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
42
                'assertion' => JWTGenerator::generateAssertion($tenant->getSharedSecret(), $tenant->getOauthClientId(), $tenant->getBaseUrl(), $userId),
0 ignored issues
show
Bug introduced by
It seems like $tenant->getSharedSecret() can also be of type null; however, parameter $secret of AtlassianConnectBundle\S...or::generateAssertion() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                'assertion' => JWTGenerator::generateAssertion(/** @scrutinizer ignore-type */ $tenant->getSharedSecret(), $tenant->getOauthClientId(), $tenant->getBaseUrl(), $userId),
Loading history...
Bug introduced by
It seems like $tenant->getBaseUrl() can also be of type null; however, parameter $baseUrl of AtlassianConnectBundle\S...or::generateAssertion() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                'assertion' => JWTGenerator::generateAssertion($tenant->getSharedSecret(), $tenant->getOauthClientId(), /** @scrutinizer ignore-type */ $tenant->getBaseUrl(), $userId),
Loading history...
43
            ],
44
        ]);
45
46
        $options['headers']['Authorization'] = 'Bearer '.$result->toArray()['access_token'];
47
48
        return $this->client->request($method, $url, $options);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function stream($responses, float $timeout = null): ResponseStreamInterface
55
    {
56
        return $this->client->stream($responses, $timeout);
57
    }
58
59
    public function withOptions(array $options): self
60
    {
61
        $clone = clone $this;
62
        $clone->client = $this->client->withOptions($options);
63
64
        return $clone;
65
    }
66
}
67