-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathGitHubFactory.php
112 lines (97 loc) · 3.04 KB
/
GitHubFactory.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
<?php
declare(strict_types=1);
/*
* This file is part of Laravel GitHub.
*
* (c) Graham Campbell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace GrahamCampbell\GitHub;
use Github\Client;
use Github\HttpClient\Builder;
use GrahamCampbell\GitHub\Auth\Authenticator\AuthenticatorInterface;
use GrahamCampbell\GitHub\Auth\AuthenticatorFactory;
use GrahamCampbell\GitHub\Cache\ConnectionFactory;
use GrahamCampbell\GitHub\HttpClient\BuilderFactory;
use Http\Client\Common\Plugin\RetryPlugin;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Symfony\Component\Cache\Adapter\Psr16Adapter;
/**
* This is the github factory class.
*
* @author Graham Campbell <[email protected]>
*/
class GitHubFactory
{
/**
* Create a new github factory instance.
*
* @param \GrahamCampbell\GitHub\HttpClient\BuilderFactory $builder
* @param \GrahamCampbell\GitHub\Auth\AuthenticatorFactory $auth
* @param \GrahamCampbell\GitHub\Cache\ConnectionFactory $cache
*
* @return void
*/
public function __construct(
private readonly BuilderFactory $builder,
private readonly AuthenticatorFactory $auth,
private readonly ConnectionFactory $cache,
) {
}
/**
* Make a new github client.
*
* @param string[] $config
*
* @throws \InvalidArgumentException
*
* @return \Github\Client
*/
public function make(array $config): Client
{
$client = new Client($this->getBuilder($config), Arr::get($config, 'version'), Arr::get($config, 'enterprise'));
if (!array_key_exists('method', $config)) {
throw new InvalidArgumentException('The github factory requires an auth method.');
}
if ($config['method'] === 'none') {
return $client;
}
return $this->getAuthenticator($config['method'])->with($client)->authenticate($config);
}
/**
* Get the http client builder.
*
* @param string[] $config
*
* @return \Github\HttpClient\Builder
*/
protected function getBuilder(array $config): Builder
{
$builder = $this->builder->make();
if ($backoff = Arr::get($config, 'backoff')) {
$builder->addPlugin(new RetryPlugin(['retries' => $backoff === true ? 2 : $backoff]));
}
if (is_array($cache = Arr::get($config, 'cache', false))) {
$boundedCache = $this->cache->make($cache);
$builder->addCache(
new Psr16Adapter($boundedCache),
['cache_lifetime' => $boundedCache->getMaximumLifetime()]
);
}
return $builder;
}
/**
* Get the authenticator.
*
* @throws \InvalidArgumentException
*
* @return \GrahamCampbell\GitHub\Auth\Authenticator\AuthenticatorInterface
*/
protected function getAuthenticator(string $method): AuthenticatorInterface
{
return $this->auth->make($method);
}
}