Completed
Push — master ( 89c61c...e15fb0 )
by Guillermo A.
01:59
created

CreateTableRequest::setSortKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 2
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
     * @var array The tags.
41
     */
42
    private $tags = [];
43
44
    /**
45
     * Registers the JSON Marshaler, table name, and key schema with this object.
46
     *
47
     * @param Marshaler $marshaler The JSON Marshaler.
48
     * @param string $tableName The table name.
49
     * @param array $keySchema OPTIONAL The key schema.
50
     */
51 16
    public function __construct(Marshaler $marshaler, string $tableName, array $keySchema = [])
52
    {
53 16
        parent::__construct($marshaler, $tableName);
54 16
        if (!empty($keySchema)) {
55 13
            $this->setKeySchema($keySchema);
56
        }
57 16
    }
58
59
    /**
60
     * Registers the partition key.
61
     *
62
     * @param string $name The name of the partition key.
63
     * @param string $attributeType The attribute type.
64
     * @return CreateTableRequest This object.
65
     */
66 1
    public function setPartitionKey(string $name, string $attributeType): CreateTableRequest
67
    {
68 1
        $this->setKeySchema([
69 1
            $name => [
70 1
                'attributeType' => $attributeType,
71
                'keyType' => KeyTypes::HASH
72
            ]
73
        ]);
74 1
        return $this;
75
    }
76
77
    /**
78
     * Registers the sort key.
79
     *
80
     * @param string $name The name of the sort key.
81
     * @param string $attributeType The attribute type.
82
     * @return CreateTableRequest This object.
83
     */
84 1
    public function setSortKey(string $name, string $attributeType): CreateTableRequest
85
    {
86 1
        $this->setKeySchema([
87 1
            $name => [
88 1
                'attributeType' => $attributeType,
89
                'keyType' => KeyTypes::RANGE
90
            ]
91
        ]);
92 1
        return $this;
93
    }
94
95
    /**
96
     * Registers the key schema and attribute definitions.
97
     *
98
     * The key schema argument should be an associative array with the following keys:
99
     *
100
     * $keySchema = [
101
     *      'MyAttribute' => [ // this is the name of your attribute
102
     *          'attributeType' => 'S', // this can be one of the AttributeTypes constants
103
     *          'keyType' => 'HASH' // this can be either 'HASH' or 'RANGE' (or one of the KeyTypes constants)
104
     *     ]
105
     * ];
106
     *
107
     * This method will use the information available in the provided array to build the 'KeySchema' and
108
     * 'AttributeDefinitions' arrays needed for table creation requests.
109
     *
110
     * @param array $keySchema The key schema.
111
     * @return CreateTableRequest This object.
112
     */
113 15
    public function setKeySchema(array $keySchema): CreateTableRequest
114
    {
115 15
        foreach ($keySchema as $name => $data) {
116 15
            $this->keySchema[] = [
117 15
                'AttributeName' => $name,
118 15
                'KeyType' => $data['keyType']
119
            ];
120 15
            $this->attributeDefinitions[] = [
121 15
                'AttributeName' => $name,
122 15
                'AttributeType' => $data['attributeType']
123
            ];
124
        }
125 15
        return $this;
126
    }
127
128
    /**
129
     * Registers the maximum number of strongly consistent reads consumed per second.
130
     *
131
     * @param integer $readCapacityUnits The maximum number of strongly consistent reads consumed per second.
132
     * @return CreateTableRequest This object.
133
     */
134 1
    public function setReadCapacityUnits(int $readCapacityUnits): CreateTableRequest
135
    {
136 1
        $this->readCapacityUnits = $readCapacityUnits;
137 1
        return $this;
138
    }
139
140
    /**
141
     * Registers the maximum number of writes consumed per second.
142
     *
143
     * @param integer $writeCapacityUnits The maximum number of writes consumed per second.
144
     * @return CreateTableRequest This object.
145
     */
146 1
    public function setWriteCapacityUnits(int $writeCapacityUnits): CreateTableRequest
147
    {
148 1
        $this->writeCapacityUnits = $writeCapacityUnits;
149 1
        return $this;
150
    }
151
152
    /**
153
     * Registers the billing mode.
154
     *
155
     * @param string $billingMode The billing mode.
156
     * @return CreateTableRequest This object.
157
     */
158 1
    public function setBillingMode(string $billingMode)
159
    {
160 1
        $this->billingMode = $billingMode;
161 1
        return $this;
162
    }
163
164
    /**
165
     * Registers the server-side encryption settings.
166
     *
167
     * @param bool $isEnabled Whether or not SSE is enabled.
168
     * @param string $masterKeyId OPTIONAL The ID of the master key.
169
     * @return CreateTableRequest This object.
170
     */
171 1
    public function setSSESpecification(bool $isEnabled, string $masterKeyId = '')
172
    {
173 1
        $sseSpecification = [];
174 1
        if ($isEnabled) {
175
            $sseSpecification = [
176 1
                'Enabled' => $isEnabled,
177 1
                'SSEType' => 'KMS'
178
            ];
179 1
            if (!empty($masterKeyId)) {
180 1
                $sseSpecification['KMSMasterKeyId'] = $masterKeyId;
181
            }
182
        }
183 1
        $this->sseSpecification = $sseSpecification;
184 1
        return $this;
185
    }
186
187
    /**
188
     * Registers a tag.
189
     *
190
     * @param string $key The tag key.
191
     * @param string $value The tag value.
192
     * @return CreateTableRequest This object.
193
     */
194 1
    public function addTag(string $key, string $value)
195
    {
196 1
        $this->tags[] = [
197 1
            'Key' => $key,
198 1
            'Value' => $value
199
        ];
200 1
        return $this;
201
    }
202
203
    /**
204
     * {@inheritDoc}
205
     */
206 16
    public function get(): array
207
    {
208 16
        $query = parent::get();
209 16
        $query['KeySchema'] = $this->keySchema;
210 16
        $query['AttributeDefinitions'] = $this->attributeDefinitions;
211 16
        $query['BillingMode'] = $this->billingMode;
212 16
        $query['ProvisionedThroughput'] = [
213 16
            'ReadCapacityUnits' => $this->readCapacityUnits,
214 16
            'WriteCapacityUnits' => $this->writeCapacityUnits,
215
        ];
216 16
        if (!empty($this->sseSpecification)) {
217 1
            $query['SSESpecification'] = $this->sseSpecification;
218
        }
219 16
        if (!empty($this->tags)) {
220 1
            $query['Tags'] = $this->tags;
221
        }
222 16
        return $query;
223
    }
224
}
225