ManagesIndexes   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 84
ccs 28
cts 28
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 0 5 1
A createIndex() 0 13 2
A deleteIndex() 0 5 1
A getIndexByName() 0 10 2
A getIndexes() 0 10 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
 * Manage collection indexes
13
 *
14
 * @see https://www.arangodb.com/docs/stable/http/indexes.html
15
 */
16
trait ManagesIndexes
17
{
18
    protected ArangoClient $arangoClient;
19
20
    /**
21
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-all-indexes-of-a-collection
22
     *
23
     * @return array<mixed>
24
     *
25
     * @throws ArangoException
26
     */
27 5
    public function getIndexes(string $collection): array
28
    {
29 5
        $options = [
30 5
            'query' => [
31 5
                'collection' => $collection,
32 5
            ],
33 5
        ];
34 5
        $results = $this->arangoClient->request('get', '/_api/index', $options);
35
36 5
        return (array) $results->indexes;
37
    }
38
39
    /**
40
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index
41
     *
42
     * @throws ArangoException
43
     */
44 1
    public function getIndex(string $id): stdClass
45
    {
46 1
        $uri = '/_api/index/' . $id;
47
48 1
        return $this->arangoClient->request('get', $uri);
49
    }
50
51
    /**
52
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index
53
     *
54
     * @throws ArangoException
55
     */
56 3
    public function getIndexByName(string $collection, string $name): stdClass|false
57
    {
58 3
        $indexes = $this->getIndexes($collection);
59 3
        $searchResult = array_search($name, array_column($indexes, 'name'));
60
61 3
        if (is_int($searchResult)) {
62 3
            return (object) $indexes[$searchResult];
63
        }
64
65 1
        return false;
66
    }
67
68
    /**
69
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#create-index
70
     *
71
     * @param  array<mixed>  $index
72
     *
73
     * @throws ArangoException
74
     */
75 2
    public function createIndex(string $collection, array $index): stdClass
76
    {
77 2
        $indexType = 'persistent';
78
79 2
        if (isset($index['type'])) {
80 2
            $indexType = (string) $index['type'];
81
        }
82 2
        $uri = '/_api/index#' . $indexType;
83
84 2
        $options = ['body' => $index];
85 2
        $options['query']['collection'] = $collection;
86
87 2
        return $this->arangoClient->request('post', $uri, $options);
88
    }
89
90
    /**
91
     * @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#delete-index
92
     *
93
     * @throws ArangoException
94
     */
95 1
    public function deleteIndex(string $id): stdClass
96
    {
97 1
        $uri = '/_api/index/' . $id;
98
99 1
        return $this->arangoClient->request('delete', $uri);
100
    }
101
}
102