Passed
Push — master ( f996b3...a29f24 )
by Simon
01:59
created

FluentSiteState::updateTerms()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 10
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
     * Does the state apply to this class
32
     *
33
     * @param $class
34
     * @return bool
35
     * @throws ReflectionException
36
     */
37 1
    public function appliesTo($class): bool
38
    {
39 1
        return $this->isEnabled() &&
40 1
            SiteState::hasExtension($class, FluentExtension::class) &&
41 1
            Locale::getCached()->count();
42
    }
43
44
    /**
45
     * Is this state applicable to this extension
46
     *
47
     * @param string $state
48
     * @return bool
49
     */
50 1
    public function stateIsApplicable($state): bool
51
    {
52 1
        $locales = Locale::get()->column('Locale');
53
54 1
        return in_array($state, $locales, true) && count($locales);
55
    }
56
57
    /**
58
     * Reset the SiteState to it's default state
59
     *
60
     * @param string|null $state
61
     * @return mixed
62
     */
63 1
    public function setDefaultState($state = null)
64
    {
65 1
        FluentState::singleton()->setLocale($state);
66 1
    }
67
68
    /**
69
     * Return the current state of the site
70
     *
71
     * @return string|null
72
     */
73 1
    public function currentState()
74
    {
75 1
        return FluentState::singleton()->getLocale();
76
    }
77
78
    /**
79
     * Activate a given state. This should only be done if the state is applicable
80
     *
81
     * @param string $state
82
     * @return mixed
83
     */
84 1
    public function activateState($state)
85
    {
86 1
        FluentState::singleton()->setLocale($state);
87 1
    }
88
89
    /**
90
     * Update the Solr query to match the current State
91
     *
92
     * @param BaseQuery $query
93
     */
94
    public function updateQuery(&$query)
95
    {
96
        $locale = FluentState::singleton()->getLocale();
97
        if ($locale === '') {
98
            return;
99
        }
100
101
        $this->updatePart($query, $locale, 'BoostedFields');
102
        $this->updatePart($query, $locale, 'Filter');
103
        $this->updatePart($query, $locale, 'Exclude');
104
105
        $fields = [];
106
        foreach ($query->getFields() as $field) {
107
            $fields[] = $field . '_' . $locale;
108
        }
109
        $query->setFields($fields);
110
111
        $localisedTerms = [];
112
        foreach ($query->getTerms() as $term) {
113
            $localisedTerms = $this->updateTerms($term, $locale, $localisedTerms);
114
        }
115
        $query->setTerms($localisedTerms);
116
    }
117
118
    /**
119
     * Update a part of the query for the get and set methods.
120
     *
121
     * @param $query
122
     * @param string $locale
123
     * @param string $method
124
     */
125
    protected function updatePart(&$query, string $locale, string $method): void
126
    {
127
        if (!$locale) {
128
            return;
129
        }
130
        $new = [];
131
        $getMethod = 'get' . $method;
132
        $setMethod = 'set' . $method;
133
        foreach ($query->$getMethod() as $filterField => $value) {
134
            $fieldName = $filterField . '_' . $locale;
135
            $new[$fieldName] = $value;
136
        }
137
        $query->$setMethod($new);
138
    }
139
140
    /**
141
     * Update the field filters to localised filters
142
     *
143
     * @param $term
144
     * @param string $locale
145
     * @param array $localisedTerms
146
     * @return array
147
     */
148
    private function updateTerms($term, string $locale, array $localisedTerms): array
149
    {
150
        if (count($term['fields'])) {
151
            foreach ($term['fields'] as &$termField) {
152
                $termField .= '_' . $locale;
153
            }
154
            unset($termField);
155
        }
156
        $localisedTerms[] = $term;
157
158
        return $localisedTerms;
159
    }
160
}
161