1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guillermoandrae\DynamoDb; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Aws\DynamoDb\Marshaler; |
7
|
|
|
use Guillermoandrae\Common\CollectionInterface; |
8
|
|
|
use Guillermoandrae\DynamoDb\Contract\DynamoDbAdapterInterface; |
9
|
|
|
use Guillermoandrae\DynamoDb\Contract\DynamoDbClientAwareTrait; |
10
|
|
|
use Guillermoandrae\DynamoDb\Factory\DynamoDbClientFactory; |
11
|
|
|
use Guillermoandrae\DynamoDb\Factory\MarshalerFactory; |
12
|
|
|
use Guillermoandrae\DynamoDb\Factory\OperationFactory; |
13
|
|
|
|
14
|
|
|
final class DynamoDbAdapter implements DynamoDbAdapterInterface |
15
|
|
|
{ |
16
|
|
|
use DynamoDbClientAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string The table name. |
20
|
|
|
*/ |
21
|
|
|
private $tableName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* DynamoDBAdapter constructor. |
25
|
|
|
* |
26
|
|
|
* Registers the AWS DynamoDB client and Marshaler with this object. If no arguments are provided, we use the |
27
|
|
|
* respective factories to generate the necessary objects. |
28
|
|
|
* |
29
|
|
|
* @param DynamoDbClient|null $client OPTIONAL The DynamoDB client. |
30
|
|
|
* @param Marshaler|null $marshaler OPTIONAL The Marshaler. |
31
|
|
|
*/ |
32
|
30 |
|
public function __construct(?DynamoDbClient $client = null, ?Marshaler $marshaler = null) |
33
|
|
|
{ |
34
|
30 |
|
if (empty($client)) { |
35
|
30 |
|
$client = DynamoDbClientFactory::factory(); |
36
|
30 |
|
$this->setClient($client); |
37
|
30 |
|
OperationFactory::setClient($client); |
38
|
|
|
} |
39
|
|
|
|
40
|
30 |
|
if (empty($marshaler)) { |
41
|
30 |
|
$marshaler = MarshalerFactory::factory(); |
42
|
30 |
|
$this->setMarshaler($marshaler); |
43
|
30 |
|
OperationFactory::setMarshaler($marshaler); |
44
|
|
|
} |
45
|
30 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
10 |
|
public function createTable(array $data, ?string $tableName = '', ?array $options = []): bool |
51
|
|
|
{ |
52
|
10 |
|
if (empty($tableName)) { |
53
|
10 |
|
$tableName = $this->tableName; |
54
|
|
|
} |
55
|
10 |
|
return OperationFactory::factory('create-table', $tableName, $data)->execute(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
9 |
|
public function deleteTable(string $tableName = ''): bool |
62
|
|
|
{ |
63
|
9 |
|
if (empty($tableName)) { |
64
|
9 |
|
$tableName = $this->tableName; |
65
|
|
|
} |
66
|
9 |
|
return OperationFactory::factory('delete-table', $tableName)->execute(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
2 |
|
public function describeTable(string $tableName = ''): ?array |
73
|
|
|
{ |
74
|
2 |
|
if (empty($tableName)) { |
75
|
2 |
|
$tableName = $this->tableName; |
76
|
|
|
} |
77
|
2 |
|
return OperationFactory::factory('describe-table', $tableName)->execute(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
2 |
|
public function tableExists(string $tableName = ''): bool |
84
|
|
|
{ |
85
|
2 |
|
if (empty($tableName)) { |
86
|
1 |
|
$tableName = $this->tableName; |
87
|
|
|
} |
88
|
2 |
|
$tables = $this->listTables(); |
89
|
2 |
|
if (empty($tables)) { |
90
|
2 |
|
return false; |
91
|
|
|
} |
92
|
2 |
|
return in_array( |
93
|
2 |
|
strtolower($tableName), |
94
|
2 |
|
array_map('strtolower', $tables) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
30 |
|
public function listTables(): ?array |
102
|
|
|
{ |
103
|
30 |
|
return OperationFactory::factory('list-tables')->execute(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
18 |
|
public function useTable(string $tableName): DynamoDbAdapterInterface |
110
|
|
|
{ |
111
|
18 |
|
$this->tableName = $tableName; |
112
|
18 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function findWhere(array $conditions, int $offset = 0, ?int $limit = null): CollectionInterface |
119
|
|
|
{ |
120
|
2 |
|
return OperationFactory::factory('query', $this->tableName, $conditions)->execute(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
3 |
|
public function findAll(int $offset = 0, ?int $limit = null, ?array $conditions = []): CollectionInterface |
127
|
|
|
{ |
128
|
3 |
|
return OperationFactory::factory('scan', $this->tableName, $conditions)->execute(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
3 |
|
public function find($primaryKey): array |
135
|
|
|
{ |
136
|
3 |
|
return OperationFactory::factory('get-item', $this->tableName, $primaryKey)->execute(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
*/ |
142
|
6 |
|
public function insert(array $data): bool |
143
|
|
|
{ |
144
|
6 |
|
return OperationFactory::factory('put-item', $this->tableName, $data)->execute(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritDoc} |
149
|
|
|
*/ |
150
|
2 |
|
public function update(array $primaryKey, array $data): bool |
151
|
|
|
{ |
152
|
2 |
|
return OperationFactory::factory('update-item', $this->tableName, $primaryKey, $data)->execute(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritDoc} |
157
|
|
|
*/ |
158
|
2 |
|
public function delete($id): bool |
159
|
|
|
{ |
160
|
2 |
|
return OperationFactory::factory('delete-item', $this->tableName, $id)->execute(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|