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.

Token::export()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Identity\v3\Models;
4
5
use OpenStack\Common\Resource\Alias;
6
use OpenStack\Common\Transport\Utils;
7
use Psr\Http\Message\ResponseInterface;
8
use OpenStack\Common\Resource\OperatorResource;
9
use OpenStack\Common\Resource\Creatable;
10
use OpenStack\Common\Resource\Retrievable;
11
12
/**
13
 * @property \OpenStack\Identity\v3\Api $api
14
 */
15
class Token extends OperatorResource implements Creatable, Retrievable, \OpenStack\Common\Auth\Token
16
{
17
    /** @var array */
18
    public $methods;
19
20
    /** @var Role[] */
21
    public $roles;
22
23
    /** @var \DateTimeImmutable */
24
    public $expires;
25
26
    /** @var Project */
27
    public $project;
28
29
    /** @var Catalog */
30
    public $catalog;
31
32
    /** @var mixed */
33
    public $extras;
34
35
    /** @var User */
36
    public $user;
37
38
    /** @var \DateTimeImmutable */
39
    public $issued;
40
41
    /** @var string */
42
    public $id;
43
44
    protected $resourceKey = 'token';
45
    protected $resourcesKey = 'tokens';
46
47
    protected $cachedToken;
48
49
    /**
50
     * @inheritdoc
51
     */
52
    protected function getAliases(): array
53 5
    {
54
        return parent::getAliases() + [
55 5
            'roles'      => new Alias('roles', Role::class, true),
56
            'expires_at' => new Alias('expires', \DateTimeImmutable::class),
57 5
            'project'    => new Alias('project', Project::class),
58
            'catalog'    => new Alias('catalog', Catalog::class),
59 5
            'user'       => new Alias('user', User::class),
60
            'issued_at'  => new Alias('issued', \DateTimeImmutable::class)
61
        ];
62
    }
63
64
    /**
65 1
     * {@inheritDoc}
66
     */
67 1
    public function populateFromResponse(ResponseInterface $response)
68
    {
69
        parent::populateFromResponse($response);
70
        $this->id = $response->getHeaderLine('X-Subject-Token');
71
        return $this;
72
    }
73 1
74
    /**
75 1
     * @return string
76
     */
77
    public function getId(): string
78
    {
79
        return $this->id;
80
    }
81 1
82
    /**
83 1
     * @return bool TRUE if the token has expired (and is invalid); FALSE otherwise.
84
     */
85 1
    public function hasExpired(): bool
86 1
    {
87
        return $this->expires <= new \DateTimeImmutable('now', $this->expires->getTimezone());
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 10
    public function retrieve()
94
    {
95 10
        $response = $this->execute($this->api->getTokens(), ['tokenId' => $this->id]);
96 4
        $this->populateFromResponse($response);
97 4
    }
98 1
99
    /**
100
     * {@inheritDoc}
101 1
     *
102 1
     * @param array $data {@see \OpenStack\Identity\v3\Api::postTokens}
103
     */
104 9
    public function create(array $data): Creatable
105 1
    {
106 1
        if (isset($data['user'])) {
107 5
            $data['methods'] = ['password'];
108
            if (!isset($data['user']['id']) && empty($data['user']['domain'])) {
109
                throw new \InvalidArgumentException(
110 4
                    'When authenticating with a username, you must also provide either the domain name or domain ID to '
111 4
                    . 'which the user belongs to. Alternatively, if you provide a user ID instead, you do not need to '
112
                    . 'provide domain information.'
113
                );
114
            }
115
        } elseif (isset($data['tokenId'])) {
116
            $data['methods'] = ['token'];
117
        } else {
118
            throw new \InvalidArgumentException('Either a user or token must be provided.');
119
        }
120
121
        $response = $this->execute($this->api->postTokens(), $data);
122
        $token = $this->populateFromResponse($response);
123
124
        // Cache response as an array to export if needed.
125
        // Added key `id` which is auth token from HTTP header X-Subject-Token
126
        $this->cachedToken = Utils::flattenJson(Utils::jsonDecode($response), $this->resourceKey);
127
        $this->cachedToken['id'] = $token->id;
128
129
        return $token;
130
    }
131
132
    /**
133
     * Returns a serialized representation of an authentication token.
134
     *
135
     * Initialize OpenStack object using $params['cachedToken'] to reduce the amount of HTTP calls.
136
     *
137
     * This array is a modified version of response from `/auth/tokens`. Do not manually modify this array.
138
     *
139
     * @return array
140
     */
141
    public function export(): array
142
    {
143
        return $this->cachedToken;
144
    }
145
}
146