Completed
Push — master ( 13dce9...b41dd5 )
by Dmitry
03:01
created

Type::addIndex()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 30
Code Lines 17

Duplication

Lines 5
Ratio 16.67 %

Code Coverage

Tests 20
CRAP Score 8.048

Importance

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