Passed
Push — master ( 782827...3b6f9f )
by Simon
08:29
created

FluentExtension::onBeforeAddDoc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Firesphere\SolrSearch\Compat;
4
5
use Firesphere\SolrSearch\Indexes\BaseIndex;
6
use Firesphere\SolrSearch\Queries\BaseQuery;
7
use League\Flysystem\Adapter\Local;
8
use SilverStripe\Core\Extension;
9
use Solarium\QueryType\Select\Query\Query;
10
use TractorCow\Fluent\Extension\FluentChangesExtension;
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\Extension\FluentChangesExtension 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...
11
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...
12
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...
13
14
if (!class_exists('TractorCow\\Fluent\\Model\\Locale')) {
15
    return;
16
}
17
18
/**
19
 * Class FluentExtension
20
 *
21
 * @package Firesphere\SolrSearch\Compat
22
 * @property DocumentFactory|BaseIndex|SchemaService|FluentExtension $owner
23
 */
24
class FluentExtension extends Extension
25
{
26
    protected $fieldLocale;
27
    /**
28
     * Add the needed language copy fields to Solr
29
     */
30
    public function onAfterInit()
31
    {
32
        $locales = Locale::get()->exclude(['IsGlobalDefault' => true]);
33
        /** @var BaseIndex $owner */
34
        $owner = $this->owner;
35
        $copyFields = $owner->getCopyFields();
36
        /** @var Locale $locale */
37
        foreach ($locales as $locale) {
38
            foreach ($copyFields as $copyField => $values) {
39
                $owner->addCopyField($locale->Locale . $copyField, $values);
40
            }
41
        }
42
    }
43
44
    public function onAfterFieldDefinition($data, $item)
45
    {
46
        $locales = Locale::get()->exclude(['IsGlobalDefault' => true]);
47
48
        foreach ($locales as $locale) {
49
            $isDest = strpos($item['Destination'], $locale->Locale);
50
            if ($isDest === 0 || $item['Destination'] === null) {
51
                $copy = $item;
52
                $copy['Field'] = $item['Field'] . '_' . $locale->Locale;
53
                $data->push($copy);
54
            }
55
        }
56
    }
57
58
    public function onBeforeAddDoc(&$field, &$value)
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

58
    public function onBeforeAddDoc(&$field, /** @scrutinizer ignore-unused */ &$value)

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...
59
    {
60
        $fluentState = FluentState::singleton();
61
        $locale = $fluentState->getLocale();
62
        /** @var Locale $defaultLocale */
63
        $defaultLocale = Locale::get()->filter(['IsGlobalDefault' => true])->first();
64
        if ($locale !== $defaultLocale->Locale) {
65
            $field['name'] .= '_' . $locale;
66
        }
67
    }
68
69
    /**
70
     * @param BaseQuery $query
71
     * @param Query $clientQuery
72
     */
73
    public function onBeforeSearch($query, $clientQuery)
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

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

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...
74
    {
75
        $locale = FluentState::singleton()->getLocale();
76
        $defaultLocale = Locale::get()->filter(['IsGlobalDefault' => true])->first();
77
        if ($locale !== $defaultLocale->Locale) {
78
            $defaultField = $clientQuery->getQueryDefaultField() ?: '_text';
79
            $clientQuery->setQueryDefaultField($locale . $defaultField);
80
        }
81
    }
82
}
83