Completed
Pull Request — master (#37)
by Eugene
08:53
created

Space::normalizeIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client\Schema;
15
16
use Tarantool\Client\Client;
17
use Tarantool\Client\Exception\Exception;
18
use Tarantool\Client\Request\DeleteRequest;
19
use Tarantool\Client\Request\InsertRequest;
20
use Tarantool\Client\Request\ReplaceRequest;
21
use Tarantool\Client\Request\SelectRequest;
22
use Tarantool\Client\Request\UpdateRequest;
23 16
use Tarantool\Client\Request\UpsertRequest;
24
use Tarantool\Client\Response\BinaryResponse;
25 16
26 16
final class Space
27 16
{
28
    public const VSPACE = 281;
29 1
    public const VINDEX = 289;
30
31 1
    private $client;
32
    private $id;
33
    private $indexes = [];
34 34
35
    public function __construct(Client $client, int $id)
36 34
    {
37 34
        $this->client = $client;
38 34
        $this->id = $id;
39 34
    }
40 34
41
    public function getId() : int
42 34
    {
43
        return $this->id;
44 34
    }
45
46
    public function select(array $key = [], $index = 0, int $limit = \PHP_INT_MAX &0xffffffff, int $offset = 0, int $iteratorType = IteratorTypes::EQ) : BinaryResponse
47 16
    {
48
        if (\is_string($index)) {
49 16
            $index = $this->getIndexIdByName($index);
50
        }
51 16
52
        $request = new SelectRequest($this->id, $index, $key, $offset, $limit, $iteratorType);
53
54 2
        return BinaryResponse::createFromRaw($this->client->sendRequest($request));
55
    }
56 2
57
    public function insert(array $values) : BinaryResponse
58 2
    {
59
        $request = new InsertRequest($this->id, $values);
60
61 13
        return BinaryResponse::createFromRaw($this->client->sendRequest($request));
62
    }
63 13
64 13
    public function replace(array $values) : BinaryResponse
65
    {
66 13
        $request = new ReplaceRequest($this->id, $values);
67
68
        return BinaryResponse::createFromRaw($this->client->sendRequest($request));
69 1
    }
70
71 1
    public function update(array $key, array $operations, $index = 0) : BinaryResponse
72
    {
73 1
        if (\is_string($index)) {
74
            $index = $this->getIndexIdByName($index);
75
        }
76 5
77
        $request = new UpdateRequest($this->id, $index, $key, $operations);
78 5
79 5
        return BinaryResponse::createFromRaw($this->client->sendRequest($request));
80
    }
81 5
82
    public function upsert(array $values, array $operations) : BinaryResponse
83
    {
84 2
        $request = new UpsertRequest($this->id, $values, $operations);
85
86 2
        return BinaryResponse::createFromRaw($this->client->sendRequest($request));
87 2
    }
88
89 16
    public function delete(array $key, $index = 0) : BinaryResponse
90
    {
91 16
        if (\is_string($index)) {
92 12
            $index = $this->getIndexIdByName($index);
93
        }
94
95 5
        $request = new DeleteRequest($this->id, $index, $key);
96 5
97 5
        return BinaryResponse::createFromRaw($this->client->sendRequest($request));
98
    }
99 5
100 1
    public function flushIndexes() : void
101
    {
102
        $this->indexes = [];
103 4
    }
104
105
    private function getIndexIdByName(string $indexName) : int
106 49
    {
107
        if (isset($this->indexes[$indexName])) {
108 49
            return $this->indexes[$indexName];
109 26
        }
110
111
        $schema = $this->client->getSpaceById(self::VINDEX);
112 32
        $response = $schema->select([$this->id, $indexName], Index::INDEX_NAME);
113 21
        $data = $response->getData();
114
115
        if (empty($data)) {
116 16
            throw new Exception("No index '$indexName' is defined in space #{$this->id}");
117
        }
118
119
        return $this->indexes[$indexName] = $response->getData()[0][1];
120
    }
121
}
122