Completed
Push — master ( 05eff2...70dca9 )
by Masiukevich
03:14
created

IndexProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Event Sourcing implementation module
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\EventSourcingModule;
14
15
use function Amp\call;
16
use Amp\Promise;
17
use ServiceBus\EventSourcing\Indexes\IndexKey;
18
use ServiceBus\EventSourcing\Indexes\IndexValue;
19
use ServiceBus\EventSourcing\Indexes\Store\IndexStore;
20
use ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed;
21
use ServiceBus\Storage\Common\Exceptions\UniqueConstraintViolationCheckFailed;
22
23
/**
24
 *
25
 */
26
final class IndexProvider
27
{
28
    /**
29
     * @var IndexStore
30
     */
31
    private $store;
32
33
    /**
34
     * @param IndexStore $store
35
     */
36 6
    public function __construct(IndexStore $store)
37
    {
38 6
        $this->store = $store;
39 6
    }
40
41
    /**
42
     * Receive index value
43
     *
44
     * @noinspection PhpDocRedundantThrowsInspection
45
     * @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise
46
     *
47
     * @param IndexKey $indexKey
48
     *
49
     * @return Promise<\ServiceBus\EventSourcing\Indexes\IndexValue|null>
50
     *
51
     * @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed
52
     */
53 3
    public function get(IndexKey $indexKey): Promise
54
    {
55
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
56 3
        return call(
57
            function(IndexKey $indexKey): \Generator
58
            {
59
                try
60
                {
61
                    /**
62
                     * @psalm-suppress TooManyTemplateParams Wrong Promise template
63
                     * @var IndexValue|null $value
64
                     */
65 3
                    $value = yield $this->store->find($indexKey);
66
67 3
                    return $value;
68
                }
69
                catch(\Throwable $throwable)
70
                {
71
                    throw IndexOperationFailed::fromThrowable($throwable);
72
                }
73 3
            },
74 3
            $indexKey
75
        );
76
    }
77
78
    /**
79
     * Is there a value in the index
80
     *
81
     * @noinspection PhpDocRedundantThrowsInspection
82
     * @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise
83
     *
84
     * @param IndexKey $indexKey
85
     *
86
     * @return Promise<bool>
87
     *
88
     * @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed
89
     */
90 1
    public function has(IndexKey $indexKey): Promise
91
    {
92
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
93 1
        return call(
94
            function(IndexKey $indexKey): \Generator
95
            {
96
                try
97
                {
98
                    /**
99
                     * @psalm-suppress TooManyTemplateParams Wrong Promise template
100
                     * @var IndexValue|null $value
101
                     */
102 1
                    $value = yield $this->store->find($indexKey);
103
104 1
                    return null !== $value;
105
                }
106
                catch(\Throwable $throwable)
107
                {
108
                    throw IndexOperationFailed::fromThrowable($throwable);
109
                }
110 1
            },
111 1
            $indexKey
112
        );
113
    }
114
115
    /**
116
     * Add a value to the index. If false, then the value already exists
117
     *
118
     * @noinspection PhpDocRedundantThrowsInspection
119
     * @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise
120
     *
121
     * @param IndexKey   $indexKey
122
     * @param IndexValue $value
123
     *
124
     * @return Promise<bool>
125
     *
126
     * @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed
127
     */
128 5
    public function add(IndexKey $indexKey, IndexValue $value): Promise
129
    {
130
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
131 5
        return call(
132
            function(IndexKey $indexKey, IndexValue $value): \Generator
133
            {
134
                try
135
                {
136
                    /**
137
                     * @psalm-suppress TooManyTemplateParams Wrong Promise template
138
                     * @var int $affectedRows
139
                     */
140 5
                    $affectedRows = yield $this->store->add($indexKey, $value);
141
142 5
                    return 0 !== $affectedRows;
143
                }
144 1
                catch(UniqueConstraintViolationCheckFailed $exception)
145
                {
146 1
                    return false;
147
                }
148
                catch(\Throwable $throwable)
149
                {
150
                    throw IndexOperationFailed::fromThrowable($throwable);
151
                }
152 5
            },
153 5
            $indexKey, $value
154
        );
155
    }
156
157
    /**
158
     * Remove value from index
159
     *
160
     * @noinspection PhpDocRedundantThrowsInspection
161
     *
162
     * @param IndexKey $indexKey
163
     *
164
     * @return Promise It doesn't return any result
165
     *
166
     * @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed
167
     */
168 1
    public function remove(IndexKey $indexKey): Promise
169
    {
170
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
171 1
        return call(
172
            function(IndexKey $indexKey): \Generator
173
            {
174
                try
175
                {
176 1
                    yield $this->store->delete($indexKey);
177
                }
178
                catch(\Throwable $throwable)
179
                {
180
                    throw IndexOperationFailed::fromThrowable($throwable);
181
                }
182 1
            },
183 1
            $indexKey
184
        );
185
    }
186
187
    /**
188
     * Update value in index
189
     *
190
     * @noinspection PhpDocRedundantThrowsInspection
191
     * @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise
192
     *
193
     * @param IndexKey   $indexKey
194
     * @param IndexValue $value
195
     *
196
     * @return Promise<bool>
197
     *
198
     * @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed
199
     */
200 1
    public function update(IndexKey $indexKey, IndexValue $value): Promise
201
    {
202
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
203 1
        return call(
204
            function(IndexKey $indexKey, IndexValue $value): \Generator
205
            {
206
                try
207
                {
208
                    /**
209
                     * @psalm-suppress TooManyTemplateParams Wrong Promise template
210
                     * @var int $affectedRows
211
                     */
212 1
                    $affectedRows = yield $this->store->update($indexKey, $value);
213
214 1
                    return 0 !== $affectedRows;
215
                }
216
                catch(\Throwable $throwable)
217
                {
218
                    throw IndexOperationFailed::fromThrowable($throwable);
219
                }
220 1
            },
221 1
            $indexKey, $value
222
        );
223
    }
224
}
225