-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
EnvironmentAuthConfig.php
60 lines (49 loc) · 1.69 KB
/
EnvironmentAuthConfig.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
<?php declare(strict_types=1);
/*
* This file is part of Phive.
*
* Copyright (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de> and contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PharIo\Phive;
use function array_key_exists;
use function sprintf;
use BadMethodCallException;
class EnvironmentAuthConfig implements AuthConfig {
private const KNOWN_TOKEN = [
'api.github.com' => 'GITHUB_AUTH_TOKEN',
'gitlab.com' => 'GITLAB_AUTH_TOKEN',
];
/** @var Environment */
private $environment;
public function __construct(Environment $environment) {
$this->environment = $environment;
}
public function hasAuthentication(string $domain): bool {
if (!array_key_exists($domain, self::KNOWN_TOKEN)) {
return false;
}
return $this->environment->hasVariable(self::KNOWN_TOKEN[$domain]);
}
/**
* @throws AuthException
* @throws BadMethodCallException
*/
public function getAuthentication(string $domain): Authentication {
if (!$this->hasAuthentication($domain)) {
throw new AuthException(sprintf('No authentication data for %s', $domain));
}
$token = $this->environment->getVariable(self::KNOWN_TOKEN[$domain]);
switch ($domain) {
case 'api.github.com':
return new TokenAuthentication($token);
case 'gitlab.com':
return new BearerAuthentication($token);
default:
throw new BadMethodCallException('Unknown authentication');
}
}
}