Issues (55)

src/Schema/Space.php (1 issue)

1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Schema;
15
16
use Tarantool\Client\Exception\RequestFailed;
17
use Tarantool\Client\Handler\Handler;
18
use Tarantool\Client\Keys;
19
use Tarantool\Client\Request\DeleteRequest;
20
use Tarantool\Client\Request\InsertRequest;
21
use Tarantool\Client\Request\ReplaceRequest;
22
use Tarantool\Client\Request\SelectRequest;
23
use Tarantool\Client\Request\UpdateRequest;
24
use Tarantool\Client\Request\UpsertRequest;
25
26
final class Space
27
{
28
    public const VSPACE_ID = 281;
29
    public const VSPACE_NAME_INDEX = 2;
30
    public const VINDEX_ID = 289;
31
    public const VINDEX_NAME_INDEX = 2;
32
33
    private $handler;
34
    private $id;
35
36
    /** @var array<string, int> */
37
    private $indexes = [];
38
39 275
    public function __construct(Handler $handler, int $id)
40
    {
41 275
        $this->handler = $handler;
42 275
        $this->id = $id;
43
    }
44
45 8
    public function getId() : int
46
    {
47 8
        return $this->id;
48
    }
49
50 275
    public function select(Criteria $criteria) : array
51
    {
52 275
        $index = $criteria->getIndex();
53
54 275
        if (\is_string($index)) {
55 60
            $index = $this->getIndexIdByName($index);
56
        }
57
58 275
        $request = new SelectRequest(
59 275
            $this->id,
60 275
            $index,
61 275
            $criteria->getKey(),
62 275
            $criteria->getOffset(),
63 275
            $criteria->getLimit(),
64 275
            $criteria->getIterator()
65 275
        );
66
67 275
        return $this->handler->handle($request)->getBodyField(Keys::DATA);
68
    }
69
70
    /**
71
     * @psalm-param non-empty-array<int, mixed> $tuple
72
     */
73 76
    public function insert(array $tuple) : array
74
    {
75 76
        $request = new InsertRequest($this->id, $tuple);
76
77 76
        return $this->handler->handle($request)->getBodyField(Keys::DATA);
78
    }
79
80
    /**
81
     * @psalm-param non-empty-array<int, mixed> $tuple
82
     */
83 12
    public function replace(array $tuple) : array
84
    {
85 12
        $request = new ReplaceRequest($this->id, $tuple);
86
87 12
        return $this->handler->handle($request)->getBodyField(Keys::DATA);
88
    }
89
90
    /**
91
     * @psalm-param non-empty-array<int, mixed> $key
92
     * @param int|string $index
93
     */
94 40
    public function update(array $key, Operations $operations, $index = 0) : array
95
    {
96 40
        if (\is_string($index)) {
97 8
            $index = $this->getIndexIdByName($index);
98
        }
99
100 36
        $request = new UpdateRequest($this->id, $index, $key, $operations->toArray());
101
102 36
        return $this->handler->handle($request)->getBodyField(Keys::DATA);
103
    }
104
105
    /**
106
     * @psalm-param non-empty-array<int, mixed> $tuple
107
     */
108 8
    public function upsert(array $tuple, Operations $operations) : void
109
    {
110 8
        $request = new UpsertRequest($this->id, $tuple, $operations->toArray());
111
112 8
        $this->handler->handle($request);
113
    }
114
115
    /**
116
     * @psalm-param non-empty-array<int, mixed> $key
117
     * @param int|string $index
118
     */
119 24
    public function delete(array $key, $index = 0) : array
120
    {
121 24
        if (\is_string($index)) {
122 4
            $index = $this->getIndexIdByName($index);
123
        }
124
125 24
        $request = new DeleteRequest($this->id, $index, $key);
126
127 24
        return $this->handler->handle($request)->getBodyField(Keys::DATA);
128
    }
129
130 8
    public function flushIndexes() : void
131
    {
132 8
        $this->indexes = [];
133
    }
134
135 72
    private function getIndexIdByName(string $indexName) : int
136
    {
137 72
        if (isset($this->indexes[$indexName])) {
138 4
            return $this->indexes[$indexName];
139
        }
140
141 72
        $schema = new self($this->handler, self::VINDEX_ID);
142 72
        $data = $schema->select(Criteria::key([$this->id, $indexName])->andIndex(self::VINDEX_NAME_INDEX));
143
144 72
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145 64
            return $this->indexes[$indexName] = $data[0][1];
146
        }
147
148 8
        throw RequestFailed::unknownIndex($indexName, $this->id);
149
    }
150
}
151