Passed
Push — master ( 73aa64...4abb8f )
by
unknown
10:14 queued 07:42
created

Database::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 3
nc 4
nop 2
crap 3
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 21
    public function __construct(Client $client = null, $databaseName = null)
16
    {
17 21
        $this->client = $client === null ? $this->createClient() : $client;
18 21
        $this->databaseName = $databaseName === null ? $this->getFullDatabaseName() : $databaseName;
19 21
    }
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 3
    public function put($resourceName, $primaryKey, $resourceId, $newResource)
59
    {
60 3
        return (new Resource(
61 3
            $resourceName,
62 3
            $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 2
            $resourceName,
72 2
            $primaryKey,
73 2
            $this->client,
74 2
            $this->databaseName
75 2
        ))->delete($resourceId);
76
    }
77
78 1
    public function setDatabaseName($databaseName)
79
    {
80 1
        $this->databaseName = $databaseName;
81 1
    }
82
83 8
    public function getDatabaseName()
84
    {
85 8
        return $this->databaseName;
86
    }
87
88 7
    public function getClient()
89
    {
90 7
        return $this->client;
91
    }
92
}
93