Cleaner   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 48
ccs 15
cts 15
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 1
A validateOptions() 0 11 3
1
<?php
2
3
namespace Tarantool\SymfonyLock;
4
5
use Tarantool\Client\Client;
6
use InvalidArgumentException;
7
8
class Cleaner
9
{
10
    use OptionalConstructor;
11
12
    /**
13
     * Dropped rows counter limit
14
     */
15
    protected int $limit = 100;
16
17
    /**
18
     * Space name
19
     */
20
    protected string $space = 'lock';
21
22 4
    protected function validateOptions()
23
    {
24 4
        if ($this->limit <= 0) {
25 1
            $message = sprintf(
26 1
                'Limit expects a strictly positive. Got %d.',
27 1
                $this->limit
28
            );
29 1
            throw new InvalidArgumentException($message);
30
        }
31 3
        if ($this->space == '') {
32 1
            throw new InvalidArgumentException("Space should be defined");
33
        }
34 2
    }
35
36 2
    public function process(): int
37
    {
38
        $script = <<<LUA
39 2
        local limit, timestamp = ...
40
        local counter = 0
41
        box.begin()
42 2
        box.space.$this->space.index.expire:pairs()
43
            :take_while(function(tuple) return counter < limit end)
44
            :take_while(function(tuple) return tuple.expire <= timestamp end)
45
            :each(function(tuple)
46 2
                box.space.$this->space:delete( tuple.key )
47
                counter = counter + 1
48
            end)
49
        box.commit()
50
        return counter
51
        LUA;
52
53 2
        [$counter] = $this->client->evaluate($script, $this->limit, microtime(true));
54
55 2
        return $counter;
56
    }
57
}
58