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.

OAuth::getAccessToken()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 3
nop 1
crap 4
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace BristolSU\Service\Typeform\Connectors;
4
5
use BristolSU\Service\Typeform\Fields\TypeformAuthCode;
6
use BristolSU\Service\Typeform\Models\TypeformAuthCode as AuthModel;
7
use BristolSU\Support\Connection\Contracts\Connector;
8
use Carbon\Carbon;
9
use FormSchema\Generator\Field;
10
use FormSchema\Schema\Form;
11
use GuzzleHttp\Exception\GuzzleException;
12
13
class OAuth extends Connector
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class OAuth
Loading history...
14
{
15
16
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $method should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $uri should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $options should have a doc-comment as per coding-style.
Loading history...
17
     * @inheritDoc
18
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
19 6
    public function request($method, $uri, array $options = [])
20
    {
21 6
        $options['base_uri'] = config('typeform_service.base_uri');
22 6
        $headers = ((isset($options['headers']) && is_array($options['headers']))?$options['headers']:[]);
23 6
        $headers['Authorization'] = 'Bearer ' . $this->getAccessToken();
24 5
        $options['headers'] = $headers;
25 5
        return $this->client->request($method, $uri, $options);
26
    }
27
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
     * @inheritDoc
30
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
31 2
    public function test(): bool
32
    {
33
        try {
34 2
            $this->request('get', '/me');
35 1
            return true;
36 1
        } catch (GuzzleException $e) {
37 1
            return false;
38
        }
39
    }
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
     * @inheritDoc
43
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
44 1
    static public function settingsSchema(): Form
45
    {
46 1
        return \FormSchema\Generator\Form::make()->withField(
47 1
            Field::make(TypeformAuthCode::class, 'auth_code_id')->label('Log Into Typeform')->required(true)
48 1
        )->getSchema();
49
    }
50
51 6
    private function getAccessToken($refreshable = true): string
0 ignored issues
show
Coding Style introduced by
Private method name "OAuth::getAccessToken" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function getAccessToken()
Loading history...
52
    {
53 6
        $authCode = AuthModel::findOrFail($this->getSetting('auth_code_id'));
54 6
        if($authCode->isValid()) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
55 5
            return $authCode->auth_code;
56
        }
57 2
        if($refreshable && $this->refreshAccessToken($authCode)) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
58 1
            return $this->getAccessToken(false);
59
        } else {
60
            // TODO Throw special error to send email to people
61 1
            throw new \Exception('Access token could not be refreshed');
62
        }
63
    }
64
65 2
    private function refreshAccessToken(AuthModel $authCode)
0 ignored issues
show
Coding Style introduced by
Private method name "OAuth::refreshAccessToken" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function refreshAccessToken()
Loading history...
66
    {
67
        try {
68 2
            $response = $this->client->request('post', config('typeform_service.urlAccessToken'), [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
69
                'form_params' => [
70 2
                    'grant_type' => 'refresh_token',
71 2
                    'refresh_token' => $authCode->refresh_token,
72 2
                    'client_id' => config('typeform_service.client_id'),
73 2
                    'client_secret' => config('typeform_service.client_secret'),
74 2
                    'scope' => 'offline accounts:read responses:read webhooks:read webhooks:write forms:read'
75
                ],
76
            ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
77 1
        } catch (GuzzleException $e) {
78 1
            return false;
79
        }
80 1
        $token = json_decode($response->getBody()->getContents(), true);
81 1
        $authCode->auth_code = $token['access_token'];
82 1
        $authCode->refresh_token = $token['refresh_token'];
83 1
        $authCode->expires_at = Carbon::now()->addSeconds($token['expires_in']);
84 1
        return $authCode->save();
85
    }
86
}
87