1
|
|
|
<?php |
2
|
|
|
namespace Fathomminds\Rest\Database\DynamoDb; |
3
|
|
|
|
4
|
|
|
use Aws\Sdk; |
5
|
|
|
use Aws\DynamoDb\DynamoDbClient as Client; |
6
|
|
|
use Fathomminds\Rest\Contracts\IDatabase; |
7
|
|
|
use Fathomminds\Rest\Database\DynamoDb\Resource; |
8
|
|
|
use Fathomminds\Rest\Helpers\ReflectionHelper; |
9
|
|
|
|
10
|
|
|
class Database implements IDatabase |
11
|
|
|
{ |
12
|
|
|
protected $client; |
13
|
|
|
protected $databaseName; |
14
|
|
|
|
15
|
22 |
|
public function __construct(Client $client = null, $databaseName = null) |
16
|
|
|
{ |
17
|
22 |
|
$this->client = $client === null ? $this->createClient() : $client; |
18
|
22 |
|
$this->databaseName = $databaseName === null ? $this->getFullDatabaseName() : $databaseName; |
19
|
22 |
|
} |
20
|
|
|
|
21
|
4 |
|
private function createClient() |
22
|
|
|
{ |
23
|
4 |
|
$sdk = new Sdk([ |
24
|
4 |
|
'region' => getenv('AWS_SDK_REGION'), |
25
|
4 |
|
'version' => getenv('AWS_SDK_VERSION'), |
26
|
|
|
'http' => [ |
27
|
4 |
|
'verify' => getenv('AWS_SDK_HTTP_VERIFY') === 'false' ? false : getenv('AWS_SDK_HTTP_VERIFY'), |
28
|
|
|
] |
29
|
|
|
]); |
30
|
4 |
|
return $sdk->createDynamoDb(); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
private function getFullDatabaseName() |
34
|
|
|
{ |
35
|
4 |
|
return getenv('AWS_DYNAMODB_NAMESPACE') . '-' . getenv('AWS_DYNAMODB_DATABASE'); |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public function get($resourceName, $primaryKey, $resourceId = null) |
39
|
|
|
{ |
40
|
2 |
|
return (new Resource( |
41
|
2 |
|
$resourceName, |
42
|
2 |
|
$primaryKey, |
43
|
2 |
|
$this->client, |
44
|
2 |
|
$this->databaseName |
45
|
2 |
|
))->get($resourceId); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
public function post($resourceName, $primaryKey, $newResource) |
49
|
|
|
{ |
50
|
4 |
|
return (new Resource( |
51
|
4 |
|
$resourceName, |
52
|
4 |
|
$primaryKey, |
53
|
4 |
|
$this->client, |
54
|
4 |
|
$this->databaseName |
55
|
4 |
|
))->post($newResource); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function patch($resourceName, $primaryKey, $resourceId, $newResource) |
59
|
|
|
{ |
60
|
1 |
|
return (new Resource( |
61
|
1 |
|
$resourceName, |
62
|
1 |
|
$primaryKey, |
63
|
1 |
|
$this->client, |
64
|
1 |
|
$this->databaseName |
65
|
1 |
|
))->patch($resourceId, $newResource); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function put($resourceName, $primaryKey, $resourceId, $newResource) |
69
|
|
|
{ |
70
|
3 |
|
return (new Resource( |
71
|
3 |
|
$resourceName, |
72
|
3 |
|
$primaryKey, |
73
|
3 |
|
$this->client, |
74
|
3 |
|
$this->databaseName |
75
|
3 |
|
))->put($resourceId, $newResource); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function delete($resourceName, $primaryKey, $resourceId) |
79
|
|
|
{ |
80
|
2 |
|
return (new Resource( |
81
|
2 |
|
$resourceName, |
82
|
2 |
|
$primaryKey, |
83
|
2 |
|
$this->client, |
84
|
2 |
|
$this->databaseName |
85
|
2 |
|
))->delete($resourceId); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function setDatabaseName($databaseName) |
89
|
|
|
{ |
90
|
1 |
|
$this->databaseName = $databaseName; |
91
|
1 |
|
} |
92
|
|
|
|
93
|
8 |
|
public function getDatabaseName() |
94
|
|
|
{ |
95
|
8 |
|
return $this->databaseName; |
96
|
|
|
} |
97
|
|
|
|
98
|
7 |
|
public function getClient() |
99
|
|
|
{ |
100
|
7 |
|
return $this->client; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|