Completed
Pull Request — master (#212)
by Alexandru
10:59
created

DynamoDbClientService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 23
dl 0
loc 73
c 0
b 0
f 0
ccs 0
cts 25
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeFilter() 0 3 1
A getClient() 0 17 3
A getDebugOptions() 0 11 2
A getMarshaler() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace Rennokki\DynamoDb;
4
5
use Illuminate\Support\Arr;
6
use Aws\DynamoDb\DynamoDbClient;
7
use Aws\DynamoDb\Marshaler;
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 \Rennokki\DynamoDb\EmptyAttributeFilter
24
     */
25
    protected $attributeFilter;
26
27
    public function __construct(Marshaler $marshaler, EmptyAttributeFilter $filter)
28
    {
29
        $this->marshaler = $marshaler;
30
        $this->attributeFilter = $filter;
31
        $this->clients = [];
32
    }
33
34
    /**
35
     * @return \Aws\DynamoDb\DynamoDbClient
36
     */
37
    public function getClient($connection = null)
38
    {
39
        $connection = $connection ?: config('dynamodb.default');
40
41
        if (isset($this->clients[$connection])) {
42
            return $this->clients[$connection];
43
        }
44
45
        $config = config("dynamodb.connections.$connection", []);
46
        $config['version'] = '2012-08-10';
47
        $config['debug'] = $this->getDebugOptions(Arr::get($config, 'debug'));
48
49
        $client = new DynamoDbClient($config);
50
51
        $this->clients[$connection] = $client;
52
53
        return $client;
54
    }
55
56
    /**
57
     * @return \Aws\DynamoDb\Marshaler
58
     */
59
    public function getMarshaler()
60
    {
61
        return $this->marshaler;
62
    }
63
64
    /**
65
     * @return \Rennokki\DynamoDb\EmptyAttributeFilter
66
     */
67
    public function getAttributeFilter()
68
    {
69
        return $this->attributeFilter;
70
    }
71
72
    protected function getDebugOptions($debug = false)
73
    {
74
        if ($debug === true) {
75
            $logfn = function ($msg) {
76
                Log::info($msg);
77
            };
78
79
            return ['logfn' => $logfn];
80
        }
81
82
        return $debug;
83
    }
84
}
85