Passed
Push — master ( a1274e...f47e0d )
by Simon
12:16 queued 02:17
created

FluentExtension::onBeforeSearch()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 SilverStripe\ORM\ArrayList;
12
use SilverStripe\ORM\DataList;
13
use SilverStripe\ORM\DataObject;
14
use Solarium\QueryType\Select\Query\Query;
15
use TractorCow\Fluent\Model\Locale;
16
use TractorCow\Fluent\State\FluentState;
17
18
/**
19
 * Support for Fluent translations.
20
 *
21
 * This class should be moved to a separate repo or to Fluent, but provides the basic Fluent support for now
22
 *
23
 * @package Firesphere\SolrSearch\Compat
24
 * @property DocumentFactory|BaseIndex|SchemaService|FluentExtension $owner
25
 */
26
class FluentExtension extends Extension
27
{
28
    /**
29
     * Add the fluent states
30
     */
31
    public function onBeforeInit()
32
    {
33
        $locales = Locale::get();
34
        SiteState::addStates($locales->column('Locale'));
35
    }
36
37
    /**
38
     * Add the needed language copy fields to Solr
39
     */
40
    public function onAfterInit(): void
41
    {
42
        $locales = Locale::get();
43
        /** @var BaseIndex $owner */
44
        $owner = $this->owner;
45
        $copyFields = $owner->getCopyFields();
46
        /** @var Locale $locale */
47
        foreach ($locales as $locale) {
48
            foreach ($copyFields as $copyField => $values) {
49
                $owner->addCopyField($locale->Locale . $copyField, $values);
50
            }
51
        }
52
    }
53
54
    /**
55
     * Add the locale fields
56
     *
57
     * @param ArrayList|DataList $data
58
     * @param DataObject $item
59
     */
60
    public function onAfterFieldDefinition($data, $item): void
61
    {
62
        $locales = Locale::get();
63
64
        foreach ($locales as $locale) {
65
            $isDest = strpos($item['Destination'], $locale->Locale);
66
            if (($isDest === 0 || $item['Destination'] === null) && $locale->Locale !== null) {
67
                $copy = $item;
68
                $copy['Field'] = $item['Field'] . '_' . $locale->Locale;
69
                $data->push($copy);
70
            }
71
        }
72
    }
73
74
    /**
75
     * Update the Solr field for the value to use the locale name
76
     *
77
     * @param array $field
78
     * @param string $value
79
     */
80
    public function onBeforeAddDoc(&$field, &$value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

80
    public function onBeforeAddDoc(&$field, /** @scrutinizer ignore-unused */ &$value): 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...
81
    {
82
        $fluentState = FluentState::singleton();
83
        $locale = $fluentState->getLocale();
84
        if ($locale) {
85
            $field['name'] .= '_' . $locale;
86
        }
87
    }
88
89
    /**
90
     * Set to the correct language to search if needed
91
     *
92
     * @param BaseQuery $query
93
     * @param Query $clientQuery
94
     */
95
    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

95
    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...
96
    {
97
        $locale = FluentState::singleton()->getLocale();
98
        $defaultField = $clientQuery->getQueryDefaultField() ?: '_text';
99
        $clientQuery->setQueryDefaultField($locale . $defaultField);
100
    }
101
}
102