Passed
Pull Request — master (#9)
by Csaba
02:38
created

Database::createClient()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 0
crap 2
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 17
    public function __construct(Client $client = null, $databaseName = null)
16
    {
17 17
        $this->client = $client === null ? $this->createClient() : $client;
18 17
        $this->databaseName = $databaseName === null ? $this->getFullDatabaseName() : $databaseName;
19 17
    }
20
21 1
    private function createClient()
22
    {
23 1
        $sdk = new Sdk([
24 1
            'region' => getenv('AWS_SDK_REGION'),
25 1
            'version' => getenv('AWS_SDK_VERSION'),
26
            'http' => [
27 1
                'verify' => getenv('AWS_SDK_HTTP_VERIFY') === 'false' ? false : getenv('AWS_SDK_HTTP_VERIFY'),
28
            ]
29
        ]);
30 1
        return $sdk->createDynamoDb();
31
    }
32
33 1
    private function getFullDatabaseName()
34
    {
35 1
        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
            $resourceName,
42
            $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
            $resourceName,
52
            $primaryKey,
53 4
            $this->client,
54 4
            $this->databaseName
55 4
        ))->post($newResource);
56
    }
57
58 3
    public function put($resourceName, $primaryKey, $resourceId, $newResource)
59
    {
60 3
        return (new Resource(
61
            $resourceName,
62
            $primaryKey,
63 3
            $this->client,
64 3
            $this->databaseName
65 3
        ))->put($resourceId, $newResource);
66
    }
67
68 2
    public function delete($resourceName, $primaryKey, $resourceId)
69
    {
70 2
        return (new Resource(
71
            $resourceName,
72
            $primaryKey,
73 2
            $this->client,
74 2
            $this->databaseName
75 2
        ))->delete($resourceId);
76
    }
77
78 5
    public function getDatabaseName()
79
    {
80 5
        return $this->databaseName;
81
    }
82
83 6
    public function getClient()
84
    {
85 6
        return $this->client;
86
    }
87
}
88