Completed
Push — master ( c8b0a5...59e33a )
by Dmitry
01:58
created

Schema::once()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Exception;
6
7
class Schema
8
{
9
    private $mapper;
10
11
    private $names = [];
12
    private $spaces = [];
13
    private $params = [];
14
15
    public function __construct(Mapper $mapper, $meta = null)
16
    {
17
        $this->mapper = $mapper;
18
        if ($meta) {
19
            $this->names = $meta['names'];
20
            $this->params = $meta['params'];
21
        } else {
22
            $this->reset();
23
        }
24
    }
25
26
    public function createSpace($space, $properties = null)
27
    {
28
        $id = $this->mapper->getClient()->evaluate("
29
            box.schema.space.create('$space')
30
            return box.space.$space.id
31
        ")->getData()[0];
32
33
        $this->names[$space] = $id;
34
35
        $this->spaces[$id] = new Space($this->mapper, $id, $space);
36
37
        if ($properties) {
38
            $this->spaces[$id]->addProperties($properties);
39
        }
40
41
        return $this->spaces[$id];
42
    }
43
44
    public function formatValue($type, $value)
45
    {
46
        switch ($type) {
47
            case 'STR':
48
            case 'str':
49
                return (string) $value;
50
51
            case 'unsigned':
52
            case 'UNSIGNED':
53
            case 'num':
54
            case 'NUM':
55
                return (int) $value;
56
57
            default:
58
                return $value;
59
        }
60
    }
61
62
    public function getSpace($id)
63
    {
64
        if (is_string($id)) {
65
            return $this->getSpace($this->getSpaceId($id));
66
        }
67
68
        if (!$id) {
69
            throw new Exception("Space id or name not defined");
70
        }
71
72
        if (!array_key_exists($id, $this->spaces)) {
73
            $name = array_search($id, $this->names);
74
            $meta = array_key_exists($id, $this->params) ? $this->params[$id] : null;
75
            $this->spaces[$id] = new Space($this->mapper, $id, $name, $meta);
76
        }
77
        return $this->spaces[$id];
78
    }
79
80
    public function getSpaceId($name)
81
    {
82
        if (!$this->hasSpace($name)) {
83
            throw new Exception("No space $name");
84
        }
85
        return $this->names[$name];
86
    }
87
88
    public function getSpaces()
89
    {
90
        foreach ($this->names as $id) {
91
            $this->getSpace($id);
92
        }
93
        return $this->spaces;
94
    }
95
96
    public function hasSpace($name)
97
    {
98
        return array_key_exists($name, $this->names);
99
    }
100
101
    public function once($name, $callback)
102
    {
103
        $key = 'once' . $name;
104
105
        $rows = $this->mapper->find('_schema', ['key' => $key]);
106
        if (!count($rows)) {
107
            $this->mapper->create('_schema', ['key' => $key]);
108
            return $callback($this->mapper);
109
        }
110
    }
111
112
    public function reset()
113
    {
114
        $this->names = $this->mapper->getClient()->evaluate("
115
            local spaces = {}
116
            local i, s
117
            for i, s in box.space._space:pairs() do
118
                spaces[s[3]] = s[1]
119
            end
120
            return spaces
121
        ")->getData()[0];
122
    }
123
124
    public function getMeta()
125
    {
126
        $params = [];
127
        foreach ($this->getSpaces() as $space) {
128
            $params[$space->getId()] = $space->getMeta();
129
        }
130
131
        return [
132
            'names' => $this->names,
133
            'params' => $params
134
        ];
135
    }
136
}
137