Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.36% |
75 / 83 |
|
83.33% |
10 / 12 |
CRAP | |
0.00% |
0 / 1 |
Index | |
90.36% |
75 / 83 |
|
83.33% |
10 / 12 |
28.70 | |
0.00% |
0 / 1 |
readResponse | |
100.00% |
46 / 46 |
|
100.00% |
1 / 1 |
3 | |||
getConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultTemplate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getLanguageConfig | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentLanguage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getQueryStringWithLang | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getTranslations | |
30.00% |
3 / 10 |
|
0.00% |
0 / 1 |
23.81 | |||
getAvailableLanguages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDepartment | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
shouldRedirectToScope | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
hasDisabledButton | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
4.25 | |||
getQueryString | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | /** |
4 | * |
5 | * @package Zmsticketprinter |
6 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
7 | * |
8 | */ |
9 | |
10 | namespace BO\Zmsticketprinter; |
11 | |
12 | use BO\Mellon\Unvalidated; |
13 | use BO\Mellon\Valid; |
14 | use BO\Mellon\Validator; |
15 | use BO\Slim\Render; |
16 | use BO\Zmsentities\Ticketprinter; |
17 | use Psr\Http\Message\RequestInterface; |
18 | use Psr\Http\Message\ResponseInterface; |
19 | |
20 | class Index extends BaseController |
21 | { |
22 | /** |
23 | * @SuppressWarnings(UnusedFormalParameter) |
24 | * @return ResponseInterface |
25 | */ |
26 | public function readResponse(RequestInterface $request, ResponseInterface $response, array $args) |
27 | { |
28 | Helper\HomeUrl::create($request); |
29 | $config = $this->getConfig(); |
30 | |
31 | $validator = $request->getAttribute('validator'); |
32 | $defaultTemplate = $this->getDefaultTemplate($validator); |
33 | $languageConfig = $this->getLanguageConfig($validator); |
34 | |
35 | $currentLang = $this->getCurrentLanguage($validator); |
36 | $queryString = $this->getQueryStringWithLang(); |
37 | |
38 | $translations = $this->getTranslations($languageConfig, $currentLang); |
39 | $defaultLanguage = $languageConfig['defaultLanguage'] ?? 'de'; |
40 | $languages = $this->getAvailableLanguages($languageConfig); |
41 | |
42 | $ticketprinterHelper = new Helper\Ticketprinter($args, $request); |
43 | $ticketprinter = $ticketprinterHelper->getEntity(); |
44 | |
45 | $ticketprinter->testValid(); |
46 | $scope = $ticketprinter->getScopeList()->getFirst(); |
47 | $department = $this->getDepartment($scope); |
48 | $organisation = $ticketprinterHelper->getOrganisation(); |
49 | |
50 | if ($this->shouldRedirectToScope($ticketprinter)) { |
51 | return Render::redirect( |
52 | 'TicketprinterByScope', |
53 | ['scopeId' => $ticketprinter->buttons[0]['scope']['id']], |
54 | $this->getQueryString($validator, $ticketprinter, $defaultTemplate) |
55 | ); |
56 | } |
57 | |
58 | $template = (new Helper\TemplateFinder($defaultTemplate->getValue())) |
59 | ->setCustomizedTemplate($ticketprinter, $organisation); |
60 | |
61 | return Render::withHtml( |
62 | $response, |
63 | $template->getTemplate(), |
64 | [ |
65 | 'debug' => \App::DEBUG, |
66 | 'refreshInSeconds' => 30, |
67 | 'urlQueryString' => $queryString, |
68 | 'currentLang' => $currentLang, |
69 | 'enabled' => $ticketprinter->isEnabled() |
70 | || !$organisation->getPreference('ticketPrinterProtectionEnabled'), |
71 | 'title' => 'Wartennumer ziehen', |
72 | 'ticketprinter' => $ticketprinter, |
73 | 'organisation' => $organisation, |
74 | 'department' => $department, |
75 | 'buttonDisplay' => $template->getButtonTemplateType($ticketprinter), |
76 | 'config' => $config, |
77 | 'defaultLanguage' => $defaultLanguage, |
78 | 'languages' => $languages, |
79 | 'translations' => $translations, |
80 | 'hasDisabledButton' => $this->hasDisabledButton($ticketprinter) |
81 | ] |
82 | ); |
83 | } |
84 | |
85 | private function getConfig() |
86 | { |
87 | return \App::$http->readGetResult('/config/', [], \App::SECURE_TOKEN)->getEntity(); |
88 | } |
89 | |
90 | private function getDefaultTemplate($validator) |
91 | { |
92 | return $validator->getParameter("template") |
93 | ->isPath() |
94 | ->setDefault('default'); |
95 | } |
96 | |
97 | private function getLanguageConfig($validator) |
98 | { |
99 | $config = $validator->getParameter("config") |
100 | ->isString() |
101 | ->getValue(); |
102 | |
103 | $decoded = base64_decode(str_replace(' ', '+', $config)); |
104 | |
105 | return json_decode($decoded, true); |
106 | } |
107 | |
108 | private function getCurrentLanguage($validator) |
109 | { |
110 | return $validator->getParameter("lang")->isString()->getValue(); |
111 | } |
112 | |
113 | private function getQueryStringWithLang() |
114 | { |
115 | $queryString = $_SERVER['QUERY_STRING'] ?? ''; |
116 | if (!strpos($queryString, 'lang=')) { |
117 | $queryString .= '&lang=de'; |
118 | } |
119 | return str_replace('/&', '', $queryString); |
120 | } |
121 | |
122 | private function getTranslations($languageConfig, $currentLang) |
123 | { |
124 | $translations = ['printText' => '']; |
125 | if ($languageConfig) { |
126 | foreach ($languageConfig['languages'] as $language) { |
127 | if ($language['language'] !== $currentLang) { |
128 | continue; |
129 | } |
130 | foreach ($language['translations'] as $requestId => $translation) { |
131 | $translations[$requestId] = $translation; |
132 | } |
133 | } |
134 | if (empty($currentLang) || $currentLang === 'de') { |
135 | $translations['printText'] = $languageConfig['defaultPrintText'] ?? ''; |
136 | } |
137 | } |
138 | return $translations; |
139 | } |
140 | |
141 | private function getAvailableLanguages($languageConfig) |
142 | { |
143 | return array_column($languageConfig['languages'] ?? [], 'language'); |
144 | } |
145 | |
146 | private function getDepartment($scope) |
147 | { |
148 | return \App::$http->readGetResult('/scope/' . $scope->id . '/department/')->getEntity(); |
149 | } |
150 | |
151 | private function shouldRedirectToScope($ticketprinter) |
152 | { |
153 | return count($ticketprinter->buttons) === 1 && $ticketprinter->buttons[0]['type'] === 'scope'; |
154 | } |
155 | |
156 | private function hasDisabledButton($ticketprinter) |
157 | { |
158 | foreach ($ticketprinter->buttons as $button) { |
159 | if (!isset($button['enabled']) || $button['enabled'] != 1) { |
160 | return true; |
161 | } |
162 | } |
163 | return false; |
164 | } |
165 | |
166 | /** |
167 | * @param Validator $validator |
168 | * @param Ticketprinter $ticketprinter |
169 | * @param Unvalidated|Valid $defaultTemplate |
170 | * @return array |
171 | */ |
172 | protected function getQueryString($validator, $ticketprinter, $defaultTemplate) |
173 | { |
174 | $query = ($defaultTemplate->getValue() === 'default') ? [] : ['template' => $defaultTemplate->getValue()]; |
175 | if (isset($ticketprinter['home'])) { |
176 | $homeUrl = $validator::value($ticketprinter['home'])->isUrl()->getValue(); |
177 | if ($homeUrl) { |
178 | $query['ticketprinter[home]'] = $homeUrl; |
179 | } |
180 | } |
181 | return $query; |
182 | } |
183 | } |