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