Completed
Push — master ( bdd70c...0550f6 )
by Eugene
10:03
created

Space::upsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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