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