Completed
Push — master ( c2865e...8d4810 )
by Masiukevich
06:56
created

SqlIndexStore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 151
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 28 1
A find() 0 33 3
A __construct() 0 3 1
A update() 0 25 1
A delete() 0 21 1
1
<?php
2
3
/**
4
 * Event Sourcing implementation
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\EventSourcing\Indexes\Store;
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\Storage\Common\DatabaseAdapter;
20
use function ServiceBus\Storage\Sql\deleteQuery;
21
use function ServiceBus\Storage\Sql\equalsCriteria;
22
use function ServiceBus\Storage\Sql\fetchOne;
23
use function ServiceBus\Storage\Sql\insertQuery;
24
use function ServiceBus\Storage\Sql\selectQuery;
25
use function ServiceBus\Storage\Sql\updateQuery;
26
27
/**
28
 *
29
 */
30
final class SqlIndexStore implements IndexStore
31
{
32
    private const TABLE_NAME = 'event_sourcing_indexes';
33
34
    /**
35
     * @var DatabaseAdapter
36
     */
37
    private $adapter;
38
39
    /**
40
     * @param DatabaseAdapter $adapter
41
     */
42 4
    public function __construct(DatabaseAdapter $adapter)
43
    {
44 4
        $this->adapter = $adapter;
45 4
    }
46
47
    /**
48
     * @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise
49
     *
50
     * @inheritDoc
51
     */
52 3
    public function find(IndexKey $indexKey): Promise
53
    {
54
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
55 3
        return call(
56
            function(IndexKey $indexKey): \Generator
57
            {
58 3
                $selectQuery = selectQuery(self::TABLE_NAME)
59 3
                    ->where(equalsCriteria('index_tag', $indexKey->indexName))
60 3
                    ->andWhere(equalsCriteria('value_key', $indexKey->valueKey));
61
62 3
                $compiledQuery = $selectQuery->compile();
63
64
                /**
65
                 * @psalm-suppress TooManyTemplateParams Wrong Promise template
66
                 * @var \ServiceBus\Storage\Common\ResultSet $resultSet
67
                 */
68 3
                $resultSet = yield $this->adapter->execute($compiledQuery->sql(), $compiledQuery->params());
69
70
                /**
71
                 * @psalm-suppress TooManyTemplateParams Wrong Promise template
72
                 * @var array<string, mixed>|null $result
73
                 */
74 3
                $result = yield fetchOne($resultSet);
75
76 3
                unset($selectQuery, $compiledQuery, $resultSet);
77
78 3
                if(null !== $result && true === \is_array($result))
79
                {
80 2
                    return IndexValue::create($result['value_data']);
81
                }
82
83 3
            },
84 3
            $indexKey
85
        );
86
    }
87
88
    /**
89
     * @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise
90
     *
91
     * @inheritDoc
92
     */
93 4
    public function add(IndexKey $indexKey, IndexValue $value): Promise
94
    {
95
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
96 4
        return call(
97
            function(IndexKey $indexKey, IndexValue $value): \Generator
98
            {
99
                /** @var \Latitude\QueryBuilder\Query\InsertQuery $insertQuery */
100 4
                $insertQuery = insertQuery(self::TABLE_NAME, [
101 4
                    'index_tag'  => $indexKey->indexName,
102 4
                    'value_key'  => $indexKey->valueKey,
103 4
                    'value_data' => $value->value
104
                ]);
105
106 4
                $compiledQuery = $insertQuery->compile();
107
108
                /**
109
                 * @psalm-suppress TooManyTemplateParams Wrong Promise template
110
                 * @var \ServiceBus\Storage\Common\ResultSet $resultSet
111
                 */
112 4
                $resultSet = yield $this->adapter->execute($compiledQuery->sql(), $compiledQuery->params());
113
114 4
                $affectedRows = $resultSet->affectedRows();
115
116 4
                unset($insertQuery, $compiledQuery, $resultSet);
117
118 4
                return $affectedRows;
119 4
            },
120 4
            $indexKey, $value
121
        );
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127 1
    public function delete(IndexKey $indexKey): Promise
128
    {
129
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
130 1
        return call(
131
            function(IndexKey $indexKey): \Generator
132
            {
133 1
                $deleteQuery = deleteQuery(self::TABLE_NAME)
134 1
                    ->where(equalsCriteria('index_tag', $indexKey->indexName))
135 1
                    ->andWhere(equalsCriteria('value_key', $indexKey->valueKey));
136
137 1
                $compiledQuery = $deleteQuery->compile();
138
139
                /**
140
                 * @psalm-suppress TooManyTemplateParams Wrong Promise template
141
                 * @var \ServiceBus\Storage\Common\ResultSet $resultSet
142
                 */
143 1
                $resultSet = yield $this->adapter->execute($compiledQuery->sql(), $compiledQuery->params());
144
145 1
                unset($deleteQuery, $compiledQuery, $resultSet);
146 1
            },
147 1
            $indexKey
148
        );
149
    }
150
151
    /**
152
     * @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise
153
     *
154
     * @inheritDoc
155
     */
156 1
    public function update(IndexKey $indexKey, IndexValue $value): Promise
157
    {
158
        /** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */
159 1
        return call(
160
            function(IndexKey $indexKey, IndexValue $value): \Generator
161
            {
162 1
                $updateQuery = updateQuery(self::TABLE_NAME, ['value_data' => $value->value])
163 1
                    ->where(equalsCriteria('index_tag', $indexKey->indexName))
164 1
                    ->andWhere(equalsCriteria('value_key', $indexKey->valueKey));
165
166 1
                $compiledQuery = $updateQuery->compile();
167
168
                /**
169
                 * @psalm-suppress TooManyTemplateParams Wrong Promise template
170
                 * @var \ServiceBus\Storage\Common\ResultSet $resultSet
171
                 */
172 1
                $resultSet = yield $this->adapter->execute($compiledQuery->sql(), $compiledQuery->params());
173
174 1
                $affectedRows = $resultSet->affectedRows();
175
176 1
                unset($updateQuery, $compiledQuery, $resultSet);
177
178 1
                return $affectedRows;
179 1
            },
180 1
            $indexKey, $value
181
        );
182
    }
183
}
184