Passed
Pull Request — master (#37)
by Eugene
06:05
created

Space::upsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\Exception\RequestFailed;
17
use Tarantool\Client\Handler\Handler;
18
use Tarantool\Client\IProto;
19
use Tarantool\Client\Request\Delete;
20
use Tarantool\Client\Request\Insert;
21
use Tarantool\Client\Request\Replace;
22
use Tarantool\Client\Request\Select;
23
use Tarantool\Client\Request\Update;
24
use Tarantool\Client\Request\Upsert;
25
26
final class Space
27
{
28
    public const VSPACE_ID = 281;
29
    public const VINDEX_ID = 289;
30
31
    private $handler;
32
    private $id;
33
    private $indexes = [];
34
35 112
    public function __construct(Handler $handler, int $id)
36
    {
37 112
        $this->handler = $handler;
38 112
        $this->id = $id;
39 112
    }
40
41 4
    public function getId() : int
42
    {
43 4
        return $this->id;
44
    }
45
46 112
    public function select(Criteria $criteria) : array
47
    {
48 112
        $index = $criteria->getIndex();
49
50 112
        if (\is_string($index)) {
0 ignored issues
show
introduced by
The condition is_string($index) is always false.
Loading history...
51 30
            $index = $this->getIndexIdByName($index);
52
        }
53
54 112
        $request = new Select(
55 112
            $this->id,
56 112
            $index,
57 112
            $criteria->getKey(),
58 112
            $criteria->getOffset(),
59 112
            $criteria->getLimit(),
60 112
            $criteria->getIteratorType()
61
        );
62
63 112
        return $this->handler->handle($request)->getBodyField(IProto::DATA);
64
    }
65
66 32
    public function insert(array $tuple) : array
67
    {
68 32
        $request = new Insert($this->id, $tuple);
69
70 32
        return $this->handler->handle($request)->getBodyField(IProto::DATA);
71
    }
72
73 2
    public function replace(array $tuple) : array
74
    {
75 2
        $request = new Replace($this->id, $tuple);
76
77 2
        return $this->handler->handle($request)->getBodyField(IProto::DATA);
78
    }
79
80 18
    public function update(array $key, Operations $operations, $index = 0) : array
81
    {
82 18
        if (\is_string($index)) {
83 4
            $index = $this->getIndexIdByName($index);
84
        }
85
86 16
        $request = new Update($this->id, $index, $key, $operations->toArray());
87
88 16
        return $this->handler->handle($request)->getBodyField(IProto::DATA);
89
    }
90
91 2
    public function upsert(array $tuple, Operations $operations) : void
92
    {
93 2
        $request = new Upsert($this->id, $tuple, $operations->toArray());
94
95 2
        $this->handler->handle($request);
96 2
    }
97
98 8
    public function delete(array $key, $index = 0) : array
99
    {
100 8
        if (\is_string($index)) {
101 2
            $index = $this->getIndexIdByName($index);
102
        }
103
104 8
        $request = new Delete($this->id, $index, $key);
105
106 8
        return $this->handler->handle($request)->getBodyField(IProto::DATA);
107
    }
108
109 4
    public function flushIndexes() : void
110
    {
111 4
        $this->indexes = [];
112 4
    }
113
114 36
    private function getIndexIdByName(string $indexName) : int
115
    {
116 36
        if (isset($this->indexes[$indexName])) {
117 2
            return $this->indexes[$indexName];
118
        }
119
120 36
        $schema = new self($this->handler, self::VINDEX_ID);
121 36
        $data = $schema->select(Criteria::key([$this->id, $indexName])->andIndex(Index::INDEX_NAME));
122
123 36
        if ([] === $data) {
124 4
            throw RequestFailed::unknownIndex($indexName, $this->id);
125
        }
126
127 32
        return $this->indexes[$indexName] = $data[0][1];
128
    }
129
}
130