Passed
Push — next ( c9a2a9...fad6bc )
by Bas
02:30
created

DatabaseClient::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArangoClient;
6
7
use GuzzleHttp\Exception\GuzzleException;
8
9
class DatabaseClient
10
{
11
    /**
12
     * @var Connector
13
     */
14
    protected Connector $connector;
15
16
    /**
17
     * Documents constructor.
18
     * @param  Connector  $connector
19
     */
20 8
    public function __construct(Connector $connector)
21
    {
22 8
        $this->connector = $connector;
23 8
    }
24
25
    /**
26
     * @return array<mixed>
27
     * @throws GuzzleException|Exceptions\ArangoDbException
28
     */
29 1
    public function read(): array
30
    {
31 1
        return (array) $this->connector->request('get', '/_api/database/current');
32
    }
33
34
    /**
35
     * @return array<mixed>
36
     *
37
     * @throws GuzzleException|Exceptions\ArangoDbException
38
     */
39 8
    public function listDatabases(): array
40
    {
41 8
        return (array) $this->connector->request('get', '/_api/database');
42
    }
43
44
    /**
45
     * @param $database
46
     * @return bool
47
     * @throws Exceptions\ArangoDbException
48
     * @throws GuzzleException
49
     */
50 8
    public function exists($database): bool
51
    {
52 8
        $databaseList = $this->listDatabases();
53 8
        return in_array($database, $databaseList);
54
    }
55
56
    /**
57
     * @return array<mixed>
58
     *
59
     * @throws GuzzleException|Exceptions\ArangoDbException
60
     */
61 1
    public function listMyDatabases(): array
62
    {
63 1
        return (array) $this->connector->request('get', '/_api/database/user');
64
    }
65
66
    /**
67
     * @param  string  $name
68
     * @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...
69
     * @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...
70
     * @return bool
71
     * @throws Exceptions\ArangoDbException
72
     * @throws GuzzleException
73
     */
74 3
    public function create(string $name, $options = null, $users = null): bool
75
    {
76 3
        $database = json_encode((object)['name' => $name, 'options' => $options, 'users' => $users]);
77
78 3
        return (bool) $this->connector->request('post', '/_api/database', ['body' => $database]);
79
    }
80
81
    /**
82
     * @param  string  $name
83
     * @return bool
84
     * @throws Exceptions\ArangoDbException
85
     * @throws GuzzleException
86
     */
87 1
    public function delete(string $name): bool
88
    {
89 1
        $uri = '/_api/database/' . $name;
90
91 1
        return (bool) $this->connector->request('delete', $uri);
92
    }
93
}
94