Passed
Push — next ( fad6bc...2b9454 )
by Bas
02:29
created

ManagesDatabases::listDatabases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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/database.html
11
 */
12
trait ManagesDatabases
13
{
14
    protected Connector $connector;
15
16
    /**
17
     * @return array<mixed>
18
     * @throws ArangoException
19
     * @throws GuzzleException
20
     */
21 1
    public function getCurrentDatabase(): array
22
    {
23 1
        return (array) $this->connector->request('get', '/_api/database/current');
24
    }
25
26
    /**
27
     * @return array<mixed>
28
     *
29
     * @throws GuzzleException|ArangoException
30
     */
31 21
    public function listDatabases(): array
32
    {
33 21
        return (array) $this->connector->request('get', '/_api/database');
34
    }
35
36
    /**
37
     * @param string    $database
38
     * @return bool
39
     * @throws ArangoException
40
     * @throws GuzzleException
41
     */
42 21
    public function hasDatabase(string $database): bool
43
    {
44 21
        $databaseList = $this->listDatabases();
45 21
        return in_array($database, $databaseList);
46
    }
47
48
    /**
49
     * @return array<mixed>
50
     *
51
     * @throws GuzzleException|ArangoException
52
     */
53 1
    public function listMyDatabases(): array
54
    {
55 1
        return (array) $this->connector->request('get', '/_api/database/user');
56
    }
57
58
    /**
59
     * @param  string  $name
60
     * @param  null  $options
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $options is correct as it would always require null to be passed?
Loading history...
61
     * @param  null  $users
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $users is correct as it would always require null to be passed?
Loading history...
62
     * @return bool
63
     * @throws ArangoException
64
     * @throws GuzzleException
65
     */
66 3
    public function createDatabase(string $name, $options = null, $users = null): bool
67
    {
68 3
        $database = json_encode((object)['name' => $name, 'options' => $options, 'users' => $users]);
69
70 3
        return (bool) $this->connector->request('post', '/_api/database', ['body' => $database]);
71
    }
72
73
    /**
74
     * @param  string  $name
75
     * @return bool
76
     * @throws ArangoException
77
     * @throws GuzzleException
78
     */
79 1
    public function deleteDatabase(string $name): bool
80
    {
81 1
        $uri = '/_api/database/' . $name;
82
83 1
        return (bool) $this->connector->request('delete', $uri);
84
    }
85
}
86