Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.87% |
71 / 79 |
|
83.33% |
10 / 12 |
CRAP | |
0.00% |
0 / 1 |
Index | |
89.87% |
71 / 79 |
|
83.33% |
10 / 12 |
28.81 | |
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% |
1 / 1 |
|
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 | return json_decode(base64_decode(str_replace(' ', '+', $validator->getParameter("config")->isString()->getValue())), true); |
100 | } |
101 | |
102 | private function getCurrentLanguage($validator) |
103 | { |
104 | return $validator->getParameter("lang")->isString()->getValue(); |
105 | } |
106 | |
107 | private function getQueryStringWithLang() |
108 | { |
109 | $queryString = $_SERVER['QUERY_STRING'] ?? ''; |
110 | if (!strpos($queryString, 'lang=')) { |
111 | $queryString .= '&lang=de'; |
112 | } |
113 | return str_replace('/&', '', $queryString); |
114 | } |
115 | |
116 | private function getTranslations($languageConfig, $currentLang) |
117 | { |
118 | $translations = ['printText' => '']; |
119 | if ($languageConfig) { |
120 | foreach ($languageConfig['languages'] as $language) { |
121 | if ($language['language'] !== $currentLang) { |
122 | continue; |
123 | } |
124 | foreach ($language['translations'] as $requestId => $translation) { |
125 | $translations[$requestId] = $translation; |
126 | } |
127 | } |
128 | if (empty($currentLang) || $currentLang === 'de') { |
129 | $translations['printText'] = $languageConfig['defaultPrintText'] ?? ''; |
130 | } |
131 | } |
132 | return $translations; |
133 | } |
134 | |
135 | private function getAvailableLanguages($languageConfig) |
136 | { |
137 | return array_column($languageConfig['languages'] ?? [], 'language'); |
138 | } |
139 | |
140 | private function getDepartment($scope) |
141 | { |
142 | return \App::$http->readGetResult('/scope/' . $scope->id . '/department/')->getEntity(); |
143 | } |
144 | |
145 | private function shouldRedirectToScope($ticketprinter) |
146 | { |
147 | return count($ticketprinter->buttons) === 1 && $ticketprinter->buttons[0]['type'] === 'scope'; |
148 | } |
149 | |
150 | private function hasDisabledButton($ticketprinter) |
151 | { |
152 | foreach ($ticketprinter->buttons as $button) { |
153 | if (!isset($button['enabled']) || $button['enabled'] != 1) { |
154 | return true; |
155 | } |
156 | } |
157 | return false; |
158 | } |
159 | |
160 | /** |
161 | * @param Validator $validator |
162 | * @param Ticketprinter $ticketprinter |
163 | * @param Unvalidated|Valid $defaultTemplate |
164 | * @return array |
165 | */ |
166 | protected function getQueryString($validator, $ticketprinter, $defaultTemplate) |
167 | { |
168 | $query = ($defaultTemplate->getValue() === 'default') ? [] : ['template' => $defaultTemplate->getValue()]; |
169 | if (isset($ticketprinter['home'])) { |
170 | $homeUrl = $validator::value($ticketprinter['home'])->isUrl()->getValue(); |
171 | if ($homeUrl) { |
172 | $query['ticketprinter[home]'] = $homeUrl; |
173 | } |
174 | } |
175 | return $query; |
176 | } |
177 | } |