Completed
Push — master ( dcba90...2ccedc )
by Dmitry
03:50
created

Type::getManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Mapper\Contracts;
6
use LogicException;
7
8
class Type implements Contracts\Type
9
{
10
    protected $properties = [];
11
    protected $manager;
12
    protected $name;
13
14 4
    public function __construct(Contracts\Manager $manager, $name, array $properties = null)
15
    {
16 4
        $this->manager = $manager;
17 4
        $this->name = $name;
18
19 4
        if ($name == 'mapping') {
20 4
            $properties = ['id', 'space', 'line', 'property'];
21 4
        }
22
23 4
        if ($properties) {
24 4
            $this->properties = $properties;
25 4
        }
26 4
    }
27
28 4
    public function getSpace()
29
    {
30 4
        return $this->getManager()->getClient()->getSpace($this->name);
31
    }
32
33 4
    public function getManager()
34
    {
35 4
        return $this->manager;
36
    }
37
38 4
    public function getName()
39
    {
40 4
        return $this->name;
41
    }
42
43 4
    public function getMapping()
44
    {
45 4
        return $this->properties;
46
    }
47
48 4
    public function addIndex($properties, array $arguments = null)
49
    {
50 4
        $properties = (array) $properties;
51 4 View Code Duplication
        foreach ($properties as $property) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
52 4
            if (!$this->hasProperty($property)) {
53
                throw new LogicException("Unknown property $property for " . $this->name);
54
            }
55 4
        }
56
57 4
        $schema = $this->manager->getSchema();
58
59 4
        sort($properties);
60 4
        $indexName = implode('_', $properties);
61
62 4
        if ($schema->hasIndex($this->getName(), $indexName)) {
63
            throw new LogicException("Index $indexName already exists!");
64
        }
65 4
        if(!$arguments) {
66 4
            $arguments = [];
67 4
        }
68
69 4
        if (!array_key_exists('parts', $arguments) || !count($arguments['parts'])) {
70 4
            $arguments['parts'] = [];
71 4
            foreach ($this->getMapping() as $index => $name) {
72 4
                if (in_array($name, $properties)) {
73 4
                    $arguments['parts'][] = $index + 1;
74 4
                    $arguments['parts'][] = $name == 'id' ? 'NUM' : 'STR';
75 4
                }
76 4
            }
77 4
        }
78
79 4
        $schema->createIndex($this->getName(), $indexName, $arguments);
80 4
    }
81
82
    /**
83
     * @param $property name
84
     * @return Type
85
     */
86 4
    public function addProperty($first)
87
    {
88 4 View Code Duplication
        foreach (func_get_args() as $property) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89 4
            if ($this->hasProperty($property)) {
90
                throw new LogicException("Duplicate property $property");
91
            }
92 4
            $this->properties[] = $property;
93 4
        }
94 4
        return $this;
95
    }
96
97 4
    public function hasProperty($name) {
98 4
        return in_array($name, $this->properties);
99
    }
100
101 4 View Code Duplication
    public function encode($input)
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...
102
    {
103 4
        $output = [];
104 4
        foreach ($this->getMapping() as $index => $name) {
105 4
            if (array_key_exists($name, $input)) {
106 4
                $output[$index] = $input[$name];
107 4
            }
108 4
        }
109 4
        return $output;
110
    }
111
112 4 View Code Duplication
    public function decode($input)
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...
113
    {
114 4
        $output = [];
115 4
        foreach ($this->getMapping() as $index => $name) {
116 4
            if (array_key_exists($index, $input)) {
117 4
                $output[$name] = $input[$index];
118 4
            }
119 4
        }
120 4
        return $output;
121
    }
122
}
123