Passed
Push — master ( 4ad0c6...ebd289 )
by Simon
08:50
created

FluentSiteState::updatePart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Firesphere\SolrSearch\Fluent;
4
5
use Firesphere\SolrSearch\Interfaces\SiteStateInterface;
6
use Firesphere\SolrSearch\Queries\BaseQuery;
7
use Firesphere\SolrSearch\States\SiteState;
8
use ReflectionException;
9
use TractorCow\Fluent\Extension\FluentExtension;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Firesphere\SolrSearch\Fluent\FluentExtension. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use TractorCow\Fluent\Model\Locale;
11
use TractorCow\Fluent\State\FluentState;
12
13
/**
14
 * Class FluentSiteState
15
 *
16
 * @package Firesphere\SolrSearch\Fluent
17
 */
18
class FluentSiteState extends SiteState implements SiteStateInterface
19
{
20
    /**
21
     * Does the state apply to this class
22
     *
23
     * @param $class
24
     * @return bool
25
     * @throws ReflectionException
26
     */
27
    public function appliesTo($class): bool
28
    {
29
        return $this->isEnabled() &&
30
            SiteState::hasExtension($class, FluentExtension::class, true) &&
0 ignored issues
show
Unused Code introduced by
The call to Firesphere\SolrSearch\St...teState::hasExtension() has too many arguments starting with true. ( Ignorable by Annotation )

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

30
            SiteState::/** @scrutinizer ignore-call */ 
31
                       hasExtension($class, FluentExtension::class, true) &&

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31
            Locale::getCached()->count();
32
    }
33
34
    /**
35
     * Is this state applicable to this extension
36
     *
37
     * @param string $state
38
     * @return bool
39
     */
40
    public function stateIsApplicable($state): bool
41
    {
42
        $locales = Locale::get()->column('Locale');
43
44
        return in_array($state, $locales, true);
45
    }
46
47
    /**
48
     * Reset the SiteState to it's default state
49
     *
50
     * @param string|null $state
51
     * @return mixed
52
     */
53
    public function setDefaultState($state = null)
54
    {
55
        FluentState::singleton()->setLocale($state);
56
    }
57
58
    /**
59
     * Return the current state of the site
60
     *
61
     * @return string
62
     */
63
    public function currentState(): string
64
    {
65
        return FluentState::singleton()->getLocale();
66
    }
67
68
    /**
69
     * Activate a given state. This should only be done if the state is applicable
70
     *
71
     * @param string $state
72
     * @return mixed
73
     */
74
    public function activateState($state)
75
    {
76
        FluentState::singleton()->setLocale($state);
77
    }
78
79
    /**
80
     * @param BaseQuery $query
81
     */
82
    public function updateQuery(&$query)
83
    {
84
        $locale = FluentState::singleton()->getLocale();
85
86
        $this->updatePart($query, $locale, 'BoostedFields');
87
        $this->updatePart($query, $locale, 'Filter');
88
        $this->updatePart($query, $locale, 'Exclude');
89
90
        $fields = [];
91
        foreach ($query->getFields() as $field) {
92
            $fields[] = $field . '_' . $locale;
93
        }
94
        $query->setFields($fields);
95
96
        $localisedTerms = [];
97
        foreach ($query->getTerms() as $term) {
98
            if (count($term['fields'])) {
99
                foreach ($term['fields'] as &$termField) {
100
                    $termField .= '_' . $locale;
101
                }
102
            }
103
            $localisedTerms[] = $term;
104
        }
105
        $query->setTerms($localisedTerms);
106
    }
107
108
    /**
109
     * @param $query
110
     * @param string $locale
111
     * @param string $method
112
     */
113
    protected function updatePart(&$query, string $locale, string $method): void
114
    {
115
        $new = [];
116
        $getMethod = 'get' . $method;
117
        $setMethod = 'set' . $method;
118
        foreach ($query->$getMethod() as $filterField => $value) {
119
            $fieldName = $filterField . '_' . $locale;
120
            $new[$fieldName] = $value;
121
        }
122
        $query->$setMethod($new);
123
    }
124
}
125