Failed Conditions
Push — next ( 1e4ade...f47ec0 )
by Bas
04:16 queued 11s
created

ManagesViews::updateView()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArangoClient\Schema;
6
7
use ArangoClient\ArangoClient;
8
use ArangoClient\Exceptions\ArangoException;
9
10
/*
11
 * @see https://www.arangodb.com/docs/stable/http/views.html
12
 */
13
trait ManagesViews
14
{
15
    protected ArangoClient $arangoClient;
16
17
    /**
18
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#create-an-arangosearch-view
19
     *
20
     * @param  array<mixed>  $view
21
     * @return array<mixed>
22
     * @throws ArangoException
23
     */
24 8
    public function createView(array $view): array
25
    {
26 8
        $view['type'] = isset($view['type']) ? (string) $view['type'] : 'arangosearch';
27
28 8
        $uri = '/_api/view#' . $view['type'];
29 8
        $body = json_encode((object) $view);
30
31 8
        return  $this->arangoClient->request('post', $uri, ['body' => $body]);
32
    }
33
34
    /**
35
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#drops-a-view
36
     *
37
     * @param  string  $name
38
     * @return bool
39
     * @throws ArangoException
40
     */
41 8
    public function deleteView(string $name): bool
42
    {
43 8
        $uri = '/_api/view/' . $name;
44
45 8
        return (bool) $this->arangoClient->request('delete', $uri);
46
    }
47
48
    /**
49
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#list-all-views
50
     *
51
     * @return array<mixed>
52
     * @throws ArangoException
53
     */
54 8
    public function getViews(): array
55
    {
56 8
        $results = $this->arangoClient->request('get', '/_api/view');
57
58 8
        return (array) $results['result'];
59
    }
60
61
    /**
62
     * Check for view existence
63
     *
64
     * @param  string  $name
65
     * @return bool
66
     * @throws ArangoException
67
     */
68 8
    public function hasView(string $name): bool
69
    {
70 8
        $views = $this->getViews();
71
72 8
        return array_search($name, array_column($views, 'name'), true) !== false;
73
    }
74
75
    /**
76
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#return-information-about-a-view
77
     *
78
     * @param  string  $name
79
     * @return array<mixed>
80
     * @throws ArangoException
81
     */
82 1
    public function getView(string $name): array
83
    {
84 1
        return $this->getViewProperties($name);
85
    }
86
87
    /**
88
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#read-properties-of-a-view
89
     *
90
     * @param  string  $name
91
     * @return array<mixed>
92
     * @throws ArangoException
93
     */
94 2
    public function getViewProperties(string $name): array
95
    {
96 2
        $uri = '/_api/view/' . $name . '/properties';
97
98 2
        return $this->arangoClient->request('get', $uri);
99
    }
100
101
    /**
102
     * @param  string  $old
103
     * @param  string  $new
104
     * @return array<mixed>
105
     * @throws ArangoException
106
     */
107 1
    public function renameView(string $old, string $new): array
108
    {
109 1
        $uri = '/_api/view/' . $old . '/rename';
110
111 1
        $body = json_encode((object) ['name' => $new]);
112 1
        $options = ['body' => $body];
113
114 1
        return $this->arangoClient->request('put', $uri, $options);
115
    }
116
117
    /**
118
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#partially-changes-properties-of-an-arangosearch-view
119
     *
120
     * @param  string  $name
121
     * @param  array<mixed>  $properties
122
     * @return array<mixed>
123
     * @throws ArangoException
124
     */
125 1
    public function updateView(string $name, array $properties): array
126
    {
127
        // PrimarySort & primarySortCompression are immutable and will throw if we try to change it.
128
        // Use replaceView if you want to update these properties.
129 1
        if (isset($properties['primarySort'])) {
130 1
            unset($properties['primarySort']);
131
        }
132 1
        if (isset($properties['primarySortCompression'])) {
133
            unset($properties['primarySortCompression']);
134
        }
135
136 1
        $properties['type'] = isset($properties['type']) ? (string) $properties['type'] : 'arangosearch';
137
138 1
        $uri = '/_api/view/' . $name . '/properties#' . $properties['type'];
139 1
        $body = json_encode((object) $properties);
140 1
        $options = ['body' => $body];
141
142 1
        return $this->arangoClient->request('patch', $uri, $options);
143
    }
144
145
    /**
146
     * Replace an existing view. Use this to change immutable fields like primarySort. Note that
147
     * this is just a shorthand for delete(old)/create(new). ArangoDB will have to rebuild the view data.
148
     *
149
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#create-an-arangosearch-view
150
     *
151
     * @param string $name
152
     * @param array<mixed> $newView
153
     * @return false|array<mixed>
154
     * @throws ArangoException
155
     */
156 1
    public function replaceView(string $name, array $newView)
157
    {
158 1
        if (! $this->hasView($name)) {
159
            return false;
160
        }
161 1
        $this->deleteView($name);
162
163
        // Enforce the view name
164 1
        $newView['name'] = $name;
165
166 1
        return $this->createView($newView);
167
    }
168
}
169