Completed
Push — master ( 20a668...02e94f )
by Guillermo A.
02:17
created

AbstractFilterExpressionAwareRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 32
dl 0
loc 69
c 0
b 0
f 0
ccs 30
cts 30
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 3
B parseExpression() 0 19 8
A setFilterExpression() 0 9 2
1
<?php
2
3
namespace Guillermoandrae\DynamoDb;
4
5
use ErrorException;
6
7
abstract class AbstractFilterExpressionAwareRequest extends AbstractItemRequest
8
{
9
    use LimitAwareRequestTrait;
10
11
    /**
12
     * @var string The filter expression.
13
     */
14
    protected $filterExpression;
15
16
    /**
17
     * Registers the filter expression with this object.
18
     *
19
     * @param array $data The filter expression data.
20
     * @return AbstractFilterExpressionAwareRequest An implementation of this abstract.
21
     * @throws ErrorException
22
     */
23 6
    final public function setFilterExpression(array $data): AbstractFilterExpressionAwareRequest
24
    {
25 6
        $filterExpressionArray = [];
26 6
        foreach ($data as $key => $options) {
27 6
            $filterExpressionArray[] = $this->parseExpression($options['operator'], $key);
28 5
            $this->addExpressionAttributeValue($key, $options['value']);
29
        }
30 5
        $this->filterExpression = implode(' and ', $filterExpressionArray);
31 5
        return $this;
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37 21
    public function get(): array
38
    {
39 21
        $query = parent::get();
40 21
        if ($this->limit) {
41 3
            $query['Limit'] = $this->limit;
42
        }
43 21
        if ($this->filterExpression) {
44 5
            $query['FilterExpression'] = $this->filterExpression;
45
        }
46 21
        return $query;
47
    }
48
49
    /**
50
     * Uses the operator to build the filter expression.
51
     *
52
     * @param string $operator The request operator.
53
     * @param string $key The attribute key.
54
     * @return string The expression.
55
     * @throws ErrorException
56
     */
57 9
    protected function parseExpression(string $operator, string $key): string
58
    {
59
        switch ($operator) {
60 9
            case RequestOperators::BEGINS_WITH:
61 1
                return sprintf('begins_with(%s, :%s)', $key, $key);
62 9
            case RequestOperators::CONTAINS:
63 1
                return sprintf('contains(%s, :%s)', $key, $key);
64 9
            case RequestOperators::EQ:
65 4
                return sprintf('%s = :%s', $key, $key);
66 7
            case RequestOperators::GT:
67 1
                return sprintf('%s > :%s', $key, $key);
68 6
            case RequestOperators::GTE:
69 3
                return sprintf('%s >= :%s', $key, $key);
70 3
            case RequestOperators::LT:
71 1
                return sprintf('%s < :%s', $key, $key);
72 2
            case RequestOperators::LTE:
73 1
                return sprintf('%s <= :%s', $key, $key);
74
            default:
75 1
                throw new ErrorException('The provided operator is not supported.');
76
        }
77
    }
78
}
79