Completed
Push — master ( e7f8de...7e52b4 )
by Dmitry
03:22
created

Schema::getNumberType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
crap 3
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 $numberType;
14
    protected $spaceSpace;
15
    protected $indexSpace;
16
    protected $spaceId = [];
17
18 64
    public function __construct(Client $client, array $data = null)
19
    {
20 64
        $this->client = $client;
21 64
        $this->spaceSpace = $client->getSpace(Space::VSPACE);
22 64
        $this->indexSpace = $client->getSpace(Space::VINDEX);
23
24 64
        if($data) {
25 1
            $this->spaceId = $data[0];
26 1
            $this->numberType = $data[1];
27
28 1
        } else {
29 64
            $this->collectData();
30
        }
31
32 64
    }
33
34 64
    public function getNumberType()
35
    {
36 64
        if(!$this->numberType) {
37 64
            $version = $this->client->evaluate('return box.info.version')->getData()[0];
38 64
            $this->numberType = $version >= '1.7' ? 'UNSIGNED' : 'NUM';
39 64
        }
40 64
        return $this->numberType;
41
    }
42
43 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...
44
    {
45 64
        if (!array_key_exists($space, $this->spaceId)) {
46 64
            $response = $this->spaceSpace->select([$space], Index::SPACE_NAME);
47 64
            $data = $response->getData();
48 64
            if (!empty($data)) {
49 64
                $this->spaceId[$space] = $data[0][0];
50 64
            }
51 64
        }
52 64
        if (array_key_exists($space, $this->spaceId)) {
53 64
            return $this->spaceId[$space];
54
        }
55 64
    }
56
57 7 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...
58
    {
59 7
        if (!in_array($spaceId, $this->spaceId)) {
60 1
            $response = $this->spaceSpace->select([$spaceId], 0);
61 1
            $data = $response->getData();
62 1
            if (!empty($data)) {
63 1
                $this->spaceId[$data[0][2]] = $spaceId;
64 1
            }
65 1
        }
66
67 7
        if (in_array($spaceId, $this->spaceId)) {
68 7
            return array_search($spaceId, $this->spaceId);
69
        }
70
    }
71
72 64
    public function hasSpace($space)
73
    {
74 64
        return $this->getSpaceId($space) !== null;
75
    }
76
77 64
    public function createSpace($space)
78
    {
79 64
        $this->client->evaluate("box.schema.space.create('$space')");
80 64
    }
81
82 2
    public function dropSpace($space)
83
    {
84 2
        $this->client->evaluate('box.schema.space.drop('.$this->getSpaceId($space).')');
85 2
        unset($this->spaceId[$space]);
86 2
    }
87
88 64
    public function hasIndex($space, $index)
89
    {
90 64
        $spaceId = $this->getSpaceId($space);
91 64
        $response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME);
92
93 64
        return !empty($response->getData());
94
    }
95
96 64
    public function listIndexes($space)
97
    {
98 64
        $result = [];
99 64
        $response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME);
100
101 64
        foreach ($response->getData() as $row) {
102 64
            $result[$row[2]] = [];
103 64
            foreach ($row[5] as $f) {
104 64
                $result[$row[2]][] = $f[0];
105 64
            }
106 64
        }
107
108 64
        return $result;
109
    }
110
111 64
    public function createIndex($space, $index, array $arguments)
112
    {
113 64
        $config = [];
114 64
        foreach ($arguments as $k => $v) {
115 64
            if (is_array($v)) {
116
                // convert to lua array
117 64
                $v = str_replace(['[', ']'], ['{', '}'], json_encode($v));
118 64
            }
119 64
            if (is_bool($v)) {
120 64
                $v = $v ? 'true' : 'false';
121 64
            }
122 64
            $config[] = $k.' = '.$v;
123 64
        }
124 64
        $config = '{'.implode(', ', $config).'}';
125 64
        $this->client->evaluate("box.space.$space:create_index('$index', $config)");
126
127 64
        $schema = $this->client->getSpace(Space::VINDEX);
128 64
        $response = $schema->select([$this->getSpaceId($space), $index], Index::INDEX_NAME);
129
130 64
        return $response->getData()[0][1];
131
    }
132
133 3
    public function dropIndex($spaceId, $index)
134
    {
135 3
        $space = $this->client->getSpace('_vindex');
136 3
        $row = $space->select([$spaceId, $index])->getData();
137 3
        $spaceName = $this->getSpaceName($spaceId);
138 3
        $indexName = $row[0][2];
139 3
        $this->client->evaluate("box.space.$spaceName.index.$indexName:drop{}");
140 3
    }
141
142 64
    private function collectData()
143
    {
144 64
        $spaces = $this->spaceSpace->select([])->getData();
145 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...
146 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...
147 64
            $this->spaceId[$name] = $id;
148 64
        }
149 64
    }
150
151 1
    public function toArray()
152
    {
153 1
        $this->collectData();
154 1
        return [$this->spaceId, $this->numberType];
155
    }
156
}
157