Index::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
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 Index implements IndexType, GuardSupport
23
{
24
    /**
25
     * @var string|null
26
     */
27
    private $name;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $collectionName;
33
34
    /**
35
     * @var array
36
     */
37
    private $options;
38
39
    /**
40
     * @var string
41
     */
42
    private $uri;
43
44
    /**
45
     * @var string
46
     */
47
    private $method;
48
49
    /**
50
     * Guard
51
     *
52
     * @var Guard
53
     */
54
    private $guard;
55
56 6
    private function __construct(
57
        ?string $name,
58
        ?string $collectionName,
59
        string $uri = '',
60
        string $method = RequestMethodInterface::METHOD_GET,
61
        array $options = []
62
    ) {
63 6
        $this->name = $name;
64 6
        $this->collectionName = $collectionName;
65 6
        $this->uri = $uri;
66 6
        $this->method = $method;
67 6
        $this->options = $options;
68 6
    }
69
70 2
    public static function listAll(string $collectionName): IndexType
71
    {
72 2
        return new self(null, $collectionName, '?' . http_build_query(['collection' => $collectionName]));
73
    }
74
75 2
    public static function info(string $indexName): IndexType
76
    {
77 2
        return new self($indexName, null, '/' . $indexName);
78
    }
79
80 2
    public static function create(string $collectionName, array $options = []): IndexType
81
    {
82 2
        return new self(
83 2
            null,
84
            $collectionName,
85 2
            '?' . http_build_query(['collection' => $collectionName]),
86 2
            RequestMethodInterface::METHOD_POST,
87
            $options
88
        );
89
    }
90
91 1
    public static function delete(string $indexName): IndexType
92
    {
93 1
        return new self(
94 1
            $indexName,
95 1
            null,
96 1
            '/' . $indexName,
97 1
            RequestMethodInterface::METHOD_DELETE
98
        );
99
    }
100
101 5
    public function toRequest(
102
        RequestFactoryInterface $requestFactory,
103
        StreamFactoryInterface $streamFactory
104
    ): RequestInterface {
105 5
        $request = $requestFactory->createRequest($this->method, Url::INDEX . $this->uri);
106
107 5
        if (0 === count($this->options)) {
108 4
            return $request;
109
        }
110
111 2
        return $request->withBody($streamFactory->createStream(Json::encode($this->options)));
112
    }
113
114
    public function useGuard(Guard $guard): Type
115
    {
116
        $this->guard = $guard;
117
        return $this;
118
    }
119
120 1
    public function guard(): ?Guard
121
    {
122 1
        return $this->guard;
123
    }
124
}
125