Passed
Push — main ( ae47d0...a5bc80 )
by Simon
01:06
created

CoreIndex::init()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 0
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\SearchBackend\Indexes;
4
5
use Firesphere\SearchBackend\Traits\IndexingTraits\CoreIndexTrait;
6
use LogicException;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Dev\Deprecation;
10
11
abstract class CoreIndex
12
{
13
    use Extensible;
14
    use Configurable;
15
    use CoreIndexTrait;
16
    /**
17
     * Field types that can be added
18
     * Used in init to call build methods from configuration yml
19
     *
20
     * @array
21
     */
22
    protected static $fieldTypes = [
23
        'FulltextFields',
24
        'SortFields',
25
        'FilterFields',
26
        'BoostedFields',
27
        'CopyFields',
28
        'DefaultField',
29
        'FacetFields',
30
        'StoredFields',
31
    ];
32
33
    /**
34
     * Required to initialise the fields.
35
     * It's loaded in to the non-static properties for backward compatibility with FTS
36
     * Also, it's a tad easier to use this way, loading the other way around would be very
37
     * memory intensive, as updating the config for each item is not efficient
38
     */
39
    public function init()
40
    {
41
        $config = static::config()->get($this->getIndexName());
0 ignored issues
show
Bug introduced by
The method getIndexName() does not exist on Firesphere\SearchBackend\Indexes\CoreIndex. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $config = static::config()->get($this->/** @scrutinizer ignore-call */ getIndexName());
Loading history...
42
        if (!$config) {
43
            Deprecation::notice('5', 'Please set an index name and use a config yml');
44
        }
45
46
        if (!empty($this->getClasses())) {
0 ignored issues
show
Bug introduced by
The method getClasses() does not exist on Firesphere\SearchBackend\Indexes\CoreIndex. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        if (!empty($this->/** @scrutinizer ignore-call */ getClasses())) {
Loading history...
47
            if (!$this->usedAllFields) {
48
                Deprecation::notice('5', 'It is advised to use a config YML for most cases');
49
            }
50
51
            return;
52
        }
53
54
        $this->initFromConfig($config);
55
    }
56
57
    /**
58
     * Generate the config from yml if possible
59
     * @param array|null $config
60
     */
61
    protected function initFromConfig($config): void
62
    {
63
        if (!$config || !array_key_exists('Classes', $config)) {
64
            throw new LogicException('No classes or config to index found!');
65
        }
66
67
        $this->setClasses($config['Classes']);
0 ignored issues
show
Bug introduced by
The method setClasses() does not exist on Firesphere\SearchBackend\Indexes\CoreIndex. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        $this->/** @scrutinizer ignore-call */ 
68
               setClasses($config['Classes']);
Loading history...
68
69
        // For backward compatibility, copy the config to the protected values
70
        // Saves doubling up further down the line
71
        foreach (self::$fieldTypes as $type) {
72
            if (array_key_exists($type, $config)) {
73
                $method = 'set' . $type;
74
                if (method_exists($this, $method)) {
75
                    $this->$method($config[$type]);
76
                }
77
            }
78
        }
79
    }
80
81
    /**
82
     * Get all fields that are required for indexing in a unique way
83
     *
84
     * @return array
85
     */
86
    public function getFieldsForIndexing(): array
87
    {
88
        $facets = [];
89
        foreach ($this->getFacetFields() as $field) {
90
            $facets[] = $field['Field'];
91
        }
92
        // Return values to make the key reset
93
        // Only return unique values
94
        // And make it all a single array
95
        $fields = array_values(
96
            array_unique(
97
                array_merge(
98
                    $this->getFulltextFields(),
99
                    $this->getSortFields(),
100
                    $facets,
101
                    $this->getFilterFields()
102
                )
103
            )
104
        );
105
106
        $this->extend('updateFieldsForIndexing', $fields);
107
108
        return $fields;
109
    }
110
}
111