Passed
Pull Request — master (#7)
by Sandro
02:02
created

Collection::count()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2020 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types=1);
11
12
namespace ArangoDb\Handler;
13
14
use ArangoDb\Exception\GuardErrorException;
15
use ArangoDb\Exception\UnexpectedResponse;
16
use ArangoDb\Guard\SuccessHttpStatusCode;
17
use ArangoDb\Http\TypeSupport;
18
use ArangoDb\Type\Collection as CollectionType;
19
use ArangoDb\Util\Json;
20
use Psr\Http\Message\ResponseInterface;
21
22
final class Collection implements CollectionHandler
23
{
24
    /**
25
     * @var TypeSupport
26
     **/
27
    private $client;
28
29
    /**
30
     * @var SuccessHttpStatusCode
31
     */
32
    private static $guard;
33
34
    public function __construct(TypeSupport $client)
35
    {
36
        $this->client = $client;
37
        self::$guard = SuccessHttpStatusCode::withoutContentId();
38
    }
39
40
    public function create(string $collectionName, array $options = []): string
41
    {
42
        $type = CollectionType::create($collectionName, $options)
43
            ->useGuard(self::$guard);
44
45
        $response = $this->client->sendType($type);
46
47
        $data = Json::decode($response->getBody()->getContents());
48
49
        if (!isset($data['id'])) {
50
            throw UnexpectedResponse::forType($type, $response);
51
        }
52
53
        return $data['id'];
54
    }
55
56
    public function has(string $collectionName): bool
57
    {
58
        $type = CollectionType::info($collectionName)
59
            ->useGuard(self::$guard);
60
61
        try {
62
            $this->client->sendType($type);
63
64
            return true;
65
        } catch (GuardErrorException $e) {
66
            return false;
67
        }
68
    }
69
70
    public function drop(string $collectionName): void
71
    {
72
        $type = CollectionType::delete($collectionName)
73
            ->useGuard(self::$guard);
74
75
        $this->client->sendType($type);
76
    }
77
78
    public function count(string $collectionName): int
79
    {
80
        $type = CollectionType::count($collectionName)
81
            ->useGuard(self::$guard);
82
83
        $response = $this->client->sendType($type);
84
85
        $data = Json::decode($response->getBody()->getContents());
86
87
        if (!isset($data['count'])) {
88
            throw UnexpectedResponse::forType($type, $response);
89
        }
90
91
        return $data['count'];
92
    }
93
94
    public function get(string $collectionName): ResponseInterface
95
    {
96
        $type = CollectionType::info($collectionName)
97
            ->useGuard(self::$guard);
98
99
        return $this->client->sendType($type);
100
    }
101
102
    public function truncate(string $collectionName): void
103
    {
104
        $type = CollectionType::truncate($collectionName)
105
            ->useGuard(self::$guard);
106
107
        $this->client->sendType($type);
108
    }
109
}
110