Failed Conditions
Push — feature/add-delete-all-collect... ( 76c9d5...f7a1f8 )
by Bas
02:17
created

ManagesCollections::deleteAllCollections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
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
    public function getCollections(bool $excludeSystemCollections = false): array
26
    {
27
        $results = $this->arangoClient->request(
28
            'get',
29
            '/_api/collection',
30
            [
31
                'query' => [
32
                    'excludeSystem' => $excludeSystemCollections,
33
                ],
34
            ],
35
        );
36
37
        return (array) $results->result;
38
    }
39
40
    /**
41
     * Check for collection existence in current DB.
42
     *
43
     *
44
     * @throws ArangoException
45
     */
46
    public function hasCollection(string $name): bool
47
    {
48
        $results = $this->getCollections();
49
50
        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
    public function getCollection(string $name): stdClass
59
    {
60
        $uri = '/_api/collection/' . $name;
61
62
        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
    public function getCollectionProperties(string $name): stdClass
71
    {
72
        $uri = '/_api/collection/' . $name . '/properties';
73
74
        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
    public function getCollectionWithDocumentCount(string $name): stdClass
83
    {
84
        $uri = '/_api/collection/' . $name . '/count';
85
86
        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
    public function getCollectionDocumentCount(string $name): int
95
    {
96
        $results = $this->getCollectionWithDocumentCount($name);
97
98
        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
    public function getCollectionStatistics(string $name, bool $details = false): stdClass
109
    {
110
        $uri = '/_api/collection/' . $name . '/figures';
111
112
        return $this->arangoClient->request(
113
            'get',
114
            $uri,
115
            [
116
                'query' => [
117
                    'details' => $details,
118
                ],
119
            ],
120
        );
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
    public function createCollection(
131
        string $name,
132
        array $config = [],
133
        $waitForSyncReplication = null,
134
        $enforceReplicationFactor = null,
135
    ): stdClass {
136
        $options = [];
137
        if (isset($waitForSyncReplication)) {
138
            $options['query']['waitForSyncReplication'] = (int) $waitForSyncReplication;
139
        }
140
        if (isset($enforceReplicationFactor)) {
141
            $options['query']['enforceReplicationFactor'] = (int) $enforceReplicationFactor;
142
        }
143
144
        $collection = array_merge($config, ['name' => $name]);
145
        $options['body'] = $collection;
146
147
        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
    public function createEdgeCollection(
158
        string $name,
159
        array $config = [],
160
        $waitForSyncReplication = null,
161
        $enforceReplicationFactor = null,
162
    ): stdClass {
163
        $config['type'] = 3;
164
165
        return $this->createCollection($name, $config, $waitForSyncReplication, $enforceReplicationFactor);
166
    }
167
168
    /**
169
     * @param  array<mixed>  $config
170
     *
171
     * @throws ArangoException
172
     */
173
    public function updateCollection(string $name, array $config = []): stdClass
174
    {
175
        $uri = '/_api/collection/' . $name . '/properties';
176
177
        $options = ['body' => $config];
178
179
        return $this->arangoClient->request('put', $uri, $options);
180
    }
181
182
    /**
183
     * @throws ArangoException
184
     */
185
    public function renameCollection(string $old, string $new): stdClass
186
    {
187
        $uri = '/_api/collection/' . $old . '/rename';
188
189
        $options = [
190
            'body' => [
191
                'name' => $new,
192
            ],
193
        ];
194
195
        return $this->arangoClient->request('put', $uri, $options);
196
    }
197
198
    /**
199
     * @throws ArangoException
200
     */
201
    public function truncateCollection(string $name): stdClass
202
    {
203
        $uri = '/_api/collection/' . $name . '/truncate';
204
205
        return $this->arangoClient->request('put', $uri);
206
    }
207
208
    /**
209
     * @throws ArangoException
210
     */
211
    public function deleteCollection(string $name): bool
212
    {
213
        $uri = '/_api/collection/' . $name;
214
215
        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