FluentIndexExtension::onBeforeInit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class FluentIndexExtension|Firesphere\SolrFluent\Extensions\FluentIndexExtension Add Fluent locales to the index
4
 *
5
 * @package Firesphere\Solr\Fluent
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrFluent\Extensions;
11
12
use Firesphere\SolrSearch\Indexes\BaseIndex;
13
use Firesphere\SolrSearch\Queries\BaseQuery;
14
use Firesphere\SolrSearch\States\SiteState;
15
use SilverStripe\Core\Extension;
16
use Solarium\QueryType\Select\Query\Query;
17
use TractorCow\Fluent\Model\Locale;
18
use TractorCow\Fluent\State\FluentState;
19
20
/**
21
 * Support for Fluent translations in the index.
22
 *
23
 *
24
 * @package Firesphere\Solr\Fluent
25
 * @property BaseIndex|FluentIndexExtension $owner
26
 */
27
class FluentIndexExtension extends Extension
28
{
29
    /**
30
     * Add the fluent states
31
     */
32 2
    public function onBeforeInit()
33
    {
34 2
        $locales = Locale::get();
35 2
        SiteState::addStates($locales->column('Locale'));
36 2
    }
37
38
    /**
39
     * Add the needed language copy fields to Solr
40
     */
41 2
    public function onAfterInit(): void
42
    {
43 2
        $locales = Locale::get();
44
        /** @var BaseIndex $owner */
45 2
        $owner = $this->owner;
46 2
        $copyFields = $owner->getCopyFields();
47
        /** @var Locale $locale */
48 2
        foreach ($locales as $locale) {
49 2
            foreach ($copyFields as $copyField => $values) {
50 2
                $owner->addCopyField($locale->Locale . $copyField, $values);
51
            }
52
        }
53 2
    }
54
55
    /**
56
     * Set to the correct language to search if needed
57
     *
58
     * @param BaseQuery $query
59
     * @param Query $clientQuery
60
     */
61 1
    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

61
    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...
62
    {
63 1
        $locale = FluentState::singleton()->getLocale();
64 1
        $defaultField = $clientQuery->getQueryDefaultField() ?: '_text';
65 1
        $clientQuery->setQueryDefaultField($locale . $defaultField);
66 1
    }
67
}
68