Completed
Push — master ( 61da31...475d79 )
by Dmitry
03:04
created

Meta::has()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
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 27
    public function __construct(Contracts\Manager $manager)
16
    {
17 27
        $this->manager = $manager;
18
19 27
        $client = $manager->getClient();
20 27
        foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
21 27
            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 27
            if (!array_key_exists($spaceId, $this->property)) {
23 27
                $this->property[$spaceId] = [];
24 27
                $this->types[$spaceId] = [];
25
            }
26 27
            $this->property[$spaceId][$line] = $name;
27 27
            $this->types[$spaceId][$name] = $type;
28
        }
29 27
        foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
30 27
            list($spaceId, $num, $name, $type, $params, $properties) = $index;
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...
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...
Unused Code introduced by
The assignment to $name 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 27
            if (!array_key_exists($spaceId, $this->property)) {
32
                // tarantool space index
33 27
                continue;
34
            }
35 27
            if (!isset($this->indexes[$spaceId])) {
36 27
                $this->indexes[$spaceId] = [];
37
            }
38 27
            $this->indexes[$spaceId][$num] = [];
39 27
            foreach ($properties as $row) {
40 27
                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 27
                $this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part];
42
            }
43
        }
44 27
        foreach ($this->property as $spaceId => $collection) {
45 27
            ksort($collection);
46 27
            $this->property[$spaceId] = $collection;
47
        }
48 27
    }
49
50
    /**
51
     * @return Type
52
     */
53 27
    public function get($type)
54
    {
55 27
        if (!array_key_exists($type, $this->types)) {
56 27
            $spaceId = $this->manager->getSchema()->getSpaceId($type);
57 27
            if (!$spaceId) {
58 1
                throw new LogicException("Type $type not exists");
59
            }
60
61 27
            $this->types[$type] = new Type(
62 27
                $this->manager, $type,
63 27
                $this->property[$spaceId],
64 27
                $this->types[$spaceId],
65 27
                $this->indexes[$spaceId]
66
            );
67
        }
68
69 27
        return $this->types[$type];
70
    }
71
72 2
    public function has($type)
73
    {
74
        // was created
75 2
        if (array_key_exists($type, $this->types)) {
76 1
            return true;
77
        }
78
79
        // can be created
80 2
        $spaceId = $this->manager->getSchema()->getSpaceId($type);
81 2
        if (array_key_exists($spaceId, $this->property)) {
82 1
            return true;
83
        }
84
85 2
        return false;
86
    }
87
88
    /**
89
     * @return Type
90
     */
91 27
    public function create($type, array $fields = null)
92
    {
93 27
        if ($this->manager->getSchema()->hasSpace($type)) {
94 1
            throw new LogicException("Type $type exists");
95
        }
96
97 27
        $this->manager->getSchema()->createSpace($type);
98
99 27
        $instance = new Type($this->manager, $type, [], [], []);
100
101 27
        $instance->addProperty('id');
102 27
        $instance->addIndex('id');
103
104 27
        if ($fields) {
105 27
            foreach ($fields as $index => $field) {
106 27
                if ($field instanceof Contracts\Type) {
107 2
                    if (!is_numeric($index)) {
108 1
                        $instance->reference($field, $index);
109
                    } else {
110 2
                        $instance->reference($field);
111
                    }
112
                } else {
113 27
                    $instance->addProperty($field);
114
                }
115
            }
116
        }
117
118 27
        return $this->types[$type] = $instance;
119
    }
120
121 1
    public function setConvention(Contracts\Convention $convention)
122
    {
123 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...
124
125 1
        return $this;
126
    }
127
128 27
    public function getConvention()
129
    {
130 27
        if (!isset($this->convention)) {
131 27
            $this->convention = new Convention();
132
        }
133
134 27
        return $this->convention;
135
    }
136
}
137