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.
Passed
Push — master ( d1111f...994164 )
by Sean
01:06
created

Provider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 29
dl 0
loc 101
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getProvider() 0 9 1
A getCompanyId() 0 3 1
A getAccessToken() 0 15 5
A setUrl() 0 3 1
A getUrl() 0 3 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
11
/**
12
 * Class OAuthSigner
13
 *
14
 * @package Kobas\APIClient\Auth
15
 */
16
class Provider
17
{
18
    /**
19
     * @var AccessToken
20
     */
21
    protected static $token;
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
    public function __construct($companyId, $clientId, $clientSecret, $scopes)
52
    {
53
        $this->companyId = (int)$companyId;
54
        $this->clientId = (string)$clientId;
55
        $this->clientSecret = (string)$clientSecret;
56
        $this->scopes = (string)$scopes;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getUrl()
63
    {
64
        return $this->url;
65
    }
66
67
    /**
68
     * @param $url
69
     */
70
    public function setUrl($url)
71
    {
72
        $this->url = $url;
73
    }
74
75
    /**
76
     * @return int
77
     */
78
    public function getCompanyId()
79
    {
80
        return $this->companyId;
81
    }
82
83
    /**
84
     * @return string
85
     * @throws AuthenticationException
86
     * @throws CurlException
87
     */
88
    public function getAccessToken()
89
    {
90
        $provider = $this->getProvider();
91
92
        if (!self::$token instanceof AccessToken || self::$token->hasExpired()) {
0 ignored issues
show
introduced by
self::token is always a sub-type of League\OAuth2\Client\Token\AccessToken. If self::token can have other possible types, add them to src/Kobas/APIClient/Auth/Provider.php:19.
Loading history...
93
            try {
94
                self::$token = $provider->getAccessToken('client_credentials', ['scope' => $this->scopes]);
95
            } catch (IdentityProviderException $e) {
96
                throw new AuthenticationException($e->getMessage(), $e->getCode());
97
            } catch (ConnectException $e) {
98
                throw new CurlException($e->getMessage(), $e->getCode());
99
            }
100
        }
101
102
        return self::$token->getToken();
103
    }
104
105
    /**
106
     * @return \Kobas\OAuth2\Client\Provider\Kobas
107
     */
108
    public function getProvider()
109
    {
110
        $provider = new \Kobas\OAuth2\Client\Provider\Kobas([
111
            'clientId' => $this->clientId,
112
            'clientSecret' => $this->clientSecret,
113
            'companyId' => $this->companyId,
114
            'url' => $this->url
115
        ]);
116
        return $provider;
117
    }
118
}
119