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.

LPTrackerBase   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 134
rs 10
c 0
b 0
f 0

4 Methods

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