Passed
Pull Request — master (#1)
by Simon
08:01
created

FluentExtension::onBeforeInit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Firesphere\SolrSearch\Compat;
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;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\Model\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use TractorCow\Fluent\State\FluentState;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\State\FluentState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
        if (!class_exists('TractorCow\\Fluent\\Model\\Locale')) {
34
            return;
35
        }
36
        $locales = Locale::get()->exclude(['IsGlobalDefault' => true]);
37
        SiteState::addStates($locales->column('Locale'));
38
    }
39
40
    /**
41
     * Add the needed language copy fields to Solr
42
     */
43
    public function onAfterInit(): void
44
    {
45
        if (!class_exists('TractorCow\\Fluent\\Model\\Locale')) {
46
            return;
47
        }
48
        $locales = Locale::get()->exclude(['IsGlobalDefault' => true]);
49
        /** @var BaseIndex $owner */
50
        $owner = $this->owner;
51
        $copyFields = $owner->getCopyFields();
52
        /** @var Locale $locale */
53
        foreach ($locales as $locale) {
54
            foreach ($copyFields as $copyField => $values) {
55
                $owner->addCopyField($locale->Locale . $copyField, $values);
56
            }
57
        }
58
    }
59
60
    /**
61
     * Add the locale fields
62
     *
63
     * @param ArrayList|DataList $data
64
     * @param DataObject $item
65
     */
66
    public function onAfterFieldDefinition($data, $item): void
67
    {
68
        if (!class_exists('TractorCow\\Fluent\\Model\\Locale')) {
69
            return;
70
        }
71
        $locales = Locale::get()->exclude(['IsGlobalDefault' => true]);
72
73
        foreach ($locales as $locale) {
74
            $isDest = strpos($item['Destination'], $locale->Locale);
75
            if ($isDest === 0 || $item['Destination'] === null) {
76
                $copy = $item;
77
                $copy['Field'] = $item['Field'] . '_' . $locale->Locale;
78
                $data->push($copy);
79
            }
80
        }
81
    }
82
83
    /**
84
     * Update the Solr field for the value to use the locale name
85
     *
86
     * @param array $field
87
     * @param string $value
88
     */
89
    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

89
    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...
90
    {
91
        if (!class_exists('TractorCow\\Fluent\\Model\\Locale')) {
92
            return;
93
        }
94
        $fluentState = FluentState::singleton();
95
        $locale = $fluentState->getLocale();
96
        /** @var Locale $defaultLocale */
97
        $defaultLocale = Locale::get()->filter(['IsGlobalDefault' => true])->first();
98
        if ($locale !== null && $locale !== $defaultLocale->Locale) {
99
            $field['name'] .= '_' . $locale;
100
        }
101
    }
102
103
    /**
104
     * Set to the correct language to search if needed
105
     *
106
     * @param BaseQuery $query
107
     * @param Query $clientQuery
108
     */
109
    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

109
    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...
110
    {
111
        if (!class_exists('TractorCow\\Fluent\\Model\\Locale')) {
112
            return;
113
        }
114
        $locale = FluentState::singleton()->getLocale();
115
        $defaultLocale = Locale::get()->filter(['IsGlobalDefault' => true])->first();
116
        if ($locale && $defaultLocale !== null && $locale !== $defaultLocale->Locale) {
117
            $defaultField = $clientQuery->getQueryDefaultField() ?: '_text';
118
            $clientQuery->setQueryDefaultField($locale . $defaultField);
119
        }
120
    }
121
}
122