Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
26 / 28 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| HomeUrl | |
92.86% |
26 / 28 |
|
50.00% |
1 / 2 |
15.08 | |
0.00% |
0 / 1 |
| create | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| sanitizeUrl | |
88.89% |
16 / 18 |
|
0.00% |
0 / 1 |
11.17 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @package Zmsticketprinter |
| 6 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace BO\Zmsticketprinter\Helper; |
| 11 | |
| 12 | use BO\Mellon\Validator; |
| 13 | |
| 14 | class HomeUrl |
| 15 | { |
| 16 | public static function create(\Psr\Http\Message\RequestInterface $request) |
| 17 | { |
| 18 | $homeUrl = null; |
| 19 | $validator = $request->getAttribute('validator'); |
| 20 | $ticketprinter = $validator->getParameter('ticketprinter')->isArray()->getValue(); |
| 21 | if ($ticketprinter && array_key_exists('home', $ticketprinter)) { |
| 22 | $homeUrl = Validator::value($ticketprinter['home'])->isUrl()->getValue(); |
| 23 | } elseif (!$homeUrl) { |
| 24 | $homeUrl = $request->getRequestTarget(); |
| 25 | } |
| 26 | $homeUrl = static::sanitizeUrl($homeUrl); |
| 27 | \BO\Zmsclient\Ticketprinter::setHomeUrl($homeUrl, $request); |
| 28 | return $homeUrl; |
| 29 | } |
| 30 | |
| 31 | public static function sanitizeUrl(mixed $url): string |
| 32 | { |
| 33 | if (! is_string($url) || $url === '') { |
| 34 | return is_string($url) ? $url : ''; |
| 35 | } |
| 36 | |
| 37 | $parts = parse_url($url); |
| 38 | if ($parts === false) { |
| 39 | return $url; |
| 40 | } |
| 41 | |
| 42 | $kept = []; |
| 43 | foreach (explode('&', $parts['query'] ?? '') as $pair) { |
| 44 | if ($pair !== '' && str_contains($pair, '=')) { |
| 45 | $kept[] = $pair; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | $sanitized = ''; |
| 50 | if (isset($parts['scheme'], $parts['host'])) { |
| 51 | $sanitized = $parts['scheme'] . '://' . $parts['host']; |
| 52 | if (isset($parts['port'])) { |
| 53 | $sanitized .= ':' . $parts['port']; |
| 54 | } |
| 55 | } |
| 56 | $sanitized .= $parts['path'] ?? ''; |
| 57 | if ($kept !== []) { |
| 58 | $sanitized .= '?' . implode('&', $kept); |
| 59 | } |
| 60 | |
| 61 | return $sanitized; |
| 62 | } |
| 63 | } |