Completed
Push — master ( 8fe865 )
by Dmitry
04:10
created

Schema   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 94
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createSpace() 0 11 1
A formatValue() 0 8 3
A getSpace() 0 15 4
A getSpaceId() 0 7 2
A getSpaces() 0 7 2
A hasSpace() 0 4 1
A once() 0 10 2
A reset() 0 11 1
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
14
    public function __construct(Mapper $mapper)
15
    {
16
        $this->mapper = $mapper;
17
        $this->reset();
18
    }
19
20
    public function createSpace($space)
21
    {
22
        $id = $this->mapper->getClient()->evaluate("
23
            box.schema.space.create('$space')
24
            return box.space.$space.id
25
        ")->getData()[0];
26
27
        $this->names[$space] = $id;
28
29
        return $this->spaces[$id] = new Space($this->mapper, $id, $space);
30
    }
31
32
    public function formatValue($type, $value)
33
    {
34
        switch($type) {
35
            case 'str': return (string) $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
36
            case 'unsigned': return (int) $value;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
37
            default: return $value;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
38
        }
39
    }
40
41
    public function getSpace($id)
42
    {
43
        if(is_string($id)) {
44
            return $this->getSpace($this->getSpaceId($id));
45
        }
46
47
        if(!$id) {
48
            throw new Exception("Space id or name not defined");
49
        }
50
51
        if(!array_key_exists($id, $this->spaces)) {
52
            $this->spaces[$id] = new Space($this->mapper, $id, array_search($id, $this->names));
53
        }
54
        return $this->spaces[$id];
55
    }
56
57
    public function getSpaceId($name)
58
    {
59
        if(!$this->hasSpace($name)) {
60
            throw new Exception("No space $id");
61
        }
62
        return $this->names[$name];
63
    }
64
65
    public function getSpaces()
66
    {
67
        foreach($this->names as $id) {
68
            $this->getSpace($id);
69
        }
70
        return $this->spaces;
71
    }
72
73
    public function hasSpace($name)
74
    {
75
        return array_key_exists($name, $this->names);
76
    }
77
78
    public function once($name, $callback)
79
    {
80
        $key = 'once' . $name;
81
82
        $rows = $this->mapper->find('_schema', ['key' => $key]);
83
        if(!count($rows)) {
84
            $this->mapper->create('_schema', ['key' => $key]);
85
            return $callback($this->mapper);
86
        }
87
    }
88
89
    public function reset()
90
    {
91
        $this->names = $this->mapper->getClient()->evaluate("
92
            local spaces = {}
93
            local i, s
94
            for i, s in box.space._space:pairs() do
95
                spaces[s[3]] = s[1]
96
            end
97
            return spaces"
98
        )->getData()[0];
99
    }
100
}