AbstractSearchOperation::__construct()   A
last analyzed

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 3
crap 1
1
<?php
2
3
namespace Guillermoandrae\DynamoDb\Contract;
4
5
use Aws\DynamoDb\DynamoDbClient;
6
use Aws\DynamoDb\Marshaler;
7
8
/**
9
 * Abstract for search operations.
10
 *
11
 * @author Guillermo A. Fisher <[email protected]>
12
 */
13
abstract class AbstractSearchOperation extends AbstractOperation implements SearchOperationInterface
14
{
15
    use TableAwareOperationTrait,
16
        LimitAwareOperationTrait,
17
        ExpressionAttributeValueAwareOperationTrait,
18
        ReturnConsumedCapacityAwareOperationTrait {
19
        TableAwareOperationTrait::toArray as tableAwareTraitToArray;
20
        LimitAwareOperationTrait::toArray as limitAwareTraitToArray;
21
        ExpressionAttributeValueAwareOperationTrait::toArray as expressionAwareTraitToArray;
22
        ReturnConsumedCapacityAwareOperationTrait::toArray as returnConsumedCapacityAwareTraitToArray;
23
    }
24
25
    /**
26
     * @var boolean Whether or not the read should be consistent.
27
     */
28
    protected bool $consistentRead = false;
29
30
    /**
31
     * @var string The name of a secondary index to request against.
32
     */
33
    protected string $indexName = '';
34
35
    /**
36
     * @var string The attributes to retrieve from the specified table or index.
37
     */
38
    protected string $projectionExpression = '';
39
40
    /**
41
     * @var string The attributes to be returned in the result.
42
     */
43
    protected string $select = '';
44
45
    /**
46
     * Registers the DynamoDb client, Marshaler, and the table name with this object.
47
     *
48
     * @param DynamoDbClient $client The DynamoDb client.
49
     * @param Marshaler $marshaler The Marshaler.
50
     * @param string $tableName The table name.
51
     */
52 22
    public function __construct(DynamoDbClient $client, Marshaler $marshaler, string $tableName)
53
    {
54 22
        parent::__construct($client, $marshaler);
55 22
        $this->setTableName($tableName);
56 22
    }
57
58 1
    final public function setConsistentRead(bool $consistentRead): SearchOperationInterface
59
    {
60 1
        $this->consistentRead = $consistentRead;
61 1
        return $this;
62
    }
63
64 1
    final public function setIndexName(string $indexName): SearchOperationInterface
65
    {
66 1
        $this->indexName = $indexName;
67 1
        return $this;
68
    }
69
70 1
    final public function setSelect(string $select): SearchOperationInterface
71
    {
72 1
        $this->select = $select;
73 1
        return $this;
74
    }
75
76 1
    final public function setProjectionExpression(string $projectionExpression): SearchOperationInterface
77
    {
78 1
        $this->projectionExpression = $projectionExpression;
79 1
        return $this;
80
    }
81
82 21
    public function toArray(): array
83
    {
84 21
        $operation = $this->tableAwareTraitToArray();
85 21
        if ($this->offset || $this->limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
86 2
            $operation += $this->limitAwareTraitToArray();
87
        }
88 21
        $operation += $this->returnConsumedCapacityAwareTraitToArray();
89 21
        $operation += $this->expressionAwareTraitToArray();
90 21
        $operation['ConsistentRead'] = $this->consistentRead;
91 21
        if (!empty($this->indexName)) {
92 1
            $operation['IndexName'] = $this->indexName;
93
        }
94 21
        if (!empty($this->select)) {
95 1
            $operation['Select'] = $this->select;
96
        }
97 21
        if (!empty($this->projectionExpression)) {
98 1
            $operation['ProjectionExpression'] = $this->projectionExpression;
99
        }
100 21
        return $operation;
101
    }
102
}
103