GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#5)
by Sean
03:57
created

Provider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 12
eloc 34
dl 0
loc 108
ccs 32
cts 38
cp 0.8421
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrl() 0 3 1
A __construct() 0 6 1
A getUrl() 0 3 1
A getCompanyId() 0 3 1
A getProvider() 0 9 1
B getAccessToken() 0 22 7
1
<?php
2
3
namespace Kobas\APIClient\Auth;
4
5
use GuzzleHttp\Exception\ConnectException;
6
use Kobas\APIClient\Exception\AuthenticationException;
7
use Kobas\APIClient\Exception\CurlException;
8
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
9
use League\OAuth2\Client\Token\AccessToken;
10
11
/**
12
 * Class OAuthSigner
13
 *
14
 * @package Kobas\APIClient\Auth
15
 */
16
class Provider
17
{
18
    /**
19
     * @var AccessToken[]
20
     */
21
    protected static $tokens;
22
    /**
23
     * @var int
24
     */
25
    protected $companyId;
26
    /**
27
     * @var string
28
     */
29
    protected $clientId;
30
    /**
31
     * @var string
32
     */
33
    protected $clientSecret;
34
    /**
35
     * @var string
36
     */
37
    protected $scopes;
38
    /**
39
     * @var string
40
     */
41
    protected $url = 'https://oauth.kobas.co.uk';
42
43
    /**
44
     * OAuthSigner constructor.
45
     *
46
     * @param int $companyId
47
     * @param string $clientId
48
     * @param string $clientSecret
49
     * @param string $scopes
50
     */
51 15
    public function __construct($companyId, $clientId, $clientSecret, $scopes)
52
    {
53 15
        $this->companyId = (int)$companyId;
54 15
        $this->clientId = (string)$clientId;
55 15
        $this->clientSecret = (string)$clientSecret;
56 15
        $this->scopes = (string)$scopes;
57 15
    }
58
59
    /**
60
     * @return string
61
     */
62 3
    public function getUrl()
63
    {
64 3
        return $this->url;
65
    }
66
67
    /**
68
     * @param $url
69
     */
70 3
    public function setUrl($url)
71
    {
72 3
        $this->url = $url;
73 3
    }
74
75
    /**
76
     * @return int
77
     */
78 3
    public function getCompanyId()
79
    {
80 3
        return $this->companyId;
81
    }
82
83
    /**
84
     * @return string
85
     * @throws AuthenticationException
86
     * @throws CurlException
87
     */
88 3
    public function getAccessToken()
89
    {
90 3
        $provider = $this->getProvider();
91
92 3
        if (!is_array(self::$tokens) ||
0 ignored issues
show
introduced by
The condition is_array(self::tokens) is always true.
Loading history...
93
            !array_key_exists($provider->companyId, self::$tokens) ||
94
            !self::$tokens[$provider->companyId] instanceof AccessToken ||
95 1
            self::$tokens[$provider->companyId]->hasExpired()
96 2
        ) {
97
            try {
98 3
                self::$tokens[$provider->companyId] = $provider->getAccessToken(
99 3
                    'client_credentials',
100 3
                    ['scope' => $this->scopes]
101 2
                );
102 3
            } catch (IdentityProviderException $e) {
103 3
                throw new AuthenticationException($e->getMessage(), $e->getCode());
104
            } catch (ConnectException $e) {
105
                throw new CurlException($e->getMessage(), $e->getCode());
106
            }
107
        }
108
109
        return self::$tokens[$provider->companyId]->getToken();
110
    }
111
112
    /**
113
     * @return \Kobas\OAuth2\Client\Provider\Kobas
114
     */
115 3
    public function getProvider()
116
    {
117 3
        $provider = new \Kobas\OAuth2\Client\Provider\Kobas([
118 3
            'clientId' => $this->clientId,
119 3
            'clientSecret' => $this->clientSecret,
120 3
            'companyId' => $this->companyId,
121 3
            'url' => $this->url
122 2
        ]);
123 3
        return $provider;
124 2
    }
125
}
126