Lines 92.85% 26 / 28
Methods 50.00% 1 / 2
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 create 100.00% 10 / 10 100.00% 1 / 1 4
 sanitizeUrl 88.88% 16 / 18 0.00% 0 / 1 11.17
14class 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}