Completed
Push — master ( 229d8c...e7f8de )
by Dmitry
02:48
created

Schema::getSpaceId()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4.3244

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 8
cts 11
cp 0.7272
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 6
nop 1
crap 4.3244
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Client\Client;
6
use Tarantool\Mapper\Contracts;
7
use Tarantool\Client\Schema\Space;
8
use Tarantool\Client\Schema\Index;
9
10
class Schema implements Contracts\Schema
11
{
12
    protected $client;
13
    protected $spaceSpace;
14
    protected $indexSpace;
15
    protected $spaceId = [];
16
17 64
    public function __construct(Client $client, array $data = null)
18
    {
19 64
        $this->client = $client;
20 64
        $this->spaceSpace = $client->getSpace(Space::VSPACE);
21 64
        $this->indexSpace = $client->getSpace(Space::VINDEX);
22
23 64
        if($data) {
24
            $this->spaceId = $data;
25
26
        } else {
27 64
            $this->collectData();
28
        }
29
30 64
    }
31
32 64 View Code Duplication
    public function getSpaceId($space)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34 64
        if (!array_key_exists($space, $this->spaceId)) {
35 64
            $response = $this->spaceSpace->select([$space], Index::SPACE_NAME);
36 64
            $data = $response->getData();
37 64
            if (!empty($data)) {
38
                $this->spaceId[$space] = $data[0][0];
39
            }
40 64
        }
41 64
        if (array_key_exists($space, $this->spaceId)) {
42
            return $this->spaceId[$space];
43
        }
44 64
    }
45
46 View Code Duplication
    public function getSpaceName($spaceId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        if (!in_array($spaceId, $this->spaceId)) {
49
            $response = $this->spaceSpace->select([$spaceId], 0);
50
            $data = $response->getData();
51
            if (!empty($data)) {
52
                $this->spaceId[$data[0][2]] = $spaceId;
53
            }
54
        }
55
56
        if (in_array($spaceId, $this->spaceId)) {
57
            return array_search($spaceId, $this->spaceId);
58
        }
59
    }
60
61 64
    public function hasSpace($space)
62
    {
63 64
        return $this->getSpaceId($space) !== null;
64
    }
65
66 64
    public function createSpace($space)
67
    {
68 64
        $this->client->evaluate("box.schema.space.create('$space')");
69 64
    }
70
71
    public function dropSpace($space)
72
    {
73
        $this->client->evaluate('box.schema.space.drop('.$this->getSpaceId($space).')');
74
        unset($this->spaceId[$space]);
75
    }
76
77
    public function hasIndex($space, $index)
78
    {
79
        $spaceId = $this->getSpaceId($space);
80
        $response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME);
81
82
        return !empty($response->getData());
83
    }
84
85
    public function listIndexes($space)
86
    {
87
        $result = [];
88
        $response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME);
89
90
        foreach ($response->getData() as $row) {
91
            $result[$row[2]] = [];
92
            foreach ($row[5] as $f) {
93
                $result[$row[2]][] = $f[0];
94
            }
95
        }
96
97
        return $result;
98
    }
99
100 64
    public function createIndex($space, $index, array $arguments)
101
    {
102 64
        $config = [];
103 64
        foreach ($arguments as $k => $v) {
104 64
            if (is_array($v)) {
105
                // convert to lua array
106 64
                $v = str_replace(['[', ']'], ['{', '}'], json_encode($v));
107 64
            }
108 64
            if (is_bool($v)) {
109
                $v = $v ? 'true' : 'false';
110
            }
111 64
            $config[] = $k.' = '.$v;
112 64
        }
113 64
        $config = '{'.implode(', ', $config).'}';
114 64
        $this->client->evaluate("box.space.$space:create_index('$index', $config)");
115
116
        $schema = $this->client->getSpace(Space::VINDEX);
117
        $response = $schema->select([$this->getSpaceId($space), $index], Index::INDEX_NAME);
118
119
        return $response->getData()[0][1];
120
    }
121
122
    public function dropIndex($spaceId, $index)
123
    {
124
        $space = $this->client->getSpace('_vindex');
125
        $row = $space->select([$spaceId, $index])->getData();
126
        $spaceName = $this->getSpaceName($spaceId);
127
        $indexName = $row[0][2];
128
        $this->client->evaluate("box.space.$spaceName.index.$indexName:drop{}");
129
    }
130
131 64
    private function collectData()
132
    {
133 64
        $spaces = $this->spaceSpace->select([])->getData();
134 64
        foreach ($spaces as $row) {
0 ignored issues
show
Bug introduced by
The expression $spaces of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
135 64
            list($id, $sys, $name) = $row;
0 ignored issues
show
Unused Code introduced by
The assignment to $sys is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
136 64
            $this->spaceId[$name] = $id;
137 64
        }
138 64
    }
139
140
    public function toArray()
141
    {
142
        $this->collectData();
143
        return $this->spaceId;
144
    }
145
}
146