Passed
Push — next ( fad6bc...2b9454 )
by Bas
02:29
created

ManagesCollections   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Test Coverage

Coverage 95.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 38
dl 0
loc 182
c 1
b 0
f 0
ccs 41
cts 43
cp 0.9535
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollection() 0 4 1
A updateCollection() 0 8 1
A deleteCollection() 0 5 1
A getCollections() 0 8 1
A getCollectionDocumentCount() 0 4 1
A hasCollection() 0 4 1
A renameCollection() 0 8 1
A getCollectionProperties() 0 4 1
A createCollection() 0 17 3
A getCollectionStatistics() 0 9 1
1
<?php
2
3
namespace ArangoClient\Schema;
4
5
use ArangoClient\Connector;
6
use ArangoClient\Exceptions\ArangoException;
7
use GuzzleHttp\Exception\GuzzleException;
8
9
/*
10
 * @see https://www.arangodb.com/docs/stable/http/collection.html
11
 */
12
trait ManagesCollections
13
{
14
    protected Connector $connector;
15
16
    /**
17
     *
18
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
19
     *
20
     * @param  bool  $excludeSystemCollections
21
     * @return array<mixed>
22
     * @throws ArangoException
23
     * @throws GuzzleException
24
     */
25 11
    public function getCollections(bool $excludeSystemCollections = false): array
26
    {
27 11
        return (array) $this->connector->request(
28 11
            'get',
29 11
            '/_api/collection',
30
            [
31
                'query' => [
32 11
                    'excludeSystem' => $excludeSystemCollections
33
                ]
34
            ]
35
        );
36
    }
37
38
39
    /**
40
     * Check for collection existence in current DB.
41
     *
42
     * @param  string  $collection
43
     * @return bool
44
     * @throws ArangoException
45
     * @throws GuzzleException
46
     */
47 4
    public function hasCollection(string $collection): bool
48
    {
49 4
        $collections = $this->getCollections();
50 4
        return array_search($collection, array_column($collections, '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  $collection
57
     * @return array<mixed>
58
     * @throws ArangoException
59
     * @throws GuzzleException
60
     */
61 1
    public function getCollection(string $collection): array
62
    {
63 1
        $uri = '/_api/collection/' . $collection;
64 1
        return (array) $this->connector->request('get', $uri);
65
    }
66
67
    /**
68
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#read-properties-of-a-collection
69
     *
70
     * @param  string  $collection
71
     * @return array<mixed>
72
     * @throws ArangoException
73
     * @throws GuzzleException
74
     */
75 1
    public function getCollectionProperties(string $collection): array
76
    {
77 1
        $uri = '/_api/collection/' . $collection . '/properties';
78 1
        return (array) $this->connector->request('get', $uri);
79
    }
80
81
    /**
82
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-number-of-documents-in-a-collection
83
     *
84
     * @param  string  $collection
85
     * @return array<mixed>
86
     * @throws ArangoException
87
     * @throws GuzzleException
88
     */
89 1
    public function getCollectionDocumentCount(string $collection): array
90
    {
91 1
        $uri = '/_api/collection/' . $collection . '/count';
92 1
        return (array) $this->connector->request('get', $uri);
93
    }
94
95
96
    /**
97
     * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-statistics-for-a-collection
98
     *
99
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
100
     *
101
     * @param  string  $collection
102
     * @param  bool  $details
103
     * @return array<mixed>
104
     * @throws ArangoException
105
     * @throws GuzzleException
106
     */
107 2
    public function getCollectionStatistics(string $collection, bool $details = false): array
108
    {
109 2
        $uri = '/_api/collection/' . $collection . '/figures';
110 2
        return (array) $this->connector->request(
111 2
            'get',
112
            $uri,
113
            [
114
                'query' => [
115 2
                    'details' => $details
116
                ]
117
            ]
118
        );
119
    }
120
121
    /**
122
     * @param  string  $collection
123
     * @param  array<mixed>  $config
124
     * @param  int|null  $waitForSyncReplication
125
     * @param  int|null  $enforceReplicationFactor
126
     * @return bool
127
     * @throws ArangoException
128
     * @throws GuzzleException
129
     */
130 3
    public function createCollection(
131
        string $collection,
132
        array $config = [],
133
        $waitForSyncReplication = null,
134
        $enforceReplicationFactor = null
135
    ): bool {
136 3
        $collection = json_encode((object) array_merge($config, ['name' => $collection]));
137
138 3
        $options = ['body' => $collection];
139 3
        if (isset($waitForSyncReplication)) {
140
            $options['query']['waitForSyncReplication'] = $waitForSyncReplication;
141
        }
142 3
        if (isset($enforceReplicationFactor)) {
143
            $options['query']['enforceReplicationFactor'] = $enforceReplicationFactor;
144
        }
145
146 3
        return (bool) $this->connector->request('post', '/_api/collection', $options);
147
    }
148
149
    /**
150
     * @param  string  $name
151
     * @param  array<mixed>  $config
152
     * @return array<mixed>
153
     * @throws ArangoException
154
     * @throws GuzzleException
155
     */
156 1
    public function updateCollection(string $name, array $config = []): array
157
    {
158 1
        $uri = '/_api/collection/' . $name . '/properties';
159
160 1
        $config = json_encode((object) $config);
161 1
        $options = ['body' => $config];
162
163 1
        return (array) $this->connector->request('put', $uri, $options);
164
    }
165
166
    /**
167
     * @param  string  $old
168
     * @param  string  $new
169
     * @return array<mixed>
170
     * @throws ArangoException
171
     * @throws GuzzleException
172
     */
173 1
    public function renameCollection(string $old, string $new): array
174
    {
175 1
        $uri = '/_api/collection/' . $old . '/rename';
176
177 1
        $newName = json_encode((object) ['name' => $new]);
178 1
        $options = ['body' => $newName];
179
180 1
        return (array) $this->connector->request('put', $uri, $options);
181
    }
182
183
    /**
184
     * @param  string  $name
185
     * @return bool
186
     * @throws ArangoException
187
     * @throws GuzzleException
188
     */
189 3
    public function deleteCollection(string $name): bool
190
    {
191 3
        $uri = '/_api/collection/' . $name;
192
193 3
        return (bool) $this->connector->request('delete', $uri);
194
    }
195
}
196