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

Indexes::getIndexes()   C

Complexity

Conditions 13
Paths 65

Size

Total Lines 61
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 29
c 4
b 0
f 0
dl 0
loc 61
rs 6.6166
cc 13
nc 65
nop 0

How to fix   Long Method    Complexity   

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
// @phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
4
5
namespace Suilven\FreeTextSearch;
6
7
use SilverStripe\Core\Config\Config;
8
9
/**
10
 * Class Indexes
11
 *
12
 * @package Suilven\FreeTextSearch
13
 */
14
class Indexes
15
{
16
    /** @var array<string, \Suilven\FreeTextSearch\Index>|null */
17
    private $indexesByName;
18
19
20
    /**
21
     * Get an Index object by name from the config
22
     */
23
    public function getIndex(string $name): Index
24
    {
25
        if (\is_null($this->indexesByName)) {
26
            $this->getIndexes();
27
        }
28
29
        return $this->indexesByName[$name];
30
    }
31
32
33
    /**
34
     * Get indexes from config
35
     *
36
     * @return array<\Suilven\FreeTextSearch\Index> ClassName -> Index
37
     */
38
    public function getIndexes(): array
39
    {
40
        $indexes = [];
41
42
        $indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes') ;
43
44
        // reset
45
        $this->indexesByName = [];
46
47
        foreach ($indexesConfig as $indexConfig) {
48
            $index = new Index();
49
            $index->setClass($indexConfig['index']['class']);
50
            $index->setName($indexConfig['index']['name']);
51
            foreach ($indexConfig['index']['fields'] as $fieldname) {
52
                $index->addField($fieldname);
53
            }
54
55
            if (isset($indexConfig['index']['tokens'])) {
56
                foreach ($indexConfig['index']['tokens'] as $token) {
57
                    $index->addToken($token);
58
                }
59
            }
60
61
            // has one fields
62
            if (isset($indexConfig['index']['has_one'])) {
63
                foreach ($indexConfig['index']['has_one'] as $hasOneField) {
64
                    $index->addHasOneField($hasOneField);
65
                }
66
            }
67
68
            // has many fields
69
            // NB many many may need to be treated as bipartisan has many
70
            if (isset($indexConfig['index']['has_many'])) {
71
                foreach ($indexConfig['index']['has_many'] as $hasManyField) {
72
                    $index->addHasManyField($hasManyField['name'], [
73
                        'relationship' => $hasManyField['relationship'],
74
                        'field' => $hasManyField['field'],
75
                    ]);
76
                }
77
            }
78
79
            // fields that will be used for highlighting
80
            if (isset($indexConfig['index']['highlighted_fields'])) {
81
                foreach ($indexConfig['index']['highlighted_fields'] as $highlightedField) {
82
                    $index->addHighlightedField($highlightedField);
83
                }
84
            }
85
86
            // fields that will be used for storage, but not indexed
87
            if (isset($indexConfig['index']['stored_fields'])) {
88
                foreach ($indexConfig['index']['stored_fields'] as $storedField) {
89
                    $index->addStoredField($storedField);
90
                }
91
            }
92
93
            $indexes[] = $index;
94
95
            $this->indexesByName[$index->getName()] = $index;
96
        }
97
98
        return $indexes;
99
    }
100
101
102
    /** @return array<string> An array of facet fields, such as ['ISO', 'Aperture', 'ShutterSpeed'] */
103
    public function getFacetFields(string $indexName): array
104
    {
105
        $indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes');
106
        $result = [];
107
        foreach ($indexesConfig as $indexConfig) {
108
            $name = ($indexConfig['index']['name']);
109
110
            if ($name !== $indexName) {
111
                continue;
112
            }
113
114
            if (isset($indexConfig['index']['tokens'])) {
115
                foreach ($indexConfig['index']['tokens'] as $token) {
116
                    $result[] = $token;
117
                }
118
            }
119
        }
120
121
        return $result;
122
    }
123
124
125
    /** @return array<string> */
126
    public function getHasOneFields(string $indexName): array
127
    {
128
        $indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes');
129
130
        $result = [];
131
        foreach ($indexesConfig as $indexConfig) {
132
            $name = ($indexConfig['index']['name']);
133
134
            if ($name !== $indexName) {
135
                continue;
136
            }
137
138
            if (isset($indexConfig['index']['has_one'])) {
139
                foreach ($indexConfig['index']['has_one'] as $hasOne) {
140
                    $result[] = $hasOne;
141
                }
142
            }
143
        }
144
145
        return $result;
146
    }
147
148
149
    /** @return array<string> */
150
    public function getHasManyFields(string $indexName): array
151
    {
152
        $indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes');
153
154
        $result = [];
155
        foreach ($indexesConfig as $indexConfig) {
156
            $name = ($indexConfig['index']['name']);
157
158
            if ($name !== $indexName) {
159
                continue;
160
            }
161
162
            if (isset($indexConfig['index']['has_many'])) {
163
                foreach ($indexConfig['index']['has_many'] as $hasManyField) {
164
                    $result[] = $hasManyField;
165
                }
166
            }
167
        }
168
169
        return $result;
170
    }
171
}
172