Failed Conditions
Push — feature/add-delete-all-collect... ( 76c9d5 )
by Bas
02:49
created

ManagesAnalyzers::deleteAllAnalyzers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 17
rs 10
c 0
b 0
f 0
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
    protected function getFullName(string $name): string
19
    {
20
        if (!str_contains($name, '::')) {
21
            $name = $this->arangoClient->getDatabase() . '::' . $name;
22
        }
23
        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
    public function createAnalyzer(array $analyzer): stdClass
34
    {
35
        $uri = '/_api/analyzer';
36
37
        $options = [
38
            'body' => $analyzer,
39
        ];
40
41
        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
    public function deleteAnalyzer(string $name): bool
50
    {
51
        $uri = '/_api/analyzer/' . $name;
52
53
        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
    public function deleteAllAnalyzers(): bool
64
    {
65
        $analyzers = $this->getAnalyzers();
66
67
        $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
            $this->arangoClient->request('delete', $uri);
77
        }
78
79
        return true;
80
    }
81
82
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
     */
91
    public function getAnalyzers(): array
92
    {
93
        $results = $this->arangoClient->request('get', '/_api/analyzer');
94
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
108
        $analyzers = $this->getAnalyzers();
109
110
        return in_array($name, array_column($analyzers, 'name'), true);
111
    }
112
113
    /**
114
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#get-an-analyzer-definition
115
     *
116
     * @throws ArangoException
117
     */
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