-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
DigitalOceanFactory.php
108 lines (94 loc) · 2.73 KB
/
DigitalOceanFactory.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
<?php
declare(strict_types=1);
/*
* This file is part of Laravel DigitalOcean.
*
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace GrahamCampbell\DigitalOcean;
use DigitalOceanV2\Client;
use DigitalOceanV2\HttpClient\Builder;
use GrahamCampbell\DigitalOcean\Auth\Authenticator\AuthenticatorInterface;
use GrahamCampbell\DigitalOcean\Auth\AuthenticatorFactory;
use GrahamCampbell\DigitalOcean\HttpClient\BuilderFactory;
use Illuminate\Support\Arr;
use InvalidArgumentException;
/**
* This is the DigitalOcean factory class.
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*/
class DigitalOceanFactory
{
/**
* The http client builder factory instance.
*
* @var \GrahamCampbell\DigitalOcean\HttpClient\BuilderFactory
*/
private BuilderFactory $builder;
/**
* The authenticator factory instance.
*
* @var \GrahamCampbell\DigitalOcean\Auth\AuthenticatorFactory
*/
private AuthenticatorFactory $auth;
/**
* Create a new DigitalOcean factory instance.
*
* @param \GrahamCampbell\DigitalOcean\HttpClient\BuilderFactory $builder
* @param \GrahamCampbell\DigitalOcean\Auth\AuthenticatorFactory $auth
*
* @return void
*/
public function __construct(BuilderFactory $builder, AuthenticatorFactory $auth)
{
$this->builder = $builder;
$this->auth = $auth;
}
/**
* Make a new DigitalOcean client.
*
* @param string[] $config
*
* @throws \InvalidArgumentException
*
* @return \DigitalOceanV2\Client
*/
public function make(array $config): Client
{
$client = new Client($this->getBuilder($config));
if (!array_key_exists('method', $config)) {
throw new InvalidArgumentException('The DigitalOcean factory requires an auth method.');
}
if ($url = Arr::get($config, 'url')) {
$client->setUrl($url);
}
if ($config['method'] === 'none') {
return $client;
}
return $this->getAuthenticator($config['method'])->with($client)->authenticate($config);
}
/**
* Get the http client builder.
*
* @return \DigitalOceanV2\HttpClient\Builder
*/
protected function getBuilder(): Builder
{
return $this->builder->make();
}
/**
* Get the authenticator.
*
* @throws \InvalidArgumentException
*
* @return \GrahamCampbell\DigitalOcean\Auth\Authenticator\AuthenticatorInterface
*/
protected function getAuthenticator(string $method): AuthenticatorInterface
{
return $this->auth->make($method);
}
}