Completed
Push — master ( 5accd6...21cd89 )
by Guillermo A.
02:00
created

CreateTableRequest::setBillingMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Guillermoandrae\Db\DynamoDb;
4
5
use Aws\DynamoDb\Marshaler;
6
7
final class CreateTableRequest extends AbstractTableAwareRequest
8
{
9
    /**
10
     * @var array Attributes describing the key schema.
11
     */
12
    private $attributeDefinitions = [];
13
14
    /**
15
     * @var array The primary key.
16
     */
17
    private $keySchema = [];
18
    
19
    /**
20
     * @var int The maximum number of strongly consistent reads consumed per second.
21
     */
22
    private $readCapacityUnits = 5;
23
24
    /**
25
     * @var int The maximum number of writes consumed per second.
26
     */
27
    private $writeCapacityUnits = 5;
28
29
    /**
30
     * @var string The billing mode.
31
     */
32
    private $billingMode = BillingModes::PROVISIONED;
33
34
    /**
35
     * @var array The server-side encryption settings.
36
     */
37
    private $sseSpecification;
38
39
    /**
40
     * Registers the JSON Marshaler, table name, and key schema with this object.
41
     *
42
     * @param Marshaler $marshaler The JSON Marshaler.
43
     * @param string $tableName The table name.
44
     * @param array $keySchema OPTIONAL The key schema.
45
     */
46 15
    public function __construct(Marshaler $marshaler, string $tableName, array $keySchema = [])
47
    {
48 15
        parent::__construct($marshaler, $tableName);
49 15
        if (!empty($keySchema)) {
50 12
            $this->setKeySchema($keySchema);
51
        }
52 15
    }
53
54
    /**
55
     * Registers the partition key.
56
     *
57
     * @param string $name The name of the partition key.
58
     * @param string $attributeType The attribute type.
59
     * @return CreateTableRequest This object.
60
     */
61 1
    public function setPartitionKey(string $name, string $attributeType): CreateTableRequest
62
    {
63 1
        $this->setKeySchema([
64 1
            $name => [
65 1
                'attributeType' => $attributeType,
66
                'keyType' => KeyTypes::HASH
67
            ]
68
        ]);
69 1
        return $this;
70
    }
71
72
    /**
73
     * Registers the sort key.
74
     *
75
     * @param string $name The name of the sort key.
76
     * @param string $attributeType The attribute type.
77
     * @return CreateTableRequest This object.
78
     */
79 1
    public function setSortKey(string $name, string $attributeType): CreateTableRequest
80
    {
81 1
        $this->setKeySchema([
82 1
            $name => [
83 1
                'attributeType' => $attributeType,
84
                'keyType' => KeyTypes::RANGE
85
            ]
86
        ]);
87 1
        return $this;
88
    }
89
90
    /**
91
     * Registers the key schema and attribute definitions.
92
     *
93
     * The key schema argument should be an associative array with the following keys:
94
     *
95
     * $keySchema = [
96
     *      'MyAttribute' => [ // this is the name of your attribute
97
     *          'attributeType' => 'S', // this can be one of the AttributeTypes constants
98
     *          'keyType' => 'HASH' // this can be either 'HASH' or 'RANGE' (or one of the KeyTypes constants)
99
     *     ]
100
     * ];
101
     *
102
     * This method will use the information available in the provided array to build the 'KeySchema' and
103
     * 'AttributeDefinitions' arrays needed for table creation requests.
104
     *
105
     * @param array $keySchema The key schema.
106
     * @return CreateTableRequest This object.
107
     */
108 14
    public function setKeySchema(array $keySchema): CreateTableRequest
109
    {
110 14
        foreach ($keySchema as $name => $data) {
111 14
            $this->keySchema[] = [
112 14
                'AttributeName' => $name,
113 14
                'KeyType' => $data['keyType']
114
            ];
115 14
            $this->attributeDefinitions[] = [
116 14
                'AttributeName' => $name,
117 14
                'AttributeType' => $data['attributeType']
118
            ];
119
        }
120 14
        return $this;
121
    }
122
123
    /**
124
     * Registers the maximum number of strongly consistent reads consumed per second.
125
     *
126
     * @param integer $readCapacityUnits The maximum number of strongly consistent reads consumed per second.
127
     * @return CreateTableRequest This object.
128
     */
129 1
    public function setReadCapacityUnits(int $readCapacityUnits): CreateTableRequest
130
    {
131 1
        $this->readCapacityUnits = $readCapacityUnits;
132 1
        return $this;
133
    }
134
135
    /**
136
     * Registers the maximum number of writes consumed per second.
137
     *
138
     * @param integer $writeCapacityUnits The maximum number of writes consumed per second.
139
     * @return CreateTableRequest This object.
140
     */
141 1
    public function setWriteCapacityUnits(int $writeCapacityUnits): CreateTableRequest
142
    {
143 1
        $this->writeCapacityUnits = $writeCapacityUnits;
144 1
        return $this;
145
    }
146
147
    /**
148
     * Registers the billing mode.
149
     *
150
     * @param string $billingMode The billing mode.
151
     * @return CreateTableRequest This object.
152
     */
153 1
    public function setBillingMode(string $billingMode)
154
    {
155 1
        $this->billingMode = $billingMode;
156 1
        return $this;
157
    }
158
159
    /**
160
     * Registers the server-side encryption settings.
161
     *
162
     * @param bool $isEnabled Whether or not SSE is enabled.
163
     * @param string $masterKeyId OPTIONAL The ID of the master key.
164
     * @return CreateTableRequest This object.
165
     */
166 1
    public function setSSESpecification(bool $isEnabled, string $masterKeyId = '')
167
    {
168 1
        if ($isEnabled) {
169
            $sseSpecification = [
170 1
                'Enabled' => $isEnabled,
171 1
                'SSEType' => 'KMS'
172
            ];
173 1
            if (!empty($masterKeyId)) {
174 1
                $sseSpecification['KMSMasterKeyId'] = $masterKeyId;
175
            }
176 1
            $this->sseSpecification = $sseSpecification;
177
        }
178 1
        return $this;
179
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184 15
    public function get(): array
185
    {
186 15
        $query = parent::get();
187 15
        $query['KeySchema'] = $this->keySchema;
188 15
        $query['AttributeDefinitions'] = $this->attributeDefinitions;
189 15
        $query['ProvisionedThroughput'] = [
190 15
            'ReadCapacityUnits' => $this->readCapacityUnits,
191 15
            'WriteCapacityUnits' => $this->writeCapacityUnits,
192
        ];
193 15
        $query['BillingMode'] = $this->billingMode;
194 15
        if (!empty($this->sseSpecification)) {
195 1
            $query['SSESpecification'] = $this->sseSpecification;
196
        }
197 15
        return $query;
198
    }
199
}
200