-
Notifications
You must be signed in to change notification settings - Fork 4
/
GitlabResourceOwner.php
163 lines (140 loc) · 3.64 KB
/
GitlabResourceOwner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/*
* Gitlab OAuth2 Provider
* (c) Omines Internetbureau B.V. - https://omines.nl/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Omines\OAuth2\Client\Provider;
use Gitlab\Client;
use Gitlab\HttpClient\Builder;
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
use League\OAuth2\Client\Token\AccessToken;
/**
* GitlabResourceOwner.
*
* @author Niels Keurentjes <[email protected]>
*
* @phpstan-type ResourceOwner array{id: int, is_admin: bool, name: string, username: string, email: string, avatar_url: string, web_url: string, state: string, external: bool}
*/
class GitlabResourceOwner implements ResourceOwnerInterface
{
public const PATH_API = '/api/v4/';
/** @var ResourceOwner */
private array $data;
private string $domain;
private AccessToken $token;
/**
* Creates new resource owner.
*
* @param ResourceOwner $response
*/
public function __construct(array $response, AccessToken $token)
{
$this->data = $response;
$this->token = $token;
}
/**
* Returns the identifier of the authorized resource owner.
*/
public function getId(): int
{
return (int) ($this->data['id'] ?? 0);
}
/**
* Returns an authenticated API client.
*
* Requires optional Gitlab API client to be installed.
*
* @infection-ignore-all Cannot be tested for infection due to external dependency
*/
public function getApiClient(?Builder $builder = null): Client
{
if (!class_exists('\\Gitlab\\Client')) {
throw new \LogicException(__METHOD__ . ' requires package m4tthumphrey/php-gitlab-api to be installed and autoloaded'); // @codeCoverageIgnore
}
$client = new Client($builder);
$client->setUrl(rtrim($this->domain, '/') . self::PATH_API);
$client->authenticate($this->token->getToken(), Client::AUTH_OAUTH_TOKEN);
return $client;
}
public function getDomain(): string
{
return $this->domain;
}
public function setDomain(string $domain): self
{
$this->domain = $domain;
return $this;
}
/**
* The full name of the owner.
*/
public function getName(): string
{
return $this->data['name'];
}
/**
* Username of the owner.
*/
public function getUsername(): string
{
return $this->data['username'];
}
/**
* Email address of the owner.
*/
public function getEmail(): string
{
return $this->data['email'];
}
/**
* URL to the user's avatar.
*/
public function getAvatarUrl(): ?string
{
return $this->data['avatar_url'];
}
/**
* URL to the user's profile page.
*/
public function getProfileUrl(): ?string
{
return $this->data['web_url'];
}
public function getToken(): AccessToken
{
return $this->token;
}
/**
* Whether the user is active.
*/
public function isActive(): bool
{
return 'active' === ($this->data['state'] ?? null);
}
/**
* Whether the user is an admin.
*/
public function isAdmin(): bool
{
return $this->data['is_admin'] ?? false;
}
/**
* Whether the user is external.
*/
public function isExternal(): bool
{
return $this->data['external'] ?? true;
}
/**
* Return all of the owner details available as an array.
*
* @return ResourceOwner
*/
public function toArray(): array
{
return $this->data;
}
}