SchemaManager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 81
ccs 20
cts 20
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 40 1
A tearDown() 0 9 1
A validateOptions() 0 7 3
1
<?php
2
3
namespace Tarantool\SymfonyLock;
4
5
use Tarantool\Client\Client;
6
use InvalidArgumentException;
7
8
class SchemaManager
9
{
10
    use OptionalConstructor;
11
12
    /**
13
     * what engine should be used
14
     */
15
    protected string $engine = 'memtx';
16
17
    /**
18
     * space name
19
     */
20
    protected string $space = 'lock';
21
22 17
    public function validateOptions()
23
    {
24 17
        if ($this->engine == '') {
25 1
            throw new InvalidArgumentException("Engine should be defined");
26
        }
27 17
        if ($this->space == '') {
28 1
            throw new InvalidArgumentException("Space should be defined");
29
        }
30 17
    }
31
    /**
32
     * setup database schema
33
     */
34 17
    public function setup()
35
    {
36 17
        $this->client->evaluate('box.schema.create_space(...)', $this->space, [
37 17
            'engine' => $this->engine,
38
            'if_not_exists' => true,
39
            'format' => [
40
                [
41
                    'name' => 'key',
42
                    'type' => 'string',
43
                ],
44
                [
45
                    'name' => 'token',
46
                    'type' => 'string',
47
                ],
48
                [
49
                    'name' => 'expire',
50
                    'type' => 'number',
51
                ],
52
            ],
53
        ]);
54
55 17
        $this->client->call("box.space.{$this->space}:create_index", "key", [
56 17
            'type' => 'hash',
57
            'if_not_exists' => true,
58
            'unique' => true,
59
            'parts' => ['key'],
60
        ]);
61
62 17
        $this->client->call("box.space.{$this->space}:create_index", "token_key", [
63 17
            'type' => 'hash',
64
            'if_not_exists' => true,
65
            'unique' => true,
66
            'parts' => ['token', 'key'],
67
        ]);
68
69 17
        $this->client->call("box.space.{$this->space}:create_index", "expire", [
70 17
            'type' => 'tree',
71
            'if_not_exists' => true,
72
            'unique' => false,
73
            'parts' => ['expire'],
74
        ]);
75 17
    }
76
77
    /**
78
     * rollback lock store schema
79
     */
80 17
    public function tearDown()
81
    {
82
        $script = <<<LUA
83 17
        if box.space.$this->space then
84 17
            box.space.$this->space:drop()
85
        end
86
        LUA;
87
88 17
        $this->client->evaluate($script);
89 17
    }
90
}
91