Test Failed
Branch feature/dynamodb (336f9f)
by Csaba
05:26
created

Database::getDatabaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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