Completed
Push — feature/gssp_app_urls ( 06701c )
by
unknown
02:08
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\StepupSelfService\SelfServiceBundle\Exception\LogicException;
22
use Symfony\Component\HttpFoundation\Request;
23
24
class ViewConfig
25
{
26
    /**
27
     * @var string
28
     */
29
    private $loa;
30
31
    /**
32
     * @var string
33
     */
34
    private $logo;
35
36
    /**
37
     * @var array
38
     */
39
    private $alt;
40
41
    /**
42
     * @var array
43
     */
44
    private $title;
45
46
    /**
47
     * @var array
48
     */
49
    private $description;
50
51
    /**
52
     * @var array
53
     */
54
    private $buttonUse;
55
56
    /**
57
     * @var array
58
     */
59
    private $initiateTitle;
60
61
    /**
62
     * @var array
63
     */
64
    private $initiateButton;
65
66
    /**
67
     * @var array
68
     */
69
    private $explanation;
70
71
    /**
72
     * @var array
73
     */
74
    private $authnFailed;
75
76
    /**
77
     * @var array
78
     */
79
    private $popFailed;
80
81
    /**
82
     * @var Request
83
     */
84
    private $request;
85
86
    /**
87
     * @var string
88
     */
89
    private $androidUrl;
90
91
    /**
92
     * @var string
93
     */
94
    private $iosUrl;
95
96
    /**
97
     * The arrays are arrays of translated text, indexed on locale.
98
     *
99
     * @param Request $request
100
     * @param string $loa
101
     * @param string $logo
102
     * @param string $androidUrl
103
     * @param string $iosUrl
104
     * @param array $alt
105
     * @param array $title
106
     * @param array $description
107
     * @param array $buttonUse
108
     * @param array $initiateTitle
109
     * @param array $initiateButton
110
     * @param array $explanation
111
     * @param array $authnFailed
112
     * @param array $popFailed
113
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
114
     */
115
    public function __construct(
116
        Request $request,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
117
        $loa,
118
        $logo,
119
        $androidUrl,
120
        $iosUrl,
121
        array $alt,
122
        array $title,
123
        array $description,
124
        array $buttonUse,
125
        array $initiateTitle,
126
        array $initiateButton,
127
        array $explanation,
128
        array $authnFailed,
129
        array $popFailed
130
    ) {
131
        $this->loa = $loa;
132
        $this->logo = $logo;
133
        $this->androidUrl = $androidUrl;
134
        $this->iosUrl = $iosUrl;
135
        $this->alt = $alt;
136
        $this->title = $title;
137
        $this->description = $description;
138
        $this->buttonUse = $buttonUse;
139
        $this->initiateTitle = $initiateTitle;
140
        $this->initiateButton = $initiateButton;
141
        $this->explanation = $explanation;
142
        $this->authnFailed = $authnFailed;
143
        $this->popFailed = $popFailed;
144
        $this->request = $request;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getLogo()
151
    {
152
        return $this->logo;
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getLoa()
159
    {
160
        return $this->loa;
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getTitle()
167
    {
168
        return $this->getTranslation($this->title);
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getAlt()
175
    {
176
        return $this->getTranslation($this->alt);
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getDescription()
183
    {
184
        return $this->getTranslation($this->description);
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function getButtonUse()
191
    {
192
        return $this->getTranslation($this->buttonUse);
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getInitiateTitle()
199
    {
200
        return $this->getTranslation($this->initiateTitle);
201
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getInitiateButton()
207
    {
208
        return $this->getTranslation($this->initiateButton);
209
    }
210
211
    /**
212
     * @return string
213
     */
214
    public function getExplanation()
215
    {
216
        return $this->getTranslation($this->explanation);
217
    }
218
219
    /**
220
     * @return string
221
     */
222
    public function getAuthnFailed()
223
    {
224
        return $this->getTranslation($this->authnFailed);
225
    }
226
227
    /**
228
     * @return string
229
     */
230
    public function getPopFailed()
231
    {
232
        return $this->getTranslation($this->popFailed);
233
    }
234
235
    /**
236
     * @return string
237
     */
238
    public function getAndroidUrl()
239
    {
240
        return $this->androidUrl;
241
    }
242
243
    /**
244
     * @return string
245
     */
246
    public function getIosUrl()
247
    {
248
        return $this->iosUrl;
249
    }
250
251
    /**
252
     * @param array $translations
253
     * @return mixed
254
     * @throws LogicException
255
     */
256
    private function getTranslation(array $translations)
257
    {
258
        $currentLocale = $this->request->getLocale();
259
        if (is_null($currentLocale)) {
260
            throw new LogicException('The current language is not set');
261
        }
262
        if (isset($translations[$currentLocale])) {
263
            return $translations[$currentLocale];
264
        }
265
        throw new LogicException(
266
            sprintf(
267
                'The requested translation is not available in this language: %s. Available languages: %s',
268
                $currentLocale,
269
                implode(', ', array_keys($translations))
270
            )
271
        );
272
    }
273
}
274