ManagesCollections   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 216
ccs 67
cts 67
cp 1
rs 10
wmc 17

14 Methods

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