Passed
Push — master ( 71d298...7eaa92 )
by Simon
02:18
created

FluentSiteState::updateQuery()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 2
b 0
f 0
nc 9
nop 1
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 9.2222
1
<?php
2
/**
3
 * Class FluentSiteState|Firesphere\SolrFluent\States\FluentSiteState Set the site state for each indexing group for
4
 * Fluent translations
5
 *
6
 * @package Firesphere\SolrFluent\States
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\SolrFluent\States;
12
13
use Firesphere\SolrSearch\Interfaces\SiteStateInterface;
14
use Firesphere\SolrSearch\Queries\BaseQuery;
15
use Firesphere\SolrSearch\States\SiteState;
16
use ReflectionException;
17
use TractorCow\Fluent\Extension\FluentExtension;
18
use TractorCow\Fluent\Model\Locale;
19
use TractorCow\Fluent\State\FluentState;
20
21
/**
22
 * Class \Firesphere\SolrFluent\States\FluentSiteState
23
 *
24
 * Manage the state of the site to apply the correct locale from Fluent to search
25
 *
26
 * @package Firesphere\SolrFluent\States
27
 */
28
class FluentSiteState extends SiteState implements SiteStateInterface
29
{
30
    /**
31
     * @var array get/set methods that needs to be called to update the query
32
     */
33
    private static $methods = [
34
        'BoostedFields',
35
        'Filter',
36
        'Exclude'
37
    ];
38
    /**
39
     * Does the state apply to this class
40
     *
41
     * @param string $class Class to check
42
     * @return bool
43
     * @throws ReflectionException
44
     */
45 1
    public function appliesTo($class): bool
46
    {
47 1
        return $this->isEnabled() &&
48 1
            SiteState::hasExtension($class, FluentExtension::class) &&
49 1
            Locale::getCached()->count();
50
    }
51
52
    /**
53
     * Is this state applicable to this extension
54
     *
55
     * @param string $state State to validate
56
     * @return bool
57
     */
58 1
    public function stateIsApplicable($state): bool
59
    {
60 1
        $locales = Locale::get()->column('Locale');
61
62 1
        return in_array($state, $locales, true) && count($locales);
63
    }
64
65
    /**
66
     * Reset the SiteState to it's default state
67
     * Stub method for readability
68
     *
69
     * @param string|null $state Reset to default state
70
     * @return mixed
71
     */
72 1
    public function setDefaultState($state = null)
73
    {
74 1
        $this->activateState($state);
75 1
    }
76
77
    /**
78
     * Return the current state of the site
79
     *
80
     * @return string|null
81
     */
82 1
    public function currentState()
83
    {
84 1
        return FluentState::singleton()->getLocale();
85
    }
86
87
    /**
88
     * Activate a given state. This should only be done if the state is applicable
89
     *
90
     * @param string $state Activate the given state
91
     * @return mixed
92
     */
93 2
    public function activateState($state)
94
    {
95 2
        FluentState::singleton()->setLocale($state);
96 2
    }
97
98
    /**
99
     * Update the Solr query to match the current State
100
     *
101
     * @param BaseQuery $query Query to update
102
     */
103 1
    public function updateQuery(&$query)
104
    {
105 1
        $locale = FluentState::singleton()->getLocale();
106 1
        if ($locale === '' || !$locale) {
107 1
            return;
108
        }
109
110 1
        foreach (self::$methods as $method) {
111 1
            $this->updatePart($query, $locale, $method);
112
        }
113
114 1
        $fields = [];
115 1
        foreach ($query->getFields() as $field) {
116
            $fields[] = $field . '_' . $locale;
117
        }
118 1
        $query->setFields($fields);
119
120 1
        $localisedTerms = [];
121 1
        foreach ($query->getTerms() as $term) {
122 1
            $localisedTerms = $this->updateTerms($term, $locale, $localisedTerms);
123
        }
124 1
        $query->setTerms($localisedTerms);
125 1
    }
126
127
    /**
128
     * Update a part of the query for the get and set methods.
129
     *
130
     * @param BaseQuery $query Query that needs updating for the given method
131
     * @param string $locale Localisation to use
132
     * @param string $method Get method to call
133
     */
134 1
    protected function updatePart(&$query, string $locale, string $method): void
135
    {
136 1
        $new = [];
137 1
        $getMethod = 'get' . $method;
138 1
        $setMethod = 'set' . $method;
139 1
        foreach ($query->$getMethod() as $filterField => $value) {
140 1
            $fieldName = $filterField . '_' . $locale;
141 1
            $new[$fieldName] = $value;
142
        }
143 1
        $query->$setMethod($new);
144 1
    }
145
146
    /**
147
     * Update the field filters to localised filters
148
     *
149
     * @param string|array $term Array of terms
150
     * @param string $locale Localisation to use
151
     * @param array $localisedTerms Currently localised terms
152
     * @return array
153
     */
154 1
    protected function updateTerms($term, string $locale, array $localisedTerms): array
155
    {
156 1
        if (count($term['fields'])) {
157
            foreach ($term['fields'] as &$termField) {
158
                $termField .= '_' . $locale;
159
            }
160
            unset($termField);
161
        }
162 1
        $localisedTerms[] = $term;
163
164 1
        return $localisedTerms;
165
    }
166
}
167