Completed
Push — master ( e4e252...9e06e2 )
by Dmitry
02:48
created

Meta::__construct()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 34
ccs 24
cts 24
cp 1
rs 5.3846
cc 8
eloc 23
nc 36
nop 1
crap 8
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Mapper\Contracts;
6
use LogicException;
7
8
class Meta implements Contracts\Meta
9
{
10
    protected $manager;
11
    protected $property = [];
12
    protected $indexes = [];
13
    protected $types = [];
14
15 26
    public function __construct(Contracts\Manager $manager)
16
    {
17 26
        $this->manager = $manager;
18
19 26
        $client = $manager->getClient();
20 26
        foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
21 26
            list($id, $spaceId, $line, $name, $type) = $property;
0 ignored issues
show
Unused Code introduced by
The assignment to $id 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...
22 26
            if (!array_key_exists($spaceId, $this->property)) {
23 26
                $this->property[$spaceId] = [];
24 26
                $this->types[$spaceId] = [];
25
            }
26 26
            $this->property[$spaceId][$line] = $name;
27 26
            $this->types[$spaceId][$name] = $type;
28
        }
29 26
        foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
30 26
            list($spaceId, $num, $name, $type, $params, $properties) = $index;
0 ignored issues
show
Unused Code introduced by
The assignment to $num 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...
Unused Code introduced by
The assignment to $type 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...
Unused Code introduced by
The assignment to $params 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...
31 26
            if (!array_key_exists($spaceId, $this->property)) {
32
                // tarantool space index
33 26
                continue;
34
            }
35 26
            if (!isset($this->indexes[$spaceId])) {
36 26
                $this->indexes[$spaceId] = [];
37
            }
38 26
            $this->indexes[$spaceId][$name] = [];
39 26
            foreach ($properties as $row) {
40 26
                list($part, $type) = $row;
0 ignored issues
show
Unused Code introduced by
The assignment to $type 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...
41 26
                $this->indexes[$spaceId][$name][] = $this->property[$spaceId][$part];
42
            }
43
        }
44 26
        foreach ($this->property as $spaceId => $collection) {
45 26
            ksort($collection);
46 26
            $this->property[$spaceId] = $collection;
47
        }
48 26
    }
49
50
    /**
51
     * @return Type
52
     */
53 26
    public function get($type)
54
    {
55 26
        if (!array_key_exists($type, $this->types)) {
56 26
            $spaceId = $this->manager->getSchema()->getSpaceId($type);
57 26
            if (!$spaceId) {
58 1
                throw new LogicException("Type $type not exists");
59
            }
60
61 26
            $this->types[$type] = new Type(
62 26
                $this->manager, $type,
63 26
                $this->property[$spaceId],
64 26
                $this->types[$spaceId],
65 26
                $this->indexes[$spaceId]
66
            );
67
        }
68
69 26
        return $this->types[$type];
70
    }
71
72
    /**
73
     * @return Type
74
     */
75 26
    public function create($type, array $fields = null)
76
    {
77 26
        if ($this->manager->getSchema()->hasSpace($type)) {
78 1
            throw new LogicException("Type $type exists");
79
        }
80
81 26
        $this->manager->getSchema()->createSpace($type);
82
83 26
        $instance = new Type($this->manager, $type, [], [], []);
84
85 26
        $instance->addProperty('id');
86 26
        $instance->addIndex('id');
87
88 26
        if ($fields) {
89 26
            foreach ($fields as $index => $field) {
90 26
                if ($field instanceof Contracts\Type) {
91 2
                    if (!is_numeric($index)) {
92 1
                        $instance->reference($field, $index);
93
                    } else {
94 2
                        $instance->reference($field);
95
                    }
96
                } else {
97 26
                    $instance->addProperty($field);
98
                }
99
            }
100
        }
101
102 26
        return $this->types[$type] = $instance;
103
    }
104
105 1
    public function setConvention(Contracts\Convention $convention)
106
    {
107 1
        $this->convention = $convention;
0 ignored issues
show
Bug introduced by
The property convention does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
108
109 1
        return $this;
110
    }
111
112 26
    public function getConvention()
113
    {
114 26
        if (!isset($this->convention)) {
115 26
            $this->convention = new Convention();
116
        }
117
118 26
        return $this->convention;
119
    }
120
}
121