Passed
Push — main ( ef112d...d39948 )
by Pieter
03:42
created

Server::getVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace Apie\OpenapiSchema\Spec;
5
6
use Apie\CommonValueObjects\Url;
7
use Apie\OpenapiSchema\Concerns\CompositeValueObjectWithExtension;
8
use Apie\OpenapiSchema\Exceptions\MissingPlaceholderVariables;
9
use Apie\OpenapiSchema\Map\ServerVariableObjectList;
10
use Apie\OpenapiSchema\ValueObjects\UrlsWithPlaceholders;
11
use Apie\ValueObjects\ValueObjectCompareInterface;
12
use Apie\ValueObjects\ValueObjectInterface;
13
14
/**
15
 * @see https://swagger.io/specification/#server-object
16
 */
17
class Server implements ValueObjectInterface
18
{
19
    use CompositeValueObjectWithExtension;
0 ignored issues
show
Bug introduced by
The trait Apie\OpenapiSchema\Conce...alueObjectWithExtension requires the property $name which is not provided by Apie\OpenapiSchema\Spec\Server.
Loading history...
20
21
    /**
22
     * @var UrlsWithPlaceholders
23
     */
24
    private $url;
25
26
    /**
27
     * @var string|null
28
     */
29
    private $description;
30
31
    /**
32
     * @var ServerVariableObjectList|null
33
     */
34
    private $variables;
35
36
    public function __construct(UrlsWithPlaceholders $url)
37
    {
38
        $this->url = $url;
39
        $this->validateProperties();
40
    }
41
42
    public function getUrl(): UrlsWithPlaceholders
43
    {
44
        return $this->url;
45
    }
46
47
    /**
48
     * @return string|null
49
     */
50
    public function getDescription(): ?string
51
    {
52
        return $this->description;
53
    }
54
55
    /**
56
     * @return ServerVariableObjectList|null
57
     */
58
    public function getVariables(): ?ServerVariableObjectList
59
    {
60
        return $this->variables;
61
    }
62
63
    protected function validateProperties(): void
64
    {
65
        $placeholders = $this->url->getPlaceholders();
66
        if (!$this->variables && $placeholders) {
67
            throw new MissingPlaceholderVariables($placeholders);
68
        }
69
        $missing = [];
70
        foreach ($placeholders as $placeholder) {
71
            if (!isset($this->variables[$placeholder])) {
72
                $missing[] = $placeholder;
73
            }
74
        }
75
        if (!empty($missing)) {
76
            throw new MissingPlaceholderVariables($missing);
77
        }
78
    }
79
}