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.

MattermostAuthentication   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 91
ccs 0
cts 46
cp 0
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A authenticate() 0 9 2
A getToken() 0 32 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pnz\MattermostClient\Authentication;
6
7
use Http\Message\Authentication;
8
use Pnz\JsonException\Json;
9
use Pnz\MattermostClient\Exception\ApiException;
10
use Pnz\MattermostClient\Exception\Domain\LoginFailedException;
11
use Pnz\MattermostClient\Model\Error;
12
use Psr\Http\Client\ClientInterface;
13
use Psr\Http\Message\RequestFactoryInterface;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\StreamFactoryInterface;
16
17
class MattermostAuthentication implements Authentication
18
{
19
    private const AUTHORIZATION_URL = '/users/login';
20
21
    /**
22
     * @var ClientInterface
23
     */
24
    private $client;
25
26
    /**
27
     * @var string
28
     */
29
    private $loginId;
30
31
    /**
32
     * @var string string
33
     */
34
    private $password;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $token;
40
41
    /**
42
     * @var RequestFactoryInterface
43
     */
44
    private $requestFactory;
45
46
    /**
47
     * @var StreamFactoryInterface
48
     */
49
    private $streamFactory;
50
51
    public function __construct(
52
        string $loginId,
53
        string $password,
54
        ClientInterface $client,
55
        RequestFactoryInterface $requestFactory,
56
        StreamFactoryInterface $streamFactory
57
    ) {
58
        $this->loginId = $loginId;
59
        $this->password = $password;
60
        $this->requestFactory = $requestFactory;
61
        $this->streamFactory = $streamFactory;
62
        $this->client = $client;
63
    }
64
65
    public function authenticate(RequestInterface $request): RequestInterface
66
    {
67
        if (!$this->token) {
68
            $this->getToken();
69
        }
70
71
        $header = sprintf('Bearer %s', $this->token);
72
73
        return $request->withHeader('Authorization', $header);
74
    }
75
76
    private function getToken(): void
77
    {
78
        $credentials = [
79
            'login_id' => $this->loginId,
80
            'password' => $this->password,
81
        ];
82
83
        $request = $this->requestFactory->createRequest('POST', self::AUTHORIZATION_URL)
84
            ->withBody($this->streamFactory->createStream(
85
                Json::encode($credentials, JSON_FORCE_OBJECT)
86
            ))
87
        ;
88
89
        $response = $this->client->sendRequest($request);
90
91
        // We got a non-json response, can not continue!
92
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
93
            throw new ApiException($response);
94
        }
95
96
        switch ($response->getStatusCode()) {
97
            case 200:
98
                $this->token = $response->getHeaderLine('Token');
99
100
                return;
101
            case 401:
102
                $contents = Json::decode((string) $response->getBody(), true);
103
                $error = Error::createFromArray($contents);
104
                throw new LoginFailedException($response, $error);
105
        }
106
107
        throw new ApiException($response);
108
    }
109
}
110