Completed
Push — master ( d06bab...02e31a )
by Eugene
03:46
created

Space::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tarantool\Client\Schema;
4
5
use Tarantool\Client\Client;
6
use Tarantool\Client\Exception\Exception;
7
use Tarantool\Client\Request\DeleteRequest;
8
use Tarantool\Client\Request\InsertRequest;
9
use Tarantool\Client\Request\ReplaceRequest;
10
use Tarantool\Client\Request\SelectRequest;
11
use Tarantool\Client\Request\UpdateRequest;
12
13
class Space
14
{
15
    const VSPACE = 281;
16
    const VINDEX = 289;
17
18
    private $client;
19
    private $id;
20
    private $indexes = [];
21
22 15
    public function __construct(Client $client, $id)
23
    {
24 15
        $this->client = $client;
25 15
        $this->id = $id;
26 15
    }
27
28 1
    public function getId()
29
    {
30 1
        return $this->id;
31
    }
32
33 32
    public function select(array $key = null, $index = null, $limit = null, $offset = null, $iteratorType = null)
34
    {
35 32
        $key = null === $key ? [] : $key;
36 32
        $offset = null === $offset ? 0 : $offset;
37 32
        $limit = null === $limit ? PHP_INT_MAX & 0xffffffff : $limit;
38 32
        $iteratorType = null === $iteratorType ? 0 : $iteratorType;
39 32
        $index = $this->normalizeIndex($index);
40
41 32
        $request = new SelectRequest($this->id, $index, $key, $offset, $limit, $iteratorType);
42
43 32
        return $this->client->sendRequest($request);
44
    }
45
46 16
    public function insert(array $values)
47
    {
48 16
        $request = new InsertRequest($this->id, $values);
49
50 16
        return $this->client->sendRequest($request);
51
    }
52
53 2
    public function replace(array $values)
54
    {
55 2
        $request = new ReplaceRequest($this->id, $values);
56
57 2
        return $this->client->sendRequest($request);
58
    }
59
60 12
    public function update($key, array $operations, $index = null)
61
    {
62 12
        $index = $this->normalizeIndex($index);
63 12
        $request = new UpdateRequest($this->id, $index, $key, $operations);
64
65 12
        return $this->client->sendRequest($request);
66
    }
67
68 4
    public function delete(array $key, $index = null)
69
    {
70 4
        $index = $this->normalizeIndex($index);
71 4
        $request = new DeleteRequest($this->id, $index, $key);
72
73 4
        return $this->client->sendRequest($request);
74
    }
75
76 2
    public function flushIndexes()
77
    {
78 2
        $this->indexes = [];
79 2
    }
80
81 16
    private function getIndexIdByName($indexName)
82
    {
83 16
        if (isset($this->indexes[$indexName])) {
84 12
            return $this->indexes[$indexName];
85
        }
86
87 5
        $schema = $this->client->getSpace(Space::VINDEX);
88 5
        $response = $schema->select([$this->id, $indexName], Index::INDEX_NAME);
89 5
        $data = $response->getData();
90
91 5
        if (empty($data)) {
92 1
            throw new Exception("No index '$indexName' is defined in space #{$this->id}");
93
        }
94
95 4
        return $this->indexes[$indexName] = $response->getData()[0][1];
96
    }
97
98 47
    private function normalizeIndex($index)
99
    {
100 47
        if (null === $index) {
101 24
            return 0;
102
        }
103
104 31
        if (is_int($index)) {
105 20
            return $index;
106
        }
107
108 16
        return $this->getIndexIdByName($index);
109
    }
110
}
111