Cursor::create()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0729

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 33
ccs 12
cts 14
cp 0.8571
rs 9.4555
cc 5
nc 16
nop 6
crap 5.0729
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\Http\Url;
15
use ArangoDb\Util\Json;
16
use Fig\Http\Message\RequestMethodInterface;
17
use Psr\Http\Message\RequestFactoryInterface;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\StreamFactoryInterface;
20
21
final class Cursor implements CursorType
22
{
23
    /**
24
     * @var array
25
     */
26
    private $options;
27
28
    /**
29
     * @var string
30
     */
31
    private $uri;
32
33
    /**
34
     * @var string
35
     */
36
    private $method;
37
38 10
    private function __construct(
39
        string $uri,
40
        string $method,
41
        array $options = []
42
    ) {
43 10
        $this->uri = $uri;
44 10
        $this->method = $method;
45 10
        $this->options = $options;
46 10
    }
47
48 8
    public static function create(
49
        string $query,
50
        array $bindVars = [],
51
        int $batchSize = null,
52
        bool $count = false,
53
        bool $cache = null,
54
        array $options = []
55
    ): CursorType {
56
        $params = [
57 8
            'query' => $query,
58 8
            'count' => $count,
59
        ];
60
61 8
        if ($batchSize !== null) {
62 6
            $params['batchSize'] = $batchSize;
63
        }
64
65 8
        if (0 !== count($bindVars)) {
66
            $params['bindVars'] = $bindVars;
67
        }
68
69 8
        if (0 !== count($options)) {
70 1
            $params['options'] = $options;
71
        }
72
73 8
        if ($cache !== null) {
74
            $params['cache'] = $cache;
75
        }
76
77 8
        return new self(
78 8
            '',
79 8
            RequestMethodInterface::METHOD_POST,
80
            $params
81
        );
82
    }
83
84 1
    public static function delete(string $cursorId): CursorType
85
    {
86 1
        return new self(
87 1
            '/' . $cursorId,
88 1
            RequestMethodInterface::METHOD_DELETE
89
        );
90
    }
91
92 5
    public static function nextBatch(string $cursorId): CursorType
93
    {
94 5
        return new self(
95 5
            '/' . $cursorId,
96 5
            RequestMethodInterface::METHOD_PUT
97
        );
98
    }
99
100 10
    public function toRequest(
101
        RequestFactoryInterface $requestFactory,
102
        StreamFactoryInterface $streamFactory
103
    ): RequestInterface {
104 10
        $request = $requestFactory->createRequest($this->method, Url::CURSOR . $this->uri);
105
106 10
        if (0 === count($this->options)) {
107 6
            return $request;
108
        }
109
110 8
        return $request->withBody($streamFactory->createStream(Json::encode($this->options)));
111
    }
112
}
113