Completed
Push — master ( c93690...af98e0 )
by Dmitry
01:49
created

Schema::formatValue()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 6.1368
cc 10
eloc 16
nc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 'STRING':
49
            case 'str':
50
            case 'string':
51
                return (string) $value;
52
53
            case 'float':
54
                return (float) $value;
55
56
            case 'unsigned':
57
            case 'UNSIGNED':
58
            case 'num':
59
            case 'NUM':
60
                return (int) $value;
61
62
            default:
63
                return $value;
64
        }
65
    }
66
67
    public function getSpace($id)
68
    {
69
        if (is_string($id)) {
70
            return $this->getSpace($this->getSpaceId($id));
71
        }
72
73
        if (!$id) {
74
            throw new Exception("Space id or name not defined");
75
        }
76
77
        if (!array_key_exists($id, $this->spaces)) {
78
            $name = array_search($id, $this->names);
79
            $meta = array_key_exists($id, $this->params) ? $this->params[$id] : null;
80
            $this->spaces[$id] = new Space($this->mapper, $id, $name, $meta);
81
        }
82
        return $this->spaces[$id];
83
    }
84
85
    public function getSpaceId($name)
86
    {
87
        if (!$this->hasSpace($name)) {
88
            throw new Exception("No space $name");
89
        }
90
        return $this->names[$name];
91
    }
92
93
    public function getSpaces()
94
    {
95
        foreach ($this->names as $id) {
96
            $this->getSpace($id);
97
        }
98
        return $this->spaces;
99
    }
100
101
    public function hasSpace($name)
102
    {
103
        return array_key_exists($name, $this->names);
104
    }
105
106
    public function once($name, $callback)
107
    {
108
        $key = 'once' . $name;
109
110
        $rows = $this->mapper->find('_schema', ['key' => $key]);
111
        if (!count($rows)) {
112
            $this->mapper->create('_schema', ['key' => $key]);
113
            return $callback($this->mapper);
114
        }
115
    }
116
117
    public function reset()
118
    {
119
        $this->names = $this->mapper->getClient()->evaluate("
120
            local spaces = {}
121
            local i, s
122
            for i, s in box.space._vspace:pairs() do
123
                spaces[s[3]] = s[1]
124
            end
125
            return spaces
126
        ")->getData()[0];
127
    }
128
129
    public function getMeta()
130
    {
131
        $params = [];
132
        foreach ($this->getSpaces() as $space) {
133
            $params[$space->getId()] = $space->getMeta();
134
        }
135
136
        return [
137
            'names' => $this->names,
138
            'params' => $params
139
        ];
140
    }
141
}
142