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 ( 844242...d5afcc )
by Ema
02:36
created

MattermostAuthentication   A

Complexity

Total Complexity 7

Size/Duplication

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