Passed
Push — develop ( 60641a...1612eb )
by Csaba
58s
created

DatabaseOperation::execute()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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
    public function __construct(DynamoDbClient $client, $query)
16
    {
17
        $this->client = $client;
18
        $this->query = $query;
19
    }
20
21
    public function next()
22
    {
23
        if (!$this->first && $this->lastEvaluatedKey === null) {
24
            return null;
25
        }
26
        $this->first = false;
27
        $query = $this->setExclusiveStartKey($this->query);
28
        $result = $this->execute($query);
29
        $this->setLastEvaluatedKey($result);
30
        return $result;
31
    }
32
33
    abstract protected function execute($query);
34
35
    protected function setExclusiveStartKey($query)
36
    {
37
        if ($this->lastEvaluatedKey !== null) {
38
            $query['ExclusiveStartKey'] = $this->lastEvaluatedKey;
39
        }
40
        return $query;
41
    }
42
43
    protected function setLastEvaluatedKey($result)
44
    {
45
        $this->lastEvaluatedKey = null;
46
        if (!empty($result['LastEvaluatedKey'])) {
47
            $this->lastEvaluatedKey = $result['LastEvaluatedKey'];
48
        }
49
    }
50
}
51