Create   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 27
c 4
b 1
f 1
dl 0
loc 55
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setIndex() 0 3 1
C setBody() 0 32 13
A getIndex() 0 3 1
1
<?php
2
3
namespace Manticoresearch\Endpoints\Indices;
4
5
use Manticoresearch\Endpoints\EmulateBySql;
6
use Manticoresearch\Exceptions\RuntimeException;
7
use Manticoresearch\Utils;
8
9
/**
10
 * Class Create
11
 * @package Manticoresearch\Endpoints\Indices
12
 */
13
class Create extends EmulateBySql
14
{
15
    use Utils;
16
    /**
17
     * @var string
18
     */
19
    protected $index;
20
21
    public function setBody($params = null)
22
    {
23
        if (isset($this->index)) {
24
            $columns = [];
25
            if (isset($params['columns'])) {
26
                foreach ($params['columns'] as $name => $settings) {
27
                    $column = '`' . $name . '` ' . $settings['type'];
28
                    if (isset($settings['options']) && count($settings['options']) > 0) {
29
                        $column .= ' ' . implode(' ', $settings['options']);
30
                    }
31
                    $columns[] = $column;
32
                }
33
            }
34
            $options = "";
35
            if (isset($params['settings'])) {
36
                foreach ($params['settings'] as $name => $value) {
37
                    if (is_array($value)) {
38
                        foreach ($value as $v) {
39
                            $options.=" ".$name." = '".$v."'";
40
                        }
41
                    } else {
42
                        $options.=" ".$name." = '".$value."'";
43
                    }
44
                }
45
            }
46
            return parent::setBody(['query' => "CREATE TABLE ".
47
                (isset($params['silent']) && $params['silent']===true?' IF NOT EXISTS ':'').
48
                $this->index.
49
                (count($columns)>0?"(".implode(",", $columns).")":" ")
50
                .$options]);
51
        }
52
        throw new RuntimeException('Index name is missing.');
53
    }
54
    /**
55
     * @return mixed
56
     */
57
    public function getIndex()
58
    {
59
        return $this->index;
60
    }
61
62
    /**
63
     * @param mixed $index
64
     */
65
    public function setIndex($index)
66
    {
67
        $this->index = $index;
68
    }
69
}
70