Passed
Push — feature/php8.3-support ( 30b766...e94ce8 )
by
unknown
20:35 queued 13:27
created

ManagesAnalyzers   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 104
ccs 28
cts 29
cp 0.9655
rs 10
c 1
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAnalyzer() 0 5 1
A getAnalyzer() 0 5 1
A replaceAnalyzer() 0 11 2
A getFullName() 0 6 2
A hasAnalyzer() 0 7 1
A getAnalyzers() 0 5 1
A createAnalyzer() 0 9 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
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#list-all-analyzers
58
     *
59
     * @return array<mixed>
60
     *
61
     * @throws ArangoException
62
     */
63 7
    public function getAnalyzers(): array
64
    {
65 7
        $results = $this->arangoClient->request('get', '/_api/analyzer');
66
67 7
        return (array) $results->result;
68
    }
69
70
    /**
71
     * Check for analyzer existence
72
     *
73
     *
74
     * @throws ArangoException
75
     */
76 7
    public function hasAnalyzer(string $name): bool
77
    {
78 7
        $name = $this->getFullName($name);
79
80 7
        $analyzers = $this->getAnalyzers();
81
82 7
        return in_array($name, array_column($analyzers, 'name'), true);
83
    }
84
85
    /**
86
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#get-an-analyzer-definition
87
     *
88
     * @throws ArangoException
89
     */
90 2
    public function getAnalyzer(string $name): stdClass
91
    {
92 2
        $uri = '/_api/analyzer/' . $name;
93
94 2
        return $this->arangoClient->request('get', $uri);
95
    }
96
97
    /**
98
     * Replace an existing analyzer. Note that this is just a shorthand for delete(old)/create(new).
99
     *
100
     * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#create-an-analyzer
101
     *
102
     * @param string $name
103
     * @param array<mixed> $newAnalyzer
104
     * @return stdClass|false
105
     * @throws ArangoException
106
     */
107 1
    public function replaceAnalyzer(string $name, array $newAnalyzer): stdClass|false
108
    {
109 1
        if (!$this->hasAnalyzer($name)) {
110
            return false;
111
        }
112 1
        $this->deleteAnalyzer($name);
113
114
        // Enforce the analyzer name
115 1
        $newAnalyzer['name'] = $name;
116
117 1
        return $this->createAnalyzer($newAnalyzer);
118
    }
119
}
120