Completed
Push — ezp-25721-use-config-resolver-... ( 29ec7b )
by
unknown
24:40 queued 18s
created

LanguageResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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