Passed
Push — next ( 4ffd75...963f07 )
by Bas
04:17 queued 02:04
created

ManagesGraphs   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 222
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 222
ccs 60
cts 60
cp 1
rs 10
wmc 14

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getGraph() 0 5 1
A hasGraph() 0 5 1
A deleteGraph() 0 5 1
A createGraph() 0 13 1
A getGraphs() 0 8 1
A getGraphEdges() 0 5 1
A addGraphVertex() 0 11 1
A getGraphVertices() 0 5 1
A addGraphEdge() 0 9 1
A replaceGraphEdge() 0 15 1
A removeGraphVertex() 0 8 1
A deleteAllGraphs() 0 9 2
A removeGraphEdge() 0 13 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
use stdClass;
10
11
/**
12
 * @see https://www.arangodb.com/docs/stable/http/gharial-management.html
13
 */
14
trait ManagesGraphs
15
{
16
    protected ArangoClient $arangoClient;
17
18
    /**
19
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#create-a-graph
20
     *
21
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
22
     *
23
     * @param  array<mixed>  $config
24
     *
25
     * @throws ArangoException
26
     */
27 12
    public function createGraph(
28
        string $name,
29
        array $config = [],
30
        bool $waitForSync = false,
31
    ): stdClass {
32 12
        $options = [];
33 12
        $options['query']['waitForSync'] = (int) $waitForSync;
34 12
        $options['body'] = $config;
35 12
        $options['body']['name'] = $name;
36
37 12
        $result = $this->arangoClient->request('post', '/_api/gharial', $options);
38
39 12
        return (object) $result->graph;
40
    }
41
42
    /**
43
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#list-all-graphs
44
     *
45
     * @return array<mixed>
46
     *
47
     * @throws ArangoException
48
     */
49 11
    public function getGraphs(): array
50
    {
51 11
        $results = $this->arangoClient->request(
52 11
            'get',
53 11
            '/_api/gharial',
54 11
        );
55
56 11
        return (array) $results->graphs;
57
    }
58
59
    /**
60
     * Check for graph existence in current DB.
61
     *
62
     * @throws ArangoException
63
     */
64 10
    public function hasGraph(string $name): bool
65
    {
66 10
        $results = $this->getGraphs();
67
68 10
        return in_array($name, array_column($results, '_key'), true);
69
    }
70
71
    /**
72
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#get-a-graph
73
     *
74
     * @throws ArangoException
75
     */
76 1
    public function getGraph(string $name): stdClass
77
    {
78 1
        $uri = '/_api/gharial/' . $name;
79
80 1
        return (object) $this->arangoClient->request('get', $uri)->graph;
81
    }
82
83
    /**
84
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#drop-a-graph
85
     *
86
     * @throws ArangoException
87
     */
88 12
    public function deleteGraph(string $name): bool
89
    {
90 12
        $uri = '/_api/gharial/' . $name;
91
92 12
        return (bool) $this->arangoClient->request('delete', $uri);
93
    }
94
95
    /**
96
     * @throws ArangoException
97
     */
98
    public function deleteAllGraphs(): bool
99
    {
100
        $graphs = $this->getGraphs();
101
102 1
        foreach ($graphs as $graph) {
103
            $this->deleteGraph($graph->name);
104 1
        }
105
106 1
        return true;
107
    }
108
109
    /**
110
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#list-vertex-collections
111
     *
112
     * @return array<mixed>
113
     *
114 1
     * @throws ArangoException
115
     */
116 1
    public function getGraphVertices(string $name): array
117
    {
118 1
        $uri = '/_api/gharial/' . $name . '/vertex';
119 1
120 1
        return (array) $this->arangoClient->request('get', $uri)->collections;
121 1
    }
122 1
123
    /**
124 1
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#add-vertex-collection
125
     *
126
     * @throws ArangoException
127
     */
128
    public function addGraphVertex(string $name, string $vertex): stdClass
129
    {
130
        $uri = '/_api/gharial/' . $name . '/vertex';
131
132
        $options = [
133
            'body' => [
134 1
                'collection' => $vertex,
135
            ],
136 1
        ];
137
138 1
        return (object) $this->arangoClient->request('post', $uri, $options)->graph;
139 1
    }
140
141 1
    /**
142
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#add-vertex-collection
143
     *
144
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
145
     *
146
     * @throws ArangoException
147
     */
148
    public function removeGraphVertex(string $name, string $vertex, bool $dropCollection = false): stdClass
149
    {
150
        $uri = '/_api/gharial/' . $name . '/vertex/' . $vertex;
151 1
152
        $options = [];
153 1
        $options['query']['dropCollection'] = $dropCollection;
154
155 1
        return (object) $this->arangoClient->request('delete', $uri, $options)->graph;
156
    }
157
158
    /**
159
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#list-edge-definitions
160
     *
161
     * @return array<mixed>
162
     *
163
     * @throws ArangoException
164
     */
165 1
    public function getGraphEdges(string $name): array
166
    {
167 1
        $uri = '/_api/gharial/' . $name . '/edge';
168
169 1
        return (array) $this->arangoClient->request('get', $uri)->collections;
170 1
    }
171 1
172
    /**
173 1
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#add-edge-definition
174
     *
175
     * @param  array<mixed>  $edgeDefinition
176
     *
177
     * @throws ArangoException
178
     */
179
    public function addGraphEdge(string $name, array $edgeDefinition): stdClass
180
    {
181
        $uri = '/_api/gharial/' . $name . '/edge';
182
183
        $options = [
184
            'body' => $edgeDefinition,
185 1
        ];
186
187
        return (object) $this->arangoClient->request('post', $uri, $options)->graph;
188
    }
189
190
    /**
191
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#replace-an-edge-definition
192 1
     *
193
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
194 1
     *
195 1
     * @param  array<mixed>  $edgeDefinition
196 1
     *
197 1
     * @throws ArangoException
198
     */
199 1
    public function replaceGraphEdge(
200
        string $name,
201
        string $edge,
202
        array $edgeDefinition,
203
        bool $dropCollection = false,
204
        bool $waitForSync = false,
205
    ): stdClass {
206
        $uri = '/_api/gharial/' . $name . '/edge/' . $edge . '#definition';
207
208
        $options = [];
209 1
        $options['query']['waitForSync'] = $waitForSync;
210
        $options['query']['dropCollection'] = $dropCollection;
211
        $options['body'] = $edgeDefinition;
212
213
        return (object) $this->arangoClient->request('put', $uri, $options)->graph;
214
    }
215 1
216
    /**
217 1
     * @see https://www.arangodb.com/docs/stable/http/gharial-management.html#remove-an-edge-definition-from-the-graph
218 1
     *
219 1
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
220
     *
221 1
     * @throws ArangoException
222
     */
223
    public function removeGraphEdge(
224
        string $name,
225
        string $edge,
226
        bool $dropCollection = true,
227
        bool $waitForSync = false,
228
    ): stdClass {
229
        $uri = '/_api/gharial/' . $name . '/edge/' . $edge . '#definition';
230
231
        $options = [];
232
        $options['query']['waitForSync'] = $waitForSync;
233
        $options['query']['dropCollection'] = $dropCollection;
234
235
        return (object) $this->arangoClient->request('delete', $uri, $options)->graph;
236
    }
237
}
238