Failed Conditions
Push — next ( 1e4ade...f47ec0 )
by Bas
04:16 queued 11s
created

ManagesCollections::truncateCollection()   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 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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
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 32
    public function getCollections(bool $excludeSystemCollections = false): array
26
    {
27 32
        $results = $this->arangoClient->request(
28 32
            'get',
29 32
            '/_api/collection',
30
            [
31
                'query' => [
32 32
                    'excludeSystem' => $excludeSystemCollections
33
                ]
34
            ]
35
        );
36
37 32
        return (array) $results['result'];
38
    }
39
40
    /**
41
     * Check for collection existence in current DB.
42
     *
43
     * @param  string  $collection
44
     * @return bool
45
     * @throws ArangoException
46
     */
47 24
    public function hasCollection(string $collection): bool
48
    {
49 24
        $results = $this->getCollections();
50 24
        return array_search($collection, 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  $collection
70
     * @return array<mixed>
71
     * @throws ArangoException
72
     */
73 1
    public function getCollectionProperties(string $collection): array
74
    {
75 1
        $uri = '/_api/collection/' . $collection . '/properties';
76 1
        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  $collection
83
     * @return array<mixed>
84
     * @throws ArangoException
85
     */
86 3
    public function getCollectionWithDocumentCount(string $collection): array
87
    {
88 3
        $uri = '/_api/collection/' . $collection . '/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  $collection
96
     * @return int
97
     * @throws ArangoException
98
     */
99 1
    public function getCollectionDocumentCount(string $collection): int
100
    {
101 1
        $results = $this->getCollectionWithDocumentCount($collection);
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  $collection
112
     * @param  bool  $details
113
     * @return array<mixed>
114
     * @throws ArangoException
115
     */
116 2
    public function getCollectionStatistics(string $collection, bool $details = false): array
117
    {
118 2
        $uri = '/_api/collection/' . $collection . '/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  $collection
132
     * @param  array<mixed>  $config
133
     * @param  int|null  $waitForSyncReplication
134
     * @param  int|null  $enforceReplicationFactor
135
     * @return bool
136
     * @throws ArangoException
137
     */
138 23
    public function createCollection(
139
        string $collection,
140
        array $config = [],
141
        $waitForSyncReplication = null,
142
        $enforceReplicationFactor = null
143
    ): bool {
144 23
        $collection = json_encode((object) array_merge($config, ['name' => $collection]));
145
146 23
        $options = ['body' => $collection];
147 23
        if (isset($waitForSyncReplication)) {
148
            $options['query']['waitForSyncReplication'] = $waitForSyncReplication;
149
        }
150 23
        if (isset($enforceReplicationFactor)) {
151
            $options['query']['enforceReplicationFactor'] = $enforceReplicationFactor;
152
        }
153
154 23
        return (bool) $this->arangoClient->request('post', '/_api/collection', $options);
155
    }
156
157
    /**
158
     * @param  string  $name
159
     * @param  array<mixed>  $config
160
     * @return array<mixed>
161
     * @throws ArangoException
162
     */
163 1
    public function updateCollection(string $name, array $config = []): array
164
    {
165 1
        $uri = '/_api/collection/' . $name . '/properties';
166
167 1
        $config = json_encode((object) $config);
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 1
        $newName = json_encode((object) ['name' => $new]);
184 1
        $options = ['body' => $newName];
185
186 1
        return $this->arangoClient->request('put', $uri, $options);
187
    }
188
189
    /**
190
     * @param  string  $name
191
     * @return bool
192
     * @throws ArangoException
193
     */
194 1
    public function truncateCollection(string $name): bool
195
    {
196 1
        $uri = '/_api/collection/' . $name . '/truncate';
197
198 1
        return (bool) $this->arangoClient->request('put', $uri);
199
    }
200
201
    /**
202
     * @param  string  $name
203
     * @return bool
204
     * @throws ArangoException
205
     */
206 23
    public function deleteCollection(string $name): bool
207
    {
208 23
        $uri = '/_api/collection/' . $name;
209
210 23
        return (bool) $this->arangoClient->request('delete', $uri);
211
    }
212
}
213