Passed
Push — next ( d904ac...c0af08 )
by Bas
13:43
created

ManagesCollections::deleteCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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