Completed
Push — release-2.x ( 4fbb12...8ca245 )
by
unknown
10:19
created

ViewConfig::getTranslation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
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\StepupRa\SamlStepupProviderBundle\Provider;
20
21
use Surfnet\StepupRa\RaBundle\Exception\LogicException;
22
use Symfony\Component\HttpFoundation\Request;
23
24
class ViewConfig
25
{
26
27
    /**
28
     * @var array
29
     */
30
    public $pageTitle;
31
32
    /**
33
     * @var array
34
     */
35
    public $explanation;
36
37
    /**
38
     * @var array
39
     */
40
    public $initiate;
41
42
    /**
43
     * @var array
44
     */
45
    public $gssfIdMismatch;
46
47
    /**
48
     * @var Request
49
     */
50
    private $request;
51
52
    /**
53
     * The arrays are arrays of translated text, indexed on locale.
54
     *
55
     * @param Request $request
56
     * @param array $pageTitle
57
     * @param array $explanation
58
     * @param array $initiate
59
     * @param array $gssfIdMismatch
60
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
61
     */
62
    public function __construct(
63
        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...
64
        array $pageTitle,
65
        array $explanation,
66
        array $initiate,
67
        array $gssfIdMismatch
68
    ) {
69
        $this->request = $request;
70
        $this->pageTitle = $pageTitle;
71
        $this->explanation = $explanation;
72
        $this->initiate = $initiate;
73
        $this->gssfIdMismatch = $gssfIdMismatch;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getExplanation()
80
    {
81
        return $this->getTranslation($this->explanation);
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getGssfIdMismatch()
88
    {
89
        return $this->getTranslation($this->gssfIdMismatch);
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getInitiate()
96
    {
97
        return $this->getTranslation($this->initiate);
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getPageTitle()
104
    {
105
        return $this->getTranslation($this->pageTitle);
106
    }
107
108
    /**
109
     * @param array $translations
110
     * @return mixed
111
     * @throws LogicException
112
     */
113
    private function getTranslation(array $translations)
114
    {
115
        $currentLocale = $this->request->getLocale();
116
        if (is_null($currentLocale)) {
117
            throw new LogicException('The current language is not set');
118
        }
119
        if (isset($translations[$currentLocale])) {
120
            return $translations[$currentLocale];
121
        }
122
        throw new LogicException(
123
            sprintf(
124
                'The requested translation is not available in this language: %s. Available languages: %s',
125
                $currentLocale,
126
                implode(', ', array_keys($translations))
127
            )
128
        );
129
    }
130
}
131