Passed
Push — master ( 7671ca...14da8a )
by Csaba
44s
created

DatabaseOperation::setExclusiveStartKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace Fathomminds\Rest\Database\DynamoDb;
3
4
use Aws\DynamoDb\DynamoDbClient;
5
use Aws\DynamoDb\Marshaler;
6
7
abstract class DatabaseOperation
8
{
9
    protected $client;
10
    protected $lastEvaluatedKey;
11
    protected $query;
12
    protected $result;
13
    protected $first = true;
14
15 8
    public function __construct(DynamoDbClient $client, $query)
16
    {
17 8
        $this->client = $client;
18 8
        $this->query = $query;
19 8
    }
20
21 7
    public function next()
22
    {
23 7
        if (!$this->first && $this->lastEvaluatedKey === null) {
24 5
            return null;
25
        }
26 7
        $this->first = false;
27 7
        $query = $this->setExclusiveStartKey($this->query);
28 7
        $result = $this->execute($query);
29 7
        $this->setLastEvaluatedKey($result);
30 7
        return $result;
31
    }
32
33
    abstract protected function execute($query);
34
35 7
    protected function setExclusiveStartKey($query)
36
    {
37 7
        if ($this->lastEvaluatedKey !== null) {
38 1
            $query['ExclusiveStartKey'] = $this->lastEvaluatedKey;
39
        }
40 7
        return $query;
41
    }
42
43 7
    protected function setLastEvaluatedKey($result)
44
    {
45 7
        $this->lastEvaluatedKey = null;
46 7
        if (!empty($result['LastEvaluatedKey'])) {
47 1
            $this->lastEvaluatedKey = $result['LastEvaluatedKey'];
48
        }
49 7
    }
50
}
51