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
Push — master ( b21c26...dc2a23 )
by
unknown
21s queued 17s
created

LPTrackerBase::__construct()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
cc 7
nc 18
nop 1
1
<?php
2
3
namespace LPTracker;
4
5
use LPTracker\authentication\AccessToken;
6
use LPTracker\exceptions\LPTrackerSDKException;
7
8
/**
9
 * Class LPTracker
10
 * @package LPTracker
11
 */
12
abstract class LPTrackerBase
13
{
14
15
    /**
16
     * Версия апи
17
     */
18
    const VERSION = '1.0';
19
20
    /**
21
     * Адрес апи по умолчанию
22
     */
23
    const DEFAULT_ADDRESS = 'http://direct.lptracker.ru';
24
25
    /**
26
     * Название сервиса по умолчанию
27
     */
28
    const DEFAULT_SERVICE_NAME = 'api (sdk)';
29
30
    /**
31
     * Ключ переменной окружения логина/email главного аккаунта
32
     */
33
    const LOGIN_ENV_NAME = 'LPTRACKER_LOGIN';
34
35
    /**
36
     * Ключ переменной окружения пароля главного аккаунта
37
     */
38
    const PASSWORD_ENV_NAME = 'LPTRACKER_PASSWORD';
39
40
    /**
41
     * Ключ переменной окружения имени сервиса
42
     */
43
    const SERVICE_NAME_ENV_NAME = 'LPTRACKER_SERVICE_NAME';
44
45
    /**
46
     * Ключ переменной окружения access token от системы если уже есть
47
     */
48
    const TOKEN_ENV_NAME = 'LPTRACKER_TOKEN';
49
50
    /**
51
     * Ключ переменной окружения адреса апи
52
     */
53
    const ADDRESS_ENV_NAME = 'LPTRACKER_ADDRESS';
54
55
    /**
56
     * @var AccessToken
57
     */
58
    protected $token;
59
60
    /**
61
     * @var string
62
     */
63
    protected $address = '';
64
65
    /**
66
     * @var array
67
     */
68
    protected $config = [];
69
70
71
    /**
72
     * LPTracker constructor.
73
     *
74
     * @param array $config
75
     *
76
     * @throws LPTrackerSDKException
77
     */
78
    public function __construct(array $config = [])
79
    {
80
        $config = array_merge([
81
            'login'    => getenv(static::LOGIN_ENV_NAME),
82
            'password' => getenv(static::PASSWORD_ENV_NAME),
83
            'service'  => getenv(static::SERVICE_NAME_ENV_NAME),
84
            'token'    => getenv(static::TOKEN_ENV_NAME),
85
            'address'  => getenv(static::ADDRESS_ENV_NAME),
86
        ], $config);
87
88
        if (empty($config['token'])) {
89
            if (empty($config['login'])) {
90
                throw new LPTrackerSDKException('Required "login" key not supplied in config and could not find fallback environment variable "'.static::LOGIN_ENV_NAME.'"');
91
            }
92
            if (empty($config['password'])) {
93
                throw new LPTrackerSDKException('Required "password" key not supplied in config and could not find fallback environment variable "'.static::PASSWORD_ENV_NAME.'"');
94
            }
95
        }
96
97
        if (empty($config['address'])) {
98
            $config['address'] = self::DEFAULT_ADDRESS;
99
        }
100
        if (empty($config['service'])) {
101
            $config['service'] = self::DEFAULT_SERVICE_NAME;
102
        }
103
104
        $this->config = $config;
105
106
        $this->address = $config['address'];
107
        if (empty($config['token'])) {
108
            $this->token = $this->login($config['login'], $config['password'], $config['service']);
109
        } else {
110
            $this->token = new AccessToken($config['token']);
111
        }
112
    }
113
114
115
    /**
116
     * @return string
117
     */
118
    public function getToken()
119
    {
120
        return $this->token->getValue();
121
    }
122
123
124
    /**
125
     * @param $token
126
     *
127
     * @return $this
128
     * @throws LPTrackerSDKException
129
     */
130
    public function setToken($token)
131
    {
132
        if ($token instanceof AccessToken) {
133
            $this->token = $token;
134
        } elseif (is_string($token)) {
135
            $this->token = new AccessToken($token);
136
        } else {
137
            throw new LPTrackerSDKException('Invalid token');
138
        }
139
140
        return $this;
141
    }
142
143
144
    /**
145
     * @param        $login
146
     * @param        $password
147
     * @param string $serviceName
148
     *
149
     * @return mixed
150
     */
151
    abstract public function login($login, $password, $serviceName = '');
152
}