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 ( 636af9...b06a01 )
by Dragos
13:37
created

ApiKeyHelper::apiKeyIsValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace Speicher210\KontaktIO;
4
5
use GuzzleHttp\Client as GuzzleHttpClient;
6
use GuzzleHttp\Exception\ClientException;
7
use Speicher210\KontaktIO\Exception\ApiKeyExtractionInvalidCredentialsException;
8
9
/**
10
 * Helper functions around the API key.
11
 */
12
class ApiKeyHelper
13
{
14
    /**
15
     * Get the API key using a username and password combination.
16
     *
17
     * @param string $username The username.
18
     * @param string $password The password.
19
     * @return string
20
     */
21
    public static function getApiKey($username, $password)
22
    {
23
        $client = new GuzzleHttpClient(['cookies' => true, 'allow_redirects' => false]);
24
        $client->post(
25
            'https://panel.kontakt.io/signin',
26
            [
27
                'form_params' => [
28
                    'username' => $username,
29
                    'password' => $password,
30
                ],
31
            ]
32
        );
33
34
        $response = $client->get('https://panel.kontakt.io/api-key');
35
36
        if ($response->getStatusCode() !== 200) {
37
            throw new ApiKeyExtractionInvalidCredentialsException();
38
        }
39
40
        return (string)$response->getBody();
41
    }
42
43
    public static function apiKeyIsValid($apiKey)
44
    {
45
        $client = new Client($apiKey);
46
47
        try {
48
            $client->get('/manager/me');
49
50
            return true;
51
        } catch (ClientException $e) {
52
            if ($e->getResponse()->getStatusCode() === 401) {
53
                return false;
54
            }
55
56
            throw $e;
57
        }
58
    }
59
}
60