Passed
Push — master ( f47e0d...d9327a )
by Simon
10:55 queued 52s
created

FluentIndexExtension::onBeforeInit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Firesphere\SolrFluent\Extensions;
4
5
use Firesphere\SolrSearch\Factories\DocumentFactory;
6
use Firesphere\SolrSearch\Indexes\BaseIndex;
7
use Firesphere\SolrSearch\Queries\BaseQuery;
8
use Firesphere\SolrSearch\Services\SchemaService;
9
use Firesphere\SolrSearch\States\SiteState;
10
use SilverStripe\Core\Extension;
11
use Solarium\QueryType\Select\Query\Query;
12
use TractorCow\Fluent\Model\Locale;
13
use TractorCow\Fluent\State\FluentState;
14
15
/**
16
 * Support for Fluent translations in the index.
17
 *
18
 *
19
 * @package Firesphere\SolrFluent\Extensions
20
 * @property BaseIndex|FluentIndexExtension $owner
21
 */
22
class FluentIndexExtension extends Extension
23
{
24
    /**
25
     * Add the fluent states
26
     */
27
    public function onBeforeInit()
28
    {
29
        $locales = Locale::get();
30
        SiteState::addStates($locales->column('Locale'));
31
    }
32
33
    /**
34
     * Add the needed language copy fields to Solr
35
     */
36
    public function onAfterInit(): void
37
    {
38
        $locales = Locale::get();
39
        /** @var BaseIndex $owner */
40
        $owner = $this->owner;
41
        $copyFields = $owner->getCopyFields();
42
        /** @var Locale $locale */
43
        foreach ($locales as $locale) {
44
            foreach ($copyFields as $copyField => $values) {
45
                $owner->addCopyField($locale->Locale . $copyField, $values);
46
            }
47
        }
48
    }
49
50
    /**
51
     * Set to the correct language to search if needed
52
     *
53
     * @param BaseQuery $query
54
     * @param Query $clientQuery
55
     */
56
    public function onBeforeSearch($query, $clientQuery): void
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

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

56
    public function onBeforeSearch(/** @scrutinizer ignore-unused */ $query, $clientQuery): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        $locale = FluentState::singleton()->getLocale();
59
        $defaultField = $clientQuery->getQueryDefaultField() ?: '_text';
60
        $clientQuery->setQueryDefaultField($locale . $defaultField);
61
    }
62
}
63