Passed
Push — next ( 09b52e...659675 )
by Bas
13:30 queued 11:38
created

ManagesViews::getViews()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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