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
Pull Request — master (#159)
by Roman
01:49
created

Token::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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