Passed
Push — master ( d9c8cf...7342b9 )
by Dmitry
02:37
created

SchemaManager::validateOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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 13
    public function validateOptions()
23
    {
24 13
        if ($this->engine == '') {
25 1
            throw new InvalidArgumentException("Engine should be defined");
26
        }
27 13
        if ($this->space == '') {
28 1
            throw new InvalidArgumentException("Space should be defined");
29
        }
30 13
    }
31
    /**
32
     * setup database schema
33
     */
34 13
    public function setup()
35
    {
36 13
        $this->client->evaluate('box.schema.create_space(...)', $this->space, [
37 13
            '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 13
        $this->client->call("box.space.{$this->space}:create_index", "key", [
56 13
            'type' => 'hash',
57
            'if_not_exists' => true,
58
            'unique' => true,
59
            'parts' => ['key'],
60
        ]);
61
62 13
        $this->client->call("box.space.{$this->space}:create_index", "token_key", [
63 13
            'type' => 'hash',
64
            'if_not_exists' => true,
65
            'unique' => true,
66
            'parts' => ['token', 'key'],
67
        ]);
68
69 13
        $this->client->call("box.space.{$this->space}:create_index", "expire", [
70 13
            'type' => 'tree',
71
            'if_not_exists' => true,
72
            'unique' => false,
73
            'parts' => ['expire'],
74
        ]);
75 13
    }
76
77
    /**
78
     * rollback lock store schema
79
     */
80 13
    public function tearDown()
81
    {
82
        $script = <<<LUA
83 13
        if box.space.$this->space then
84 13
            box.space.$this->space:drop()
85
        end
86
        LUA;
87
88 13
        $this->client->evaluate($script);
89 13
    }
90
}
91