Completed
Push — master ( 0aa025...c02e16 )
by Guillermo A.
02:37
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
     * Registers the JSON Marshaler, table name, and key schema with this object.
31
     *
32
     * @param Marshaler $marshaler The JSON Marshaler.
33
     * @param string $tableName The table name.
34
     * @param array $keySchema OPTIONAL The key schema.
35
     */
36 13
    public function __construct(Marshaler $marshaler, string $tableName, array $keySchema = [])
37
    {
38 13
        parent::__construct($marshaler, $tableName);
39 13
        if ($keySchema) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keySchema of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40 10
            $this->setKeySchema($keySchema);
41
        }
42 13
    }
43
44
    /**
45
     * Registers the partition key.
46
     *
47
     * @param string $name The name of the partition key.
48
     * @param string $attributeType The attribute type.
49
     * @return CreateTableRequest This object.
50
     */
51 1
    public function setPartitionKey(string $name, string $attributeType): CreateTableRequest
52
    {
53 1
        $this->setKeySchema([
54 1
            $name => [
55 1
                'type' => $attributeType,
56
                'keyType' => KeyTypes::HASH
57
            ]
58
        ]);
59 1
        return $this;
60
    }
61
62
    /**
63
     * Registers the sort key.
64
     *
65
     * @param string $name The name of the sort key.
66
     * @param string $attributeType The attribute type.
67
     * @return CreateTableRequest This object.
68
     */
69 1
    public function setSortKey(string $name, string $attributeType): CreateTableRequest
70
    {
71 1
        $this->setKeySchema([
72 1
            $name => [
73 1
                'type' => $attributeType,
74
                'keyType' => KeyTypes::RANGE
75
            ]
76
        ]);
77 1
        return $this;
78
    }
79
80
    /**
81
     * Registers the key schema and attribute definitions.
82
     *
83
     * @param array $keySchema The key schema.
84
     * @return CreateTableRequest This object.
85
     */
86 12
    public function setKeySchema(array $keySchema): CreateTableRequest
87
    {
88 12
        foreach ($keySchema as $name => $data) {
89 12
            $this->keySchema[] = [
90 12
                'AttributeName' => $name,
91 12
                'KeyType' => $data['keyType']
92
            ];
93 12
            $this->attributeDefinitions[] = [
94 12
                'AttributeName' => $name,
95 12
                'AttributeType' => $data['type']
96
            ];
97
        }
98 12
        return $this;
99
    }
100
101
    /**
102
     * Registers the maximum number of strongly consistent reads consumed per second.
103
     *
104
     * @param integer $readCapacityUnits The maximum number of strongly consistent reads consumed per second.
105
     * @return CreateTableRequest This object.
106
     */
107 1
    public function setReadCapacityUnits(int $readCapacityUnits): CreateTableRequest
108
    {
109 1
        $this->readCapacityUnits = $readCapacityUnits;
110 1
        return $this;
111
    }
112
113
    /**
114
     * Registers the maximum number of writes consumed per second.
115
     *
116
     * @param integer $writeCapacityUnits The maximum number of writes consumed per second.
117
     * @return CreateTableRequest This object.
118
     */
119 1
    public function setWriteCapacityUnits(int $writeCapacityUnits): CreateTableRequest
120
    {
121 1
        $this->writeCapacityUnits = $writeCapacityUnits;
122 1
        return $this;
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 13
    public function get(): array
129
    {
130 13
        $query = parent::get();
131 13
        $query['KeySchema'] = $this->keySchema;
132 13
        $query['AttributeDefinitions'] = $this->attributeDefinitions;
133 13
        $query['ProvisionedThroughput'] = [
134 13
            'ReadCapacityUnits' => $this->readCapacityUnits,
135 13
            'WriteCapacityUnits' => $this->writeCapacityUnits,
136
        ];
137 13
        return $query;
138
    }
139
}
140