1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018-2020 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace ArangoDb\Handler; |
13
|
|
|
|
14
|
|
|
use ArangoDb\Exception\GuardErrorException; |
15
|
|
|
use ArangoDb\Exception\UnexpectedResponse; |
16
|
|
|
use ArangoDb\Guard\Guard; |
17
|
|
|
use ArangoDb\Guard\SuccessHttpStatusCode; |
18
|
|
|
use ArangoDb\Http\TypeSupport; |
19
|
|
|
use ArangoDb\Type\Collection as CollectionType; |
20
|
|
|
use ArangoDb\Util\Json; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
|
23
|
|
|
final class Collection implements CollectionHandler |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TypeSupport |
27
|
|
|
**/ |
28
|
|
|
private $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Guard |
32
|
|
|
*/ |
33
|
|
|
private $guard; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $collectionClass; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param TypeSupport $client |
42
|
|
|
* @param string $collectionClass FQCN of the class which implements \ArangoDb\Type\CollectionType |
43
|
|
|
* @param Guard|null $guard |
44
|
|
|
*/ |
45
|
4 |
|
public function __construct( |
46
|
|
|
TypeSupport $client, |
47
|
|
|
string $collectionClass = CollectionType::class, |
48
|
|
|
Guard $guard = null |
49
|
|
|
) { |
50
|
4 |
|
$this->client = $client; |
51
|
4 |
|
$this->collectionClass = $collectionClass; |
52
|
4 |
|
$this->guard = $guard ?? SuccessHttpStatusCode::withoutContentId(); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
4 |
|
public function create(string $collectionName, array $options = []): string |
56
|
|
|
{ |
57
|
4 |
|
$type = ($this->collectionClass)::create($collectionName, $options) |
58
|
4 |
|
->useGuard($this->guard); |
59
|
|
|
|
60
|
4 |
|
$response = $this->client->sendType($type); |
61
|
|
|
|
62
|
4 |
|
$data = Json::decode($response->getBody()->getContents()); |
63
|
|
|
|
64
|
4 |
|
if (! isset($data['id'])) { |
65
|
|
|
throw UnexpectedResponse::forType($type, $response); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
return $data['id']; |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
public function has(string $collectionName): bool |
72
|
|
|
{ |
73
|
3 |
|
$type = ($this->collectionClass)::info($collectionName) |
74
|
3 |
|
->useGuard($this->guard); |
75
|
|
|
|
76
|
|
|
try { |
77
|
3 |
|
$this->client->sendType($type); |
78
|
|
|
|
79
|
3 |
|
return true; |
80
|
1 |
|
} catch (GuardErrorException $e) { |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function drop(string $collectionName): void |
86
|
|
|
{ |
87
|
1 |
|
$type = ($this->collectionClass)::delete($collectionName) |
88
|
1 |
|
->useGuard($this->guard); |
89
|
|
|
|
90
|
1 |
|
$this->client->sendType($type); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
2 |
|
public function count(string $collectionName): int |
94
|
|
|
{ |
95
|
2 |
|
$type = ($this->collectionClass)::count($collectionName) |
96
|
2 |
|
->useGuard($this->guard); |
97
|
|
|
|
98
|
2 |
|
$response = $this->client->sendType($type); |
99
|
|
|
|
100
|
2 |
|
$data = Json::decode($response->getBody()->getContents()); |
101
|
|
|
|
102
|
2 |
|
if (! isset($data['count'])) { |
103
|
|
|
throw UnexpectedResponse::forType($type, $response); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
return $data['count']; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
public function get(string $collectionName): ResponseInterface |
110
|
|
|
{ |
111
|
1 |
|
$type = ($this->collectionClass)::info($collectionName) |
112
|
1 |
|
->useGuard($this->guard); |
113
|
|
|
|
114
|
1 |
|
return $this->client->sendType($type); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
public function truncate(string $collectionName): void |
118
|
|
|
{ |
119
|
1 |
|
$type = ($this->collectionClass)::truncate($collectionName) |
120
|
1 |
|
->useGuard($this->guard); |
121
|
|
|
|
122
|
1 |
|
$this->client->sendType($type); |
123
|
1 |
|
} |
124
|
|
|
} |
125
|
|
|
|