Passed
Push — next ( 0eb390...76c9d5 )
by Bas
04:03 queued 01:53
created

ManagesAnalyzers   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 132
ccs 28
cts 29
cp 0.9655
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAnalyzer() 0 5 1
A getFullName() 0 6 2
A createAnalyzer() 0 9 1
A getAnalyzer() 0 5 1
A replaceAnalyzer() 0 11 2
A deleteAllAnalyzers() 0 17 3
A hasAnalyzer() 0 7 1
A getAnalyzers() 0 5 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://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
            $uri = '/_api/analyzer/' . $analyzer->name;
75
76 7
            $this->arangoClient->request('delete', $uri);
77
        }
78 7
79
        return true;
80 7
    }
81
82 7
83
84
    /**
85
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#list-all-analyzers
86
     *
87
     * @return array<mixed>
88
     *
89
     * @throws ArangoException
90 2
     */
91
    public function getAnalyzers(): array
92 2
    {
93
        $results = $this->arangoClient->request('get', '/_api/analyzer');
94 2
95
        return (array) $results->result;
96
    }
97
98
    /**
99
     * Check for analyzer existence
100
     *
101
     *
102
     * @throws ArangoException
103
     */
104
    public function hasAnalyzer(string $name): bool
105
    {
106
        $name = $this->getFullName($name);
107 1
108
        $analyzers = $this->getAnalyzers();
109 1
110
        return in_array($name, array_column($analyzers, 'name'), true);
111
    }
112 1
113
    /**
114
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#get-an-analyzer-definition
115 1
     *
116
     * @throws ArangoException
117 1
     */
118
    public function getAnalyzer(string $name): stdClass
119
    {
120
        $uri = '/_api/analyzer/' . $name;
121
122
        return $this->arangoClient->request('get', $uri);
123
    }
124
125
    /**
126
     * Replace an existing analyzer. Note that this is just a shorthand for delete(old)/create(new).
127
     *
128
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#create-an-analyzer
129
     *
130
     * @param string $name
131
     * @param array<mixed> $newAnalyzer
132
     * @return stdClass|false
133
     * @throws ArangoException
134
     */
135
    public function replaceAnalyzer(string $name, array $newAnalyzer): stdClass|false
136
    {
137
        if (!$this->hasAnalyzer($name)) {
138
            return false;
139
        }
140
        $this->deleteAnalyzer($name);
141
142
        // Enforce the analyzer name
143
        $newAnalyzer['name'] = $name;
144
145
        return $this->createAnalyzer($newAnalyzer);
146
    }
147
}
148