Passed
Push — master ( 3dc1a3...8beebe )
by Gordon
05:38 queued 03:19
created

IndexCreator::getStoredFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 24/3/2561
7
 * Time: 20:36 น.
8
 */
9
10
namespace Suilven\FreeTextSearch\Base;
11
12
use SilverStripe\ORM\DataObjectSchema;
13
use Suilven\FreeTextSearch\Indexes;
14
15
abstract class IndexCreator implements \Suilven\FreeTextSearch\Interfaces\IndexCreator
16
{
17
    /**
18
     * (Re)create an index of the given name, using the index configuration from YML
19
     *
20
     * @param string $indexName The name of the index
21
     */
22
    abstract public function createIndex(string $indexName): void;
23
24
25
    /**
26
     * Helper method to get get field specs for a DataObject relevant to it's index definition
27
     *
28
     * @param string $indexName the name of the index
29
     * @return array<string,string>
30
     */
31
    protected function getFieldSpecs(string $indexName): array
32
    {
33
        $indexes = new Indexes();
34
        $index = $indexes->getIndex($indexName);
35
        $singleton = \singleton($index->getClass());
36
37
        $fields = $this->getFields($indexName);
38
39
        \error_log(\print_r($fields, true));
40
41
        /** @var \SilverStripe\ORM\DataObjectSchema $schema */
42
        $schema = $singleton->getSchema();
43
        $specs = $schema->fieldSpecs($index->getClass(), DataObjectSchema::INCLUDE_CLASS);
44
45
        /** @var array<string,string> $filteredSpecs the DB specs for fields related to the index */
46
        $filteredSpecs = [];
47
48
        foreach ($fields as $field) {
49
            \error_log('CHECKING FIELD ' . $field);
50
51
            if ($field === 'Link') {
52
                continue;
53
            }
54
            $fieldType = $specs[$field];
55
56
            // fix likes of varchar(255)
57
            $fieldType = \explode('(', $fieldType)[0];
58
59
            // remove the class name
60
            $fieldType = \explode('.', $fieldType)[1];
61
62
            $filteredSpecs[$field] = $fieldType;
63
        }
64
65
        // if Link undefined in the original index specs, add it if the method exists on the singleton dataobject
66
        if (!isset($filteredSpecs['Link'])) {
67
            if (\method_exists($singleton, 'Link')) {
68
                $filteredSpecs['Link'] = 'Varchar';
69
            }
70
        }
71
72
        return $filteredSpecs;
73
    }
74
75
76
    /** @return array<string> */
77
    protected function getStoredFields(string $indexName): array
78
    {
79
        $indexes = new Indexes();
80
        $index = $indexes->getIndex($indexName);
81
82
        return $index->getStoredFields();
83
    }
84
85
86
        /** @return array<string> */
87
    protected function getFields(string $indexName): array
88
    {
89
        $indexes = new Indexes();
90
        $index = $indexes->getIndex($indexName);
91
92
        $fields = [];
93
94
        foreach ($index->getFields() as $field) {
95
            $fields[] = $field;
96
        }
97
98
        foreach ($index->getTokens() as $token) {
99
            $fields[] = $token;
100
        }
101
102
        foreach ($index->getStoredFields() as $storedField) {
103
            $fields[] = $storedField;
104
        }
105
106
        if (!\in_array('Link', $fields, true)) {
107
            $fields[] = 'Link';
108
        }
109
110
        return $fields;
111
    }
112
}
113