1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BaoPham\DynamoDb; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Aws\DynamoDb\Marshaler; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
|
10
|
|
|
class DynamoDbClientService implements DynamoDbClientInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $clients; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Aws\DynamoDb\Marshaler |
19
|
|
|
*/ |
20
|
|
|
protected $marshaler; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \BaoPham\DynamoDb\EmptyAttributeFilter |
24
|
|
|
*/ |
25
|
|
|
protected $attributeFilter; |
26
|
|
|
|
27
|
145 |
|
public function __construct(Marshaler $marshaler, EmptyAttributeFilter $filter) |
28
|
|
|
{ |
29
|
145 |
|
$this->marshaler = $marshaler; |
30
|
145 |
|
$this->attributeFilter = $filter; |
31
|
145 |
|
$this->clients = []; |
32
|
145 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return \Aws\DynamoDb\DynamoDbClient |
36
|
|
|
*/ |
37
|
132 |
|
public function getClient($connection = null) |
38
|
|
|
{ |
39
|
132 |
|
$connection = $connection ?: config('dynamodb.default'); |
40
|
|
|
|
41
|
132 |
|
if (isset($this->clients[$connection])) { |
42
|
120 |
|
return $this->clients[$connection]; |
43
|
|
|
} |
44
|
|
|
|
45
|
132 |
|
$config = config("dynamodb.connections.$connection", []); |
46
|
132 |
|
$config['version'] = '2012-08-10'; |
47
|
132 |
|
$config['debug'] = $this->getDebugOptions(Arr::get($config, 'debug')); |
48
|
|
|
|
49
|
132 |
|
$client = new DynamoDbClient($config); |
50
|
|
|
|
51
|
132 |
|
$this->clients[$connection] = $client; |
52
|
|
|
|
53
|
132 |
|
return $client; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return \Aws\DynamoDb\Marshaler |
58
|
|
|
*/ |
59
|
138 |
|
public function getMarshaler() |
60
|
|
|
{ |
61
|
138 |
|
return $this->marshaler; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return \BaoPham\DynamoDb\EmptyAttributeFilter |
66
|
|
|
*/ |
67
|
131 |
|
public function getAttributeFilter() |
68
|
|
|
{ |
69
|
131 |
|
return $this->attributeFilter; |
70
|
|
|
} |
71
|
|
|
|
72
|
132 |
|
protected function getDebugOptions($debug = false) |
73
|
|
|
{ |
74
|
132 |
|
if ($debug === true) { |
75
|
|
|
$logfn = function ($msg) { |
76
|
|
|
Log::info($msg); |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
return ['logfn' => $logfn]; |
80
|
|
|
} |
81
|
|
|
|
82
|
132 |
|
return $debug; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|