Passed
Push — feature/unit-tests ( 3f6f78...8a1375 )
by Daniel
06:02
created

RefererUrlHelper::getAbsoluteUrlUsingOrigin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
nc 1
nop 2
cc 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Url;
15
16
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
17
use Silverback\ApiComponentBundle\Exception\OutOfBoundsException;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
class RefererUrlHelper
24
{
25
    private RequestStack $requestStack;
26
27 18
    public function __construct(RequestStack $requestStack)
28
    {
29 18
        $this->requestStack = $requestStack;
30 18
    }
31
32 14
    public function getAbsoluteUrl($path): string
33
    {
34 14
        if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
35 1
            return $path;
36
        }
37
38 13
        $request = $this->requestStack->getMasterRequest();
39 13
        if (!$request) {
40 2
            throw new InvalidArgumentException('To generate an absolute URL to the referrer, there must be a valid master request');
41
        }
42
43 11
        if (null !== ($origin = $request->headers->get('origin', null))) {
0 ignored issues
show
introduced by
The condition null !== $origin = $requ...rs->get('origin', null) is always true.
Loading history...
44 2
            return $this->getAbsoluteUrlUsingOrigin($origin, $path);
45
        }
46 9
        if (null !== ($referer = $request->headers->get('referer', null))) {
47 8
            return $this->getAbsoluteUrlUsingReferer($referer, $path);
48
        }
49
50 1
        throw new OutOfBoundsException('To generate an absolute URL to the referrer, the request must have a `origin` or `referer` header present');
51
    }
52
53 2
    private function getAbsoluteUrlUsingOrigin(string $origin, string $path): string
54
    {
55 2
        return $this->addPathToUrl(rtrim($origin, '/'), $path);
56
    }
57
58 8
    private function getAbsoluteUrlUsingReferer(string $referer, string $path): string
59
    {
60
        $defaults = [
61 8
            'host' => null,
62
            'scheme' => null,
63
            'port' => null,
64
        ];
65
66
        [
67 8
            'host' => $host,
68 8
            'scheme' => $scheme,
69 8
            'port' => $port
70 8
        ] = array_merge($defaults, parse_url($referer) ?: []);
71
72 8
        if (null === $scheme || null === $host) {
73 4
            throw new OutOfBoundsException('Could not extract either `scheme` of `host` key while parsing the `referer` header');
74
        }
75 4
        $url = $scheme . '://' . $host;
76 4
        if ($port && (('https' === $scheme && 443 !== $port) || ('http' === $scheme && 80 !== $port))) {
77 1
            $url .= ':' . $port;
78
        }
79
80 4
        return $this->addPathToUrl($url, $path);
81
    }
82
83 6
    private function addPathToUrl(string $url, string $path): string
84
    {
85 6
        return $url . '/' . ltrim($path, '/');
86
    }
87
}
88