ManagesAnalyzers::replaceAnalyzer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 6
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
use stdClass;
10
11
/*
12
 * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/
13
 */
14
trait ManagesAnalyzers
15
{
16
    protected ArangoClient $arangoClient;
17
18 7
    protected function getFullName(string $name): string
19
    {
20 7
        if (!str_contains($name, '::')) {
21 7
            $name = $this->arangoClient->getDatabase() . '::' . $name;
22
        }
23 7
        return $name;
24
    }
25
26
    /**
27
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#create-an-analyzer
28
     *
29
     * @param array<mixed> $analyzer
30
     * @return stdClass
31
     * @throws ArangoException
32
     */
33 7
    public function createAnalyzer(array $analyzer): stdClass
34
    {
35 7
        $uri = '/_api/analyzer';
36
37 7
        $options = [
38 7
            'body' => $analyzer,
39 7
        ];
40
41 7
        return $this->arangoClient->request('post', $uri, $options);
42
    }
43
44
    /**
45
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#remove-an-analyzer
46
     *
47
     * @throws ArangoException
48
     */
49 7
    public function deleteAnalyzer(string $name): bool
50
    {
51 7
        $uri = '/_api/analyzer/' . $name;
52
53 7
        return (bool) $this->arangoClient->request('delete', $uri);
54
    }
55
56
    /**
57
     * Removes all custom analyzes defined for the current database.
58
     *
59
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#remove-an-analyzer
60
     *
61
     * @throws ArangoException
62
     */
63 7
    public function deleteAllAnalyzers(): bool
64
    {
65 7
        $analyzers = $this->getAnalyzers();
66
67 7
        $database = $this->arangoClient->getDatabase();
68
69
        foreach ($analyzers as $analyzer) {
70
            if (!str_starts_with($analyzer->name, "$database::")) {
71
                continue;
72
            }
73
74
            $this->deleteAnalyzer($analyzer->name);
75
        }
76 7
77
        return true;
78 7
    }
79
80 7
81
82 7
    /**
83
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#list-all-analyzers
84
     *
85
     * @return array<mixed>
86
     *
87
     * @throws ArangoException
88
     */
89
    public function getAnalyzers(): array
90 2
    {
91
        $results = $this->arangoClient->request('get', '/_api/analyzer');
92 2
93
        return (array) $results->result;
94 2
    }
95
96
    /**
97
     * Check for analyzer existence
98
     *
99
     *
100
     * @throws ArangoException
101
     */
102
    public function hasAnalyzer(string $name): bool
103
    {
104
        $name = $this->getFullName($name);
105
106
        $analyzers = $this->getAnalyzers();
107 1
108
        return in_array($name, array_column($analyzers, 'name'), true);
109 1
    }
110
111
    /**
112 1
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#get-an-analyzer-definition
113
     *
114
     * @throws ArangoException
115 1
     */
116
    public function getAnalyzer(string $name): stdClass
117 1
    {
118
        $uri = '/_api/analyzer/' . $name;
119
120
        return $this->arangoClient->request('get', $uri);
121
    }
122
123
    /**
124
     * Replace an existing analyzer. Note that this is just a shorthand for delete(old)/create(new).
125
     *
126
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#create-an-analyzer
127
     *
128
     * @param string $name
129
     * @param array<mixed> $newAnalyzer
130
     * @return stdClass|false
131
     * @throws ArangoException
132
     */
133
    public function replaceAnalyzer(string $name, array $newAnalyzer): stdClass|false
134
    {
135
        if (!$this->hasAnalyzer($name)) {
136
            return false;
137
        }
138
        $this->deleteAnalyzer($name);
139
140
        // Enforce the analyzer name
141
        $newAnalyzer['name'] = $name;
142
143
        return $this->createAnalyzer($newAnalyzer);
144
    }
145
}
146