Completed
Push — master ( 990ac1...1ea292 )
by
unknown
02:16
created

ViewConfig::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 31
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 29
nc 1
nop 14

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Copyright 2017 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider;
20
21
use Surfnet\StepupBundle\Value\Provider\ViewConfigInterface;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\LogicException;
23
use Symfony\Component\HttpFoundation\RequestStack;
24
25
class ViewConfig implements ViewConfigInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $loa;
31
32
    /**
33
     * @var string
34
     */
35
    private $logo;
36
37
    /**
38
     * @var array
39
     */
40
    private $alt;
41
42
    /**
43
     * @var array
44
     */
45
    private $title;
46
47
    /**
48
     * @var array
49
     */
50
    private $description;
51
52
    /**
53
     * @var array
54
     */
55
    private $buttonUse;
56
57
    /**
58
     * @var array
59
     */
60
    private $initiateTitle;
61
62
    /**
63
     * @var array
64
     */
65
    private $initiateButton;
66
67
    /**
68
     * @var array
69
     */
70
    private $explanation;
71
72
    /**
73
     * @var array
74
     */
75
    private $authnFailed;
76
77
    /**
78
     * @var array
79
     */
80
    private $popFailed;
81
82
    /**
83
     * @var RequestStack
84
     */
85
    private $requestStack;
86
87
    /**
88
     * @var string
89
     */
90
    private $androidUrl;
91
92
    /**
93
     * @var string
94
     */
95
    private $iosUrl;
96
97
    /**
98
     * The arrays are arrays of translated text, indexed on locale.
99
     *
100
     * @param RequestStack $requestStack
101
     * @param string $loa
102
     * @param string $logo
103
     * @param string $androidUrl
104
     * @param string $iosUrl
105
     * @param array $alt
106
     * @param array $title
107
     * @param array $description
108
     * @param array $buttonUse
109
     * @param array $initiateTitle
110
     * @param array $initiateButton
111
     * @param array $explanation
112
     * @param array $authnFailed
113
     * @param array $popFailed
114
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
115
     */
116
    public function __construct(
117
        RequestStack $requestStack,
118
        $loa,
119
        $logo,
120
        $androidUrl,
121
        $iosUrl,
122
        array $alt,
123
        array $title,
124
        array $description,
125
        array $buttonUse,
126
        array $initiateTitle,
127
        array $initiateButton,
128
        array $explanation,
129
        array $authnFailed,
130
        array $popFailed
131
    ) {
132
        $this->loa = $loa;
133
        $this->logo = $logo;
134
        $this->androidUrl = $androidUrl;
135
        $this->iosUrl = $iosUrl;
136
        $this->alt = $alt;
137
        $this->title = $title;
138
        $this->description = $description;
139
        $this->buttonUse = $buttonUse;
140
        $this->initiateTitle = $initiateTitle;
141
        $this->initiateButton = $initiateButton;
142
        $this->explanation = $explanation;
143
        $this->authnFailed = $authnFailed;
144
        $this->popFailed = $popFailed;
145
        $this->requestStack = $requestStack;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getLogo()
152
    {
153
        return $this->logo;
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getLoa()
160
    {
161
        return $this->loa;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getTitle()
168
    {
169
        return $this->getTranslation($this->title);
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getAlt()
176
    {
177
        return $this->getTranslation($this->alt);
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function getDescription()
184
    {
185
        return $this->getTranslation($this->description);
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function getButtonUse()
192
    {
193
        return $this->getTranslation($this->buttonUse);
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getInitiateTitle()
200
    {
201
        return $this->getTranslation($this->initiateTitle);
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getInitiateButton()
208
    {
209
        return $this->getTranslation($this->initiateButton);
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function getExplanation()
216
    {
217
        return $this->getTranslation($this->explanation);
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    public function getAuthnFailed()
224
    {
225
        return $this->getTranslation($this->authnFailed);
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getPopFailed()
232
    {
233
        return $this->getTranslation($this->popFailed);
234
    }
235
236
    /**
237
     * @return string
238
     */
239
    public function getAndroidUrl()
240
    {
241
        return $this->androidUrl;
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function getIosUrl()
248
    {
249
        return $this->iosUrl;
250
    }
251
252
    /**
253
     * @param array $translations
254
     * @return mixed
255
     * @throws LogicException
256
     */
257
    private function getTranslation(array $translations)
258
    {
259
        $currentLocale = $this->requestStack->getCurrentRequest()->getLocale();
260
        if (is_null($currentLocale)) {
261
            throw new LogicException('The current language is not set');
262
        }
263
        if (isset($translations[$currentLocale])) {
264
            return $translations[$currentLocale];
265
        }
266
        throw new LogicException(
267
            sprintf(
268
                'The requested translation is not available in this language: %s. Available languages: %s',
269
                $currentLocale,
270
                implode(', ', array_keys($translations))
271
            )
272
        );
273
    }
274
}
275