Passed
Pull Request — master (#5)
by Gordon
08:50
created

IndexCreator::createIndex()   B

Complexity

Conditions 7
Paths 28

Size

Total Lines 75
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 40
c 1
b 0
f 0
nc 28
nop 1
dl 0
loc 75
rs 8.3466

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 24/3/2561
7
 * Time: 21:14 น.
8
 */
9
10
namespace Suilven\ManticoreSearch\Service;
11
12
use SilverStripe\ORM\DataObjectSchema;
13
use Suilven\FreeTextSearch\Indexes;
14
15
class IndexCreator implements \Suilven\FreeTextSearch\Interfaces\IndexCreator
16
{
17
    public function createIndex(string $indexName): void
18
    {
19
        $indexes = new Indexes();
20
21
        $index = $indexes->getIndex($indexName);
22
23
        $singleton = \singleton($index->getClass());
24
25
        // ['ID', 'CreatedAt', 'LastEdited'];
26
        $fields = ['CreatedAt'];
27
28
        // @todo different field types
29
        foreach ($index->getFields() as $field) {
30
            $fields[] = $field;
31
        }
32
33
        foreach ($index->getTokens() as $token) {
34
            $fields[] = $token;
35
        }
36
37
        /** @var \SilverStripe\ORM\DataObjectSchema $schema */
38
        $schema = $singleton->getSchema();
39
        $specs = $schema->fieldSpecs($index->getClass(), DataObjectSchema::DB_ONLY);
40
41
        $columns = [];
42
        foreach ($fields as $field) {
43
            $fieldType = $specs[$field];
44
45
            // fix likes of varchar(255)
46
            $fieldType = \explode('(', $fieldType)[0];
47
48
            // this will be the most common
49
            $indexType = 'text';
50
51
            // @todo configure index to strip HTML
52
            switch ($fieldType) {
53
                case 'Int':
54
                    $indexType = 'integer';
55
56
                    break;
57
                case 'Float':
58
                    $indexType = 'float';
59
60
                    break;
61
            }
62
63
            $options = [];
64
            if ($indexType === 'text') {
65
                $options = ['indexed', 'stored'];
66
            }
67
            $columns[$field] = ['type' => $indexType, 'options' => $options];
68
        }
69
70
71
72
        $client = new Client();
73
        $manticoreClient = $client->getConnection();
74
75
76
        $settings = [
77
            'rt_mem_limit' => '256M',
78
            'dict' => 'keywords',
79
            'min_infix_len' => 2,
80
            'html_strip' => 1,
81
        ];
82
83
        \error_log('INDEX CREATION PAYLOAD: $columns');
84
        \error_log(\print_r($columns, 1));
85
86
        $manticoreIndex = new \Manticoresearch\Index($manticoreClient, $indexName);
87
88
        $manticoreIndex->create(
89
            $columns,
90
            $settings,
91
            true,
92
        );
93
    }
94
}
95