Completed
Push — for-qa-ezp-30690-too-early-cr-... ( 9061bc )
by
unknown
51:53 queued 15:49
created

AbstractLanguageResolver::getContextLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\SiteAccessAware\Language;
10
11
use eZ\Publish\API\Repository\LanguageResolver as APILanguageResolver;
12
use eZ\Publish\API\Repository\Values\Content\Language;
13
14
/**
15
 * Common abstract implementation of Language resolver.
16
 *
17
 * @internal
18
 */
19
abstract class AbstractLanguageResolver implements APILanguageResolver
20
{
21
    /**
22
     * @var bool
23
     */
24
    private $defaultUseAlwaysAvailable;
25
26
    /**
27
     * @var bool
28
     */
29
    private $defaultShowAllTranslations;
30
31
    /**
32
     * Values typically provided by user context, will need to be set depending on your own custom logic using setter.
33
     *
34
     * E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both
35
     *      UI strings as well as default languages to prioritize for repository objects.
36
     *
37
     * If set, this will have priority over configured languages.
38
     *
39
     * @var string|null
40
     */
41
    private $contextLanguage;
42
43
    /**
44
     * @param bool $defaultUseAlwaysAvailable
45
     * @param bool $defaultShowAllTranslations
46
     */
47
    public function __construct(
48
        bool $defaultUseAlwaysAvailable = true,
49
        bool $defaultShowAllTranslations = false
50
    ) {
51
        $this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
52
        $this->defaultShowAllTranslations = $defaultShowAllTranslations;
53
    }
54
55
    /**
56
     * Get list of languages configured via dedicated layer.
57
     *
58
     * @return string[]
59
     */
60
    abstract protected function getConfiguredLanguages(): array;
61
62
    /**
63
     * For use by custom events / logic setting language for all retrieved objects from repository.
64
     *
65
     * User language will, if set, will have prepended before configured languages. But in cases PHP API consumer
66
     * specifies languages to retrieve repository objects in it will instead be appended as a fallback.
67
     *
68
     * If set, this will have priority over configured languages.
69
     *
70
     * @param string|null $contextLanguage
71
     */
72
    final public function setContextLanguage(?string $contextLanguage): void
73
    {
74
        $this->contextLanguage = $contextLanguage;
75
    }
76
77
    /**
78
     * Get context language currently set by custom logic.
79
     *
80
     * @return null|string
81
     */
82
    final public function getContextLanguage(): ?string
83
    {
84
        return $this->contextLanguage;
85
    }
86
87
    /**
88
     * Get prioritized languages taking into account forced and context languages.
89
     *
90
     * @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set.
91
     *
92
     * @return array
93
     */
94
    final public function getPrioritizedLanguages(?array $forcedLanguages = null): array
95
    {
96
        // Skip if languages param has been set by API user
97
        if ($forcedLanguages !== null) {
98
            return $forcedLanguages;
99
        }
100
101
        // Detect if we should load all languages by default
102
        if ($this->defaultShowAllTranslations) {
103
            return Language::ALL;
104
        }
105
106
        // create language based on context and configuration, where context language is made most important one
107
        $languages = [];
108
        if ($this->contextLanguage !== null) {
109
            $languages[] = $this->contextLanguage;
110
        }
111
112
        return array_values(array_unique(array_merge($languages, $this->getConfiguredLanguages())));
113
    }
114
115
    /**
116
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
117
     *
118
     * @param bool $defaultUseAlwaysAvailable
119
     */
120
    final public function setDefaultUseAlwaysAvailable(bool $defaultUseAlwaysAvailable): void
121
    {
122
        $this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
123
    }
124
125
    /**
126
     * Get currently set UseAlwaysAvailable.
127
     *
128
     * @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value,
129
     *        typically arguments provided to API.
130
     *
131
     * @return bool
132
     */
133
    final public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null): bool
134
    {
135
        if ($forcedUseAlwaysAvailable !== null) {
136
            return $forcedUseAlwaysAvailable;
137
        }
138
139
        return $this->defaultUseAlwaysAvailable;
140
    }
141
142
    /**
143
     * For use by event listening to config resolver scope changes (or other event changing configured languages).
144
     *
145
     * @param bool $defaultShowAllTranslations
146
     */
147
    final public function setShowAllTranslations(bool $defaultShowAllTranslations): void
148
    {
149
        $this->defaultShowAllTranslations = $defaultShowAllTranslations;
150
    }
151
152
    /**
153
     * Get currently set showAllTranslations.
154
     *
155
     * @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value,
156
     *        typically arguments provided to API.
157
     *
158
     * @return bool
159
     */
160
    final public function getShowAllTranslations(?bool $forcedShowAllTranslations = null): bool
161
    {
162
        if ($forcedShowAllTranslations !== null) {
163
            return $forcedShowAllTranslations;
164
        }
165
166
        return $this->defaultShowAllTranslations;
167
    }
168
}
169