Passed
Push — master ( a1274e...f47e0d )
by Simon
12:16 queued 02:17
created

FluentSiteState::updatePart()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\SolrFluent\States;
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;
10
use TractorCow\Fluent\Model\Locale;
11
use TractorCow\Fluent\State\FluentState;
12
13
/**
14
 * Class FluentSiteState
15
 *
16
 * @package Firesphere\SolrFluent
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) &&
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) && count($locales);
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|null
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
     * Update the Solr query to match the current State
81
     *
82
     * @param BaseQuery $query
83
     */
84
    public function updateQuery(&$query)
85
    {
86
        $locale = FluentState::singleton()->getLocale();
87
        if ($locale === '') {
88
            return;
89
        }
90
91
        $this->updatePart($query, $locale, 'BoostedFields');
92
        $this->updatePart($query, $locale, 'Filter');
93
        $this->updatePart($query, $locale, 'Exclude');
94
95
        $fields = [];
96
        foreach ($query->getFields() as $field) {
97
            $fields[] = $field . '_' . $locale;
98
        }
99
        $query->setFields($fields);
100
101
        $localisedTerms = [];
102
        foreach ($query->getTerms() as $term) {
103
            if (count($term['fields'])) {
104
                foreach ($term['fields'] as &$termField) {
105
                    $termField .= '_' . $locale;
106
                }
107
                unset($termField);
108
            }
109
            $localisedTerms[] = $term;
110
        }
111
        $query->setTerms($localisedTerms);
112
    }
113
114
    /**
115
     * Update a part of the query for the get and set methods.
116
     *
117
     * @param $query
118
     * @param string $locale
119
     * @param string $method
120
     */
121
    protected function updatePart(&$query, string $locale, string $method): void
122
    {
123
        if (!$locale) {
124
            return;
125
        }
126
        $new = [];
127
        $getMethod = 'get' . $method;
128
        $setMethod = 'set' . $method;
129
        foreach ($query->$getMethod() as $filterField => $value) {
130
            $fieldName = $filterField . '_' . $locale;
131
            $new[$fieldName] = $value;
132
        }
133
        $query->$setMethod($new);
134
    }
135
}
136