Passed
Pull Request — master (#27)
by Anatoly
04:06
created

AccessControlAllowOriginHeader::getFieldValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Header;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Message\UriInterface;
18
use Sunrise\Http\Message\Exception\InvalidHeaderValueException;
19
use Sunrise\Http\Message\Exception\InvalidUriException;
20
use Sunrise\Http\Message\Header;
21
use Sunrise\Http\Message\Uri;
22
23
/**
24
 * Import functions
25
 */
26
use function sprintf;
27
28
/**
29
 * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
30
 */
31
class AccessControlAllowOriginHeader extends Header
32
{
33
34
    /**
35
     * @var UriInterface|null
36
     */
37
    private ?UriInterface $uri = null;
38
39
    /**
40
     * Constructor of the class
41
     *
42
     * @param mixed $uri
43
     *
44
     * @throws InvalidUriException
45
     *         If the URI isn't valid.
46
     *
47
     * @throws InvalidHeaderValueException
48
     *         If the URI isn't valid.
49
     */
50 10
    public function __construct($uri = null)
51
    {
52 10
        if (isset($uri)) {
53 9
            $uri = Uri::create($uri);
54 9
            $this->validateUri($uri);
55 7
            $this->uri = $uri;
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 5
    public function getFieldName(): string
63
    {
64 5
        return 'Access-Control-Allow-Origin';
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 6
    public function getFieldValue(): string
71
    {
72 6
        if (!isset($this->uri)) {
73 1
            return '*';
74
        }
75
76 5
        $origin = $this->uri->getScheme() . ':';
0 ignored issues
show
Bug introduced by
The method getScheme() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $origin = $this->uri->/** @scrutinizer ignore-call */ getScheme() . ':';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77 5
        $origin .= '//' . $this->uri->getHost();
78
79 5
        $port = $this->uri->getPort();
80 5
        if (isset($port)) {
81 2
            $origin .= ':' . $port;
82
        }
83
84 5
        return $origin;
85
    }
86
87
    /**
88
     * Validates the given URI
89
     *
90
     * @param UriInterface $uri
91
     *
92
     * @return void
93
     *
94
     * @throws InvalidHeaderValueException
95
     *         If the URI isn't valid.
96
     */
97 9
    private function validateUri(UriInterface $uri): void
98
    {
99 9
        if ($uri->getScheme() === '' || $uri->getHost() === '') {
100 2
            throw new InvalidHeaderValueException(sprintf(
101 2
                'The URI "%2$s" for the header "%1$s" is not valid',
102 2
                $this->getFieldName(),
103 2
                $uri->__toString()
104 2
            ));
105
        }
106
    }
107
}
108