ViewConfig   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 122
rs 10

7 Methods

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