Passed
Push — next ( 86cbfb...d68df8 )
by Bas
03:10
created

ManagesViews::replaceView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

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 5
cts 6
cp 0.8333
crap 2.0185
rs 10
1
<?php
2
3
namespace ArangoClient\Schema;
4
5
use ArangoClient\Connector;
6
use ArangoClient\Exceptions\ArangoException;
7
use GuzzleHttp\Exception\GuzzleException;
8
9
/*
10
 * @see https://www.arangodb.com/docs/stable/http/views.html
11
 */
12
trait ManagesViews
13
{
14
    protected Connector $connector;
15
16
    /**
17
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#create-an-arangosearch-view
18
     *
19
     * @param  array<mixed>  $view
20
     * @return array<mixed>
21
     * @throws ArangoException
22
     * @throws GuzzleException
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 (array) $this->connector->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
     * @throws GuzzleException
41
     */
42 8
    public function deleteView(string $name): bool
43
    {
44 8
        $uri = '/_api/view/' . $name;
45
46 8
        return (bool) $this->connector->request('delete', $uri);
47
    }
48
49
    /**
50
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#list-all-views
51
     *
52
     * @return array<mixed>
53
     * @throws ArangoException
54
     * @throws GuzzleException
55
     */
56 8
    public function getViews(): array
57
    {
58 8
        return (array) $this->connector->request('get', '/_api/view');
59
    }
60
61
    /**
62
     * Check for view existence
63
     *
64
     * @param  string  $name
65
     * @return bool
66
     * @throws ArangoException
67
     * @throws GuzzleException
68
     */
69 8
    public function hasView(string $name): bool
70
    {
71 8
        $views = $this->getViews();
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
     * @throws GuzzleException
82
     */
83 1
    public function getView(string $name): array
84
    {
85 1
        return $this->getViewProperties($name);
86
    }
87
88
    /**
89
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#read-properties-of-a-view
90
     *
91
     * @param  string  $name
92
     * @return array<mixed>
93
     * @throws ArangoException
94
     * @throws GuzzleException
95
     */
96 2
    public function getViewProperties(string $name): array
97
    {
98 2
        $uri = '/_api/view/' . $name . '/properties';
99 2
        return (array) $this->connector->request('get', $uri);
100
    }
101
102
    /**
103
     * @param  string  $old
104
     * @param  string  $new
105
     * @return array<mixed>
106
     * @throws ArangoException
107
     * @throws GuzzleException
108
     */
109 1
    public function renameView(string $old, string $new): array
110
    {
111 1
        $uri = '/_api/view/' . $old . '/rename';
112
113 1
        $body = json_encode((object) ['name' => $new]);
114 1
        $options = ['body' => $body];
115
116 1
        return (array) $this->connector->request('put', $uri, $options);
117
    }
118
119
    /**
120
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#partially-changes-properties-of-an-arangosearch-view
121
     *
122
     * @param  string  $name
123
     * @param  array<mixed>  $properties
124
     * @return array<mixed>
125
     * @throws ArangoException
126
     * @throws GuzzleException
127
     */
128 1
    public function updateView(string $name, array $properties): array
129
    {
130
        // PrimarySort & primarySortCompression are immutable and will throw if we try to change it.
131
        // Use replaceView if you want to update these properties.
132 1
        if (isset($properties['primarySort'])) {
133 1
            unset($properties['primarySort']);
134
        }
135 1
        if (isset($properties['primarySortCompression'])) {
136
            unset($properties['primarySortCompression']);
137
        }
138
139 1
        $properties['type'] = isset($properties['type']) ? (string) $properties['type'] : 'arangosearch';
140
141 1
        $uri = '/_api/view/' . $name . '/properties#' . $properties['type'];
142 1
        $body = json_encode((object) $properties);
143 1
        $options = ['body' => $body];
144
145 1
        return (array) $this->connector->request('patch', $uri, $options);
146
    }
147
148
    /**
149
     * Replace an existing view. Use this to change immutable fields like primarySort. Note that
150
     * this is just a shorthand for delete(old)/create(new). ArangoDB will have to rebuild the view data.
151
     *
152
     * @see https://www.arangodb.com/docs/stable/http/views-arangosearch.html#create-an-arangosearch-view
153
     *
154
     * @param string $name
155
     * @param array<mixed> $newView
156
     * @return false|array<mixed>
157
     * @throws ArangoException
158
     * @throws GuzzleException
159
     */
160 1
    public function replaceView(string $name, array $newView)
161
    {
162 1
        if (! $this->hasView($name)) {
163
            return false;
164
        }
165 1
        $this->deleteView($name);
166
167
        // Enforce the view name
168 1
        $newView['name'] = $name;
169
170 1
        return $this->createView($newView);
171
    }
172
}
173