Passed
Push — next ( daff69...275875 )
by
unknown
09:23
created

ManagesCollections   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 234
ccs 45
cts 45
cp 1
rs 10
wmc 15

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollection() 0 5 1
A getCollectionWithDocumentCount() 0 5 1
A getCollections() 0 13 1
A getCollectionDocumentCount() 0 5 1
A getCollectionProperties() 0 5 1
A hasCollection() 0 5 1
A createCollection() 0 18 3
A getCollectionStatistics() 0 10 1
A createEdgeCollection() 0 9 1
A updateCollection() 0 7 1
A deleteCollection() 0 5 1
A truncateCollection() 0 5 1
A renameCollection() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArangoClient\Schema;
6
7
use ArangoClient\ArangoClient;
8
use ArangoClient\Exceptions\ArangoException;
9
use stdClass;
10
11
/**
12
 * @see https://www.arangodb.com/docs/stable/http/collection.html
13
 */
14
trait ManagesCollections
15
{
16
    protected ArangoClient $arangoClient;
17
18
    /**
19
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
20
     *
21
     * @param  bool  $excludeSystemCollections
22
     * @return array<mixed>
23
     *
24
     * @throws ArangoException
25
     */
26 39
    public function getCollections(bool $excludeSystemCollections = false): array
27
    {
28 39
        $results = $this->arangoClient->request(
29
            'get',
30
            '/_api/collection',
31
            [
32
                'query' => [
33
                    'excludeSystem' => $excludeSystemCollections,
34
                ],
35
            ]
36
        );
37
38 38
        return (array) $results->result;
39
    }
40
41
    /**
42
     * Check for collection existence in current DB.
43
     *
44
     * @param  string  $name
45
     * @return bool
46
     *
47
     * @throws ArangoException
48
     */
49 31
    public function hasCollection(string $name): bool
50
    {
51 31
        $results = $this->getCollections();
52
53 30
        return array_search($name, array_column($results, 'name'), true) !== false;
54
    }
55
56
    /**
57
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-information-about-a-collection
58
     *
59
     * @throws ArangoException
60
     */
61 1
    public function getCollection(string $name): stdClass
62
    {
63 1
        $uri = '/_api/collection/'.$name;
64
65 1
        return $this->arangoClient->request('get', $uri);
66
    }
67
68
    /**
69
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#read-properties-of-a-collection
70
     *
71
     * @param  string  $name
72
     * @return stdClass
73
     *
74
     * @throws ArangoException
75
     */
76 2
    public function getCollectionProperties(string $name): stdClass
77
    {
78 2
        $uri = '/_api/collection/'.$name.'/properties';
79
80 2
        return $this->arangoClient->request('get', $uri);
81
    }
82
83
    /**
84
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-number-of-documents-in-a-collection
85
     *
86
     * @param  string  $name
87
     * @return stdClass
88
     *
89
     * @throws ArangoException
90
     */
91 3
    public function getCollectionWithDocumentCount(string $name): stdClass
92
    {
93 3
        $uri = '/_api/collection/'.$name.'/count';
94
95 3
        return $this->arangoClient->transactionAwareRequest('get', $uri);
96
    }
97
98
    /**
99
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-number-of-documents-in-a-collection
100
     *
101
     * @param  string  $name
102
     * @return int
103
     *
104
     * @throws ArangoException
105
     */
106 1
    public function getCollectionDocumentCount(string $name): int
107
    {
108 1
        $results = $this->getCollectionWithDocumentCount($name);
109
110 1
        return (int) $results->count;
111
    }
112
113
    /**
114
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-statistics-for-a-collection
115
     *
116
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
117
     *
118
     * @param  string  $name
119
     * @param  bool  $details
120
     * @return stdClass
121
     *
122
     * @throws ArangoException
123
     */
124 2
    public function getCollectionStatistics(string $name, bool $details = false): stdClass
125
    {
126 2
        $uri = '/_api/collection/'.$name.'/figures';
127
128 2
        return $this->arangoClient->request(
129
            'get',
130
            $uri,
131
            [
132
                'query' => [
133
                    'details' => $details,
134
                ],
135
            ]
136
        );
137
    }
138
139
    /**
140
     * @param  string  $name
141
     * @param  array<mixed>  $config
142
     * @param  int|bool|null  $waitForSyncReplication
143
     * @param  int|bool|null  $enforceReplicationFactor
144
     * @return stdClass
145
     *
146
     * @throws ArangoException
147
     */
148 28
    public function createCollection(
149
        string $name,
150
        array $config = [],
151
        $waitForSyncReplication = null,
152
        $enforceReplicationFactor = null
153
    ): stdClass {
154 28
        $options = [];
155 28
        if (isset($waitForSyncReplication)) {
156 1
            $options['query']['waitForSyncReplication'] = (int) $waitForSyncReplication;
157
        }
158 28
        if (isset($enforceReplicationFactor)) {
159 1
            $options['query']['enforceReplicationFactor'] = (int) $enforceReplicationFactor;
160
        }
161
162 28
        $collection = array_merge($config, ['name' => $name]);
163 28
        $options['body'] = $collection;
164
165 28
        return $this->arangoClient->request('post', '/_api/collection', $options);
166
    }
167
168
    /**
169
     * @param  string  $name
170
     * @param  array<mixed>  $config
171
     * @param  int|bool|null  $waitForSyncReplication
172
     * @param  int|bool|null  $enforceReplicationFactor
173
     * @return stdClass
174
     *
175 1
     * @throws ArangoException
176
     */
177 1
    public function createEdgeCollection(
178
        string $name,
179 1
        array $config = [],
180
        $waitForSyncReplication = null,
181 1
        $enforceReplicationFactor = null
182
    ): stdClass {
183
        $config['type'] = 3;
184
185
        return $this->createCollection($name, $config, $waitForSyncReplication, $enforceReplicationFactor);
186
    }
187
188
    /**
189
     * @param  string  $name
190
     * @param  array<mixed>  $config
191 1
     * @return stdClass
192
     *
193 1
     * @throws ArangoException
194
     */
195 1
    public function updateCollection(string $name, array $config = []): stdClass
196
    {
197 1
        $uri = '/_api/collection/'.$name.'/properties';
198
199
        $options = ['body' => $config];
200
201 1
        return $this->arangoClient->request('put', $uri, $options);
202
    }
203
204
    /**
205
     * @param  string  $old
206
     * @param  string  $new
207
     * @return stdClass
208
     *
209
     * @throws ArangoException
210 1
     */
211
    public function renameCollection(string $old, string $new): stdClass
212 1
    {
213
        $uri = '/_api/collection/'.$old.'/rename';
214 1
215
        $options = [
216
            'body' => [
217
                'name' => $new,
218
            ],
219
        ];
220
221
        return $this->arangoClient->request('put', $uri, $options);
222
    }
223 33
224
    /**
225 33
     * @param  string  $name
226
     * @return stdClass
227 33
     *
228
     * @throws ArangoException
229
     */
230
    public function truncateCollection(string $name): stdClass
231
    {
232
        $uri = '/_api/collection/'.$name.'/truncate';
233
234
        return $this->arangoClient->request('put', $uri);
235
    }
236
237
    /**
238
     * @param  string  $name
239
     * @return bool
240
     *
241
     * @throws ArangoException
242
     */
243
    public function deleteCollection(string $name): bool
244
    {
245
        $uri = '/_api/collection/'.$name;
246
247
        return (bool) $this->arangoClient->request('delete', $uri);
248
    }
249
}
250