Completed
Pull Request — develop (#149)
by
unknown
02:31 queued 12s
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
     * @var array
28
     */
29
    public $name;
30
31
    /**
32
     * @var array
33
     */
34
    public $pageTitle;
35
36
    /**
37
     * @var array
38
     */
39
    public $explanation;
40
41
    /**
42
     * @var array
43
     */
44
    public $initiate;
45
46
    /**
47
     * @var array
48
     */
49
    public $gssfIdMismatch;
50
51
    /**
52
     * @var Request
53
     */
54
    private $request;
55
56
    /**
57
     * The arrays are arrays of translated text, indexed on locale.
58
     *
59
     * @param Request $request
60
     * @param array $name
61
     * @param array $pageTitle
62
     * @param array $explanation
63
     * @param array $initiate
64
     * @param array $gssfIdMismatch
65
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
66
     */
67
    public function __construct(
68
        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...
69
        array $name,
70
        array $pageTitle,
71
        array $explanation,
72
        array $initiate,
73
        array $gssfIdMismatch
74
    ) {
75
        $this->request = $request;
76
        $this->name = $name;
77
        $this->pageTitle = $pageTitle;
78
        $this->explanation = $explanation;
79
        $this->initiate = $initiate;
80
        $this->gssfIdMismatch = $gssfIdMismatch;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getName()
87
    {
88
        return $this->getTranslation($this->name);
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getExplanation()
95
    {
96
        return $this->getTranslation($this->explanation);
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getGssfIdMismatch()
103
    {
104
        return $this->getTranslation($this->gssfIdMismatch);
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getInitiate()
111
    {
112
        return $this->getTranslation($this->initiate);
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function getPageTitle()
119
    {
120
        return $this->getTranslation($this->pageTitle);
121
    }
122
123
    /**
124
     * @param array $translations
125
     * @return mixed
126
     * @throws LogicException
127
     */
128
    private function getTranslation(array $translations)
129
    {
130
        $currentLocale = $this->request->getLocale();
131
        if (is_null($currentLocale)) {
132
            throw new LogicException('The current language is not set');
133
        }
134
        if (isset($translations[$currentLocale])) {
135
            return $translations[$currentLocale];
136
        }
137
        throw new LogicException(
138
            sprintf(
139
                'The requested translation is not available in this language: %s. Available languages: %s',
140
                $currentLocale,
141
                implode(', ', array_keys($translations))
142
            )
143
        );
144
    }
145
}
146