Database::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
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
    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 patch($resourceName, $primaryKey, $resourceId, $newResource)
59
    {
60
        return (new Resource(
61
            $resourceName,
62
            $primaryKey,
63
            $this->client,
64
            $this->databaseName
65
        ))->patch($resourceId, $newResource);
66
    }
67
68
    public function put($resourceName, $primaryKey, $resourceId, $newResource)
69
    {
70
        return (new Resource(
71
            $resourceName,
72
            $primaryKey,
73
            $this->client,
74
            $this->databaseName
75
        ))->put($resourceId, $newResource);
76
    }
77
78
    public function delete($resourceName, $primaryKey, $resourceId)
79
    {
80
        return (new Resource(
81
            $resourceName,
82
            $primaryKey,
83
            $this->client,
84
            $this->databaseName
85
        ))->delete($resourceId);
86
    }
87
88
    public function setDatabaseName($databaseName)
89
    {
90
        $this->databaseName = $databaseName;
91
    }
92
93
    public function getDatabaseName()
94
    {
95
        return $this->databaseName;
96
    }
97
98
    public function getClient()
99
    {
100
        return $this->client;
101
    }
102
}
103