-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathConnection.php
More file actions
138 lines (121 loc) · 3.99 KB
/
Connection.php
File metadata and controls
138 lines (121 loc) · 3.99 KB
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
<?php
declare(strict_types=1);
namespace Muffin\Webservice\Datasource;
use Cake\Core\App;
use Cake\Datasource\ConnectionInterface;
use Muffin\Webservice\Datasource\Exception\MissingConnectionException;
use Muffin\Webservice\Webservice\Driver\AbstractDriver;
use Muffin\Webservice\Webservice\Exception\MissingDriverException;
use Muffin\Webservice\Webservice\Exception\UnexpectedDriverException;
use Psr\SimpleCache\CacheInterface;
/**
* Class Connection
*
* @method \Muffin\Webservice\Webservice\Driver\AbstractDriver setWebservice(string $name, \Muffin\Webservice\Webservice\WebserviceInterface $webservice) Proxy method through to the Driver
* @method \Muffin\Webservice\Webservice\WebserviceInterface getWebservice(string $name) Proxy method through to the Driver
* @method string configName() Proxy method through to the Driver
*/
class Connection implements ConnectionInterface
{
/**
* Driver
*
* @var \Muffin\Webservice\Webservice\Driver\AbstractDriver
*/
protected ?AbstractDriver $_driver = null;
protected CacheInterface $cacher;
/**
* The connection name in the connection manager.
*/
protected string $configName = '';
/**
* Constructor
*
* @param array $config Custom configuration.
* @throws \Muffin\Webservice\Webservice\Exception\UnexpectedDriverException If the driver is not an instance of `Muffin\Webservice\AbstractDriver`.
*/
public function __construct(array $config)
{
if (isset($config['name'])) {
$this->configName = $config['name'];
}
$config = $this->_normalizeConfig($config);
$driver = $config['driver'];
unset($config['driver'], $config['service']);
$this->_driver = new $driver($config);
if (!($this->_driver instanceof AbstractDriver)) {
throw new UnexpectedDriverException(['driver' => $driver]);
}
}
/**
* @param \Psr\SimpleCache\CacheInterface $cacher
* @return void
*/
public function setCacher(CacheInterface $cacher): void
{
}
/** @return \Psr\SimpleCache\CacheInterface */
public function getCacher(): CacheInterface
{
}
/**
* {@inheritDoc}
*
* @see \Cake\Datasource\ConnectionInterface::getDriver()
* @return \Muffin\Webservice\Webservice\Driver\AbstractDriver
*/
public function getDriver(string $role = self::ROLE_WRITE): object
{
return $this->_driver;
}
/**
* Get the configuration name for this connection.
*
* @return string
*/
public function configName(): string
{
return $this->configName;
}
/**
* Get the config data for this connection.
*
* @return array
*/
public function config(): array
{
return $this->_driver->getConfig();
}
/**
* Validates certain custom configuration values.
*
* @param array $config Raw custom configuration.
* @return array
* @throws \Muffin\Webservice\Datasource\Exception\MissingConnectionException If the connection does not exist.
* @throws \Muffin\Webservice\Webservice\Exception\MissingDriverException If the driver does not exist.
*/
protected function _normalizeConfig(array $config): array
{
if (empty($config['driver'])) {
if (empty($config['service'])) {
throw new MissingConnectionException(['name' => $config['name']]);
}
$config['driver'] = App::className($config['service'], 'Webservice/Driver', 'Driver');
if (!$config['driver']) {
throw new MissingDriverException(['driver' => $config['driver']]);
}
}
return $config;
}
/**
* Proxies the driver's methods.
*
* @param string $method Method name.
* @param array $args Arguments to pass-through
* @return mixed
*/
public function __call(string $method, array $args): mixed
{
return call_user_func_array([$this->_driver, $method], $args);
}
}