Collection::truncate()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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\Type;
13
14
use ArangoDb\Guard\Guard;
15
use ArangoDb\Http\Url;
16
use ArangoDb\Util\Json;
17
use Fig\Http\Message\RequestMethodInterface;
18
use Psr\Http\Message\RequestFactoryInterface;
19
use Psr\Http\Message\RequestInterface;
20
use Psr\Http\Message\StreamFactoryInterface;
21
22
final class Collection implements CollectionType
23
{
24
    /**
25
     * @var string|null
26
     */
27
    private $name;
28
29
    /**
30
     * @var array
31
     */
32
    private $options;
33
34
    /**
35
     * @var string
36
     */
37
    private $uri;
38
39
    /**
40
     * @var string
41
     */
42
    private $method;
43
44
    /**
45
     * Guard
46
     *
47
     * @var Guard
48
     */
49
    private $guard;
50
51 20
    private function __construct(
52
        ?string $name,
53
        string $uri = '',
54
        string $method = RequestMethodInterface::METHOD_GET,
55
        array $options = []
56
    ) {
57 20
        $this->name = $name;
58 20
        $this->uri = $uri;
59 20
        $this->method = $method;
60 20
        $this->options = $options;
61 20
    }
62
63 20
    public static function create(string $collectionName, array $options = []): CollectionType
64
    {
65 20
        $options['name'] = $collectionName;
66 20
        return new self($collectionName, '', RequestMethodInterface::METHOD_POST, $options);
67
    }
68
69 1
    public static function listAll(bool $excludeSystem = true): CollectionType
70
    {
71 1
        return new self(null, '?excludeSystem=' . ($excludeSystem ? 'true' : 'false'));
72
    }
73
74 4
    public static function info(string $collectionName): CollectionType
75
    {
76 4
        return new self($collectionName);
77
    }
78
79
    public static function checksum(string $collectionName): CollectionType
80
    {
81
        return new self($collectionName, '/checksum');
82
    }
83
84 3
    public static function count(string $collectionName): CollectionType
85
    {
86 3
        return new self($collectionName, '/count');
87
    }
88
89
    public static function figures(string $collectionName): CollectionType
90
    {
91
        return new self($collectionName, '/figures');
92
    }
93
94
    public static function properties(string $collectionName): CollectionType
95
    {
96
        return new self($collectionName, '/properties');
97
    }
98
99
    public static function revision(string $collectionName): CollectionType
100
    {
101
        return new self($collectionName, '/revision');
102
    }
103
104 1
    public static function delete(string $collectionName): CollectionType
105
    {
106 1
        return new self($collectionName, '', RequestMethodInterface::METHOD_DELETE);
107
    }
108
109
    public static function load(string $collectionName): CollectionType
110
    {
111
        return new self($collectionName, '/load', RequestMethodInterface::METHOD_PUT);
112
    }
113
114
    public static function loadIndexes(string $collectionName): CollectionType
115
    {
116
        return new self($collectionName, '/loadIndexesIntoMemory', RequestMethodInterface::METHOD_PUT);
117
    }
118
119
    public static function updateProperties(
120
        string $collectionName,
121
        bool $waitForSync = null,
122
        int $journalSize = null
123
    ): CollectionType {
124
        $options = [];
125
126
        if ($waitForSync !== null) {
127
            $options['waitForSync'] = $waitForSync;
128
        }
129
        if ($journalSize !== null) {
130
            $options['journalSize'] = $journalSize;
131
        }
132
133
        return new self($collectionName, '/properties', RequestMethodInterface::METHOD_PUT, $options);
134
    }
135
136
    public static function rename(string $collectionName, string $newCollectionName): CollectionType
137
    {
138
        return new self($collectionName, '/rename', RequestMethodInterface::METHOD_PUT, ['name' => $newCollectionName]);
139
    }
140
141
    public static function rotate(string $collectionName): CollectionType
142
    {
143
        return new self($collectionName, '/rotate', RequestMethodInterface::METHOD_PUT);
144
    }
145
146 1
    public static function truncate(string $collectionName): CollectionType
147
    {
148 1
        return new self($collectionName, '/truncate', RequestMethodInterface::METHOD_PUT);
149
    }
150
151
    public static function unload(string $collectionName): CollectionType
152
    {
153
        return new self($collectionName, '/unload', RequestMethodInterface::METHOD_PUT);
154
    }
155
156 20
    public function toRequest(
157
        RequestFactoryInterface $requestFactory,
158
        StreamFactoryInterface $streamFactory
159
    ): RequestInterface {
160 20
        $uri = $this->uri;
161
162 20
        if ($this->name !== '' && $this->method !== RequestMethodInterface::METHOD_POST) {
163 6
            $uri = '/' . $this->name . $uri;
164
        }
165
166 20
        $request = $requestFactory->createRequest($this->method, Url::COLLECTION . $uri);
167
168 20
        if (0 === count($this->options)) {
169 6
            return $request;
170
        }
171 20
        return $request->withBody($streamFactory->createStream(Json::encode($this->options)));
172
    }
173
174 12
    public function useGuard(Guard $guard): Type
175
    {
176 12
        $this->guard = $guard;
177 12
        return $this;
178
    }
179
180 14
    public function guard(): ?Guard
181
    {
182 14
        return $this->guard;
183
    }
184
}
185