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

Document::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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\Document as DocumentType;
19
use ArangoDb\Util\Json;
20
use Psr\Http\Message\ResponseInterface;
21
22
final class Document implements DocumentHandler
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 save(string $collectionName, array $doc, int $flags = 0): string
41
    {
42
        $type = DocumentType::create($collectionName, $doc, $flags)
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 get(string $documentId): ResponseInterface
57
    {
58
        $type = DocumentType::read($documentId)->useGuard(self::$guard);
59
60
        return $this->client->sendType($type);
61
    }
62
63
    public function getById(string $collectionName, string $id): ResponseInterface
64
    {
65
        return $this->get($collectionName . self::ID_SEPARATOR . $id);
66
    }
67
68
    public function remove(string $documentId): void
69
    {
70
        $type = DocumentType::deleteOne($documentId)
71
            ->useGuard(self::$guard);
72
73
        $this->client->sendType($type);
74
    }
75
76
    public function removeById(string $collectionName, string $id): void
77
    {
78
        $this->remove($collectionName . self::ID_SEPARATOR . $id);
79
    }
80
81
    public function has(string $documentId): bool
82
    {
83
        $type = DocumentType::readHeader($documentId)
84
            ->useGuard(self::$guard);
85
86
        try {
87
            $this->client->sendType($type);
88
89
            return true;
90
        } catch (GuardErrorException $e) {
91
            return false;
92
        }
93
    }
94
95
    public function hasById(string $collectionName, string $id): bool
96
    {
97
        return $this->has($collectionName . self::ID_SEPARATOR . $id);
98
    }
99
100
}
101