Passed
Push — next ( 86cbfb...d68df8 )
by Bas
03:10
created

ManagesIndexes::getIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 1
rs 10
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/indexes.html
11
 */
12
trait ManagesIndexes
13
{
14
    protected Connector $connector;
15
16
    /**
17
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-all-indexes-of-a-collection
18
     *
19
     * @param  string  $collection
20
     * @return array<mixed>
21
     * @throws ArangoException
22
     * @throws GuzzleException
23
     */
24 5
    public function getIndexes(string $collection): array
25
    {
26
        $options = [
27
            'query' => [
28 5
                'collection' => $collection
29
            ]
30
        ];
31 5
        $results = (array) $this->connector->request('get', '/_api/index', $options);
32 5
        return (array) $results['indexes'];
33
    }
34
35
    /**
36
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index
37
     *
38
     * @param  string  $id
39
     * @return array<mixed>
40
     * @throws ArangoException
41
     * @throws GuzzleException
42
     */
43 1
    public function getIndex(string $id): array
44
    {
45 1
        $uri = '/_api/index/' . $id;
46 1
        return (array) $this->connector->request('get', $uri);
47
    }
48
49
    /**
50
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index
51
     *
52
     * @param  string  $collection
53
     * @param  string  $name
54
     * @return array<mixed>|bool
55
     * @throws ArangoException
56
     * @throws GuzzleException
57
     */
58 3
    public function getIndexByName(string $collection, string $name)
59
    {
60 3
        $indexes = $this->getIndexes($collection);
61 3
        $searchResult = array_search($name, array_column($indexes, 'name'));
62 3
        if (is_integer($searchResult)) {
63 3
            return (array) $indexes[$searchResult];
64
        }
65 1
        return (bool) $searchResult;
66
    }
67
68
    /**
69
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#create-index
70
     *
71
     * @param  string  $collection
72
     * @param  array<mixed>  $index
73
     * @return bool
74
     * @throws ArangoException
75
     * @throws GuzzleException
76
     */
77 2
    public function createIndex(string $collection, array $index): bool
78
    {
79 2
        $indexType = 'persistent';
80
81 2
        if (isset($index['type'])) {
82 2
            $indexType = (string) $index['type'];
83
        }
84 2
        $uri = '/_api/index#' . $indexType;
85
86 2
        $index = json_encode((object) $index);
87 2
        $options = ['body' => $index];
88 2
        $options['query']['collection'] = $collection;
89
90 2
        return (bool) $this->connector->request('post', $uri, $options);
91
    }
92
93
    /**
94
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#delete-index
95
     *
96
     * @param  string  $id
97
     * @return bool
98
     * @throws ArangoException
99
     * @throws GuzzleException
100
     */
101 1
    public function deleteIndex(string $id): bool
102
    {
103 1
        $uri = '/_api/index/' . $id;
104
105 1
        return (bool) $this->connector->request('delete', $uri);
106
    }
107
}
108