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.

Provider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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