Completed
Push — master ( f0ac72...8c37f7 )
by Dmitry
02:48
created

Schema::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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