Issues (24)

src/Schema/ManagesDatabases.php (2 issues)

1
<?php
2
3
namespace ArangoClient\Schema;
4
5
use ArangoClient\ArangoClient;
6
use ArangoClient\Exceptions\ArangoException;
7
use stdClass;
8
9
/*
10
 * @see https://www.arangodb.com/docs/stable/http/database.html
11
 */
12
trait ManagesDatabases
13
{
14
    protected ArangoClient $arangoClient;
15
16
    /**
17
     * @throws ArangoException
18
     */
19 2
    public function getCurrentDatabase(): stdClass
20
    {
21 2
        $results = $this->arangoClient->request('get', '/_api/database/current');
22
23 2
        return (object) $results->result;
24
    }
25
26
    /**
27
     * @return array<mixed>
28
     *
29
     * @throws ArangoException
30
     */
31 117
    public function getDatabases(): array
32
    {
33 117
        $user = $this->arangoClient->getUser();
34
35 117
        $uri = '/_api/user/' . $user . '/database';
36
37 117
        $results = $this->arangoClient->request('get', $uri, []);
38
39 117
        return array_keys((array) $results->result);
40
    }
41
42
    /**
43
     * @throws ArangoException
44
     */
45 117
    public function hasDatabase(string $name): bool
46
    {
47 117
        $databaseList = $this->getDatabases();
48
49 117
        return in_array($name, $databaseList);
50
    }
51
52
    /**
53
     * @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...
54
     * @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...
55
     *
56
     * @throws ArangoException
57
     */
58 6
    public function createDatabase(string $name, $options = null, $users = null): bool
59
    {
60 6
        $body = ['name' => $name, 'options' => $options, 'users' => $users];
61
62 6
        $options = [
63 6
            'body' => $body,
64 6
        ];
65
66 6
        return (bool) $this->arangoClient->request('post', '/_api/database', $options, '_system');
67
    }
68
69
    /**
70
     * @throws ArangoException
71
     */
72 4
    public function deleteDatabase(string $name): bool
73
    {
74 4
        $uri = '/_api/database/' . $name;
75
76 4
        return (bool) $this->arangoClient->request('delete', $uri, [], '_system');
77
    }
78
}
79