-
Notifications
You must be signed in to change notification settings - Fork 1
/
BergenAdapter.php
75 lines (65 loc) · 2.1 KB
/
BergenAdapter.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
<?php
namespace ObjectStorage\Adapter;
use Bergen\Client\ClientBuilder;
use Bergen\Client\ClientOptions;
use Bergen\Client\Error\UnexpectedResponseError;
use Bergen\Client\V1\V1StorageClient;
use GuzzleHttp\RequestOptions;
class BergenAdapter implements BuildableAdapterInterface, StorageAdapterInterface
{
/**
* @var \Bergen\Client\V1\V1StorageClient
*/
private $client;
public static function build(array $config)
{
if (!array_key_exists('host', $config)) {
throw new InvalidArgumentException(
'Unable to build BergenAdapter: missing "host" from configuration.'
);
}
$clientOpts = [
ClientOptions::API_HOST => trim($config['host']),
];
if (isset($config['username']) && isset($config['password'])) {
$clientOpts[RequestOptions::AUTH] = [
trim($config['username']),
$config['password'],
];
}
if (array_key_exists('secure', $config)) {
$clientOpts[ClientOptions::SECURE_HTTP] = (bool) $config['secure'];
}
return new self(new V1StorageClient(new ClientBuilder($clientOpts)));
}
public function __construct(V1StorageClient $client)
{
$this->client = $client;
}
public function setData($key, $data)
{
try {
$this->client->put(rawurlencode($key), $data);
} catch (UnexpectedResponseError $e) {
throw new AdapterException('Unable to set data.', null, $e);
}
}
public function getData($key)
{
$response = null;
try {
$response = $this->client->get(rawurlencode($key));
} catch (UnexpectedResponseError $e) {
throw new AdapterException('Unable to get data.', null, $e);
}
return (string) $response->getBody();
}
public function deleteData($key)
{
try {
$this->client->delete(rawurlencode($key));
} catch (UnexpectedResponseError $e) {
throw new AdapterException('Unable to delete data.', null, $e);
}
}
}