1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Cwd PowerDNS Client |
4
|
|
|
* |
5
|
|
|
* (c) 2018 cwd.at GmbH <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Cwd\PowerDNSClient; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
16
|
|
|
|
17
|
|
|
class PowerDNSClientFactory |
18
|
|
|
{ |
19
|
|
|
private $config = []; |
20
|
|
|
private $reader; |
21
|
|
|
|
22
|
|
|
const SLAVE = 'slave'; |
23
|
|
|
const MASTER = 'master'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array<string,PowerDNSClient> |
27
|
|
|
*/ |
28
|
|
|
private $clients = []; |
29
|
|
|
|
30
|
|
|
public function __construct(array $config = [], Reader $reader) |
31
|
|
|
{ |
32
|
|
|
$this->config = $config; |
33
|
|
|
$this->reader = $reader; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getClient(string $name): PowerDNSClient |
37
|
|
|
{ |
38
|
|
|
if (isset($this->clients[$name]) && $this->clients[$name] instanceof PowerDNSClient) { |
39
|
|
|
return $this->clients[$name]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
foreach ($this->config as $configName => $config) { |
43
|
|
|
if ($name === $configName) { |
44
|
|
|
$client = new Client($config['uri'], $config['api_key'], null, $this->reader); |
45
|
|
|
$this->clients[$name] = new PowerDNSClient($client); |
46
|
|
|
|
47
|
|
|
return $this->clients[$name]; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
throw new \RuntimeException(sprintf('No configuration for "%s% is found', $name)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array<PowerDNSClient> |
56
|
|
|
*/ |
57
|
|
|
public function getSlaves(): array |
58
|
|
|
{ |
59
|
|
|
$clients = []; |
60
|
|
|
|
61
|
|
|
foreach ($this->config as $configName => $config) { |
62
|
|
|
if (isset($config['type']) && $config['type'] == self::SLAVE) { |
63
|
|
|
$clients[] = $this->getClient($configName); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $clients; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $uri |
73
|
|
|
* @param string $apiKey |
74
|
|
|
* @param string|null $defaultServer |
75
|
|
|
* @param Reader $reader |
76
|
|
|
* @return PowerDNSClient |
77
|
|
|
*/ |
78
|
|
|
static public function createClient(string $uri, string $apiKey, ?string $defaultServer = null, Reader $reader): PowerDNSClient |
79
|
|
|
{ |
80
|
|
|
$client = new Client($uri, $apiKey, null, $reader); |
81
|
|
|
$pdns = new PowerDNSClient($client); |
82
|
|
|
if ($defaultServer !== null) { |
83
|
|
|
$pdns->setDefaultServerId($defaultServer); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $pdns; |
87
|
|
|
} |
88
|
|
|
} |