Server   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getVariables() 0 3 1
A getDescription() 0 3 1
A getUrl() 0 3 1
A __construct() 0 4 1
A validateProperties() 0 14 6
1
<?php
2
3
4
namespace Apie\OpenapiSchema\Spec;
5
6
use Apie\OpenapiSchema\Concerns\CompositeValueObjectWithExtension;
7
use Apie\OpenapiSchema\Exceptions\MissingPlaceholderVariables;
8
use Apie\OpenapiSchema\Map\ServerVariableObjectList;
9
use Apie\OpenapiSchema\ValueObjects\UrlsWithPlaceholders;
10
use Apie\ValueObjects\ValueObjectInterface;
11
12
/**
13
 * @see https://swagger.io/specification/#server-object
14
 */
15
class Server implements ValueObjectInterface
16
{
17
    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...
18
19
    /**
20
     * @var UrlsWithPlaceholders
21
     */
22
    private $url;
23
24
    /**
25
     * @var string|null
26
     */
27
    private $description;
28
29
    /**
30
     * @var ServerVariableObjectList|null
31
     */
32
    private $variables;
33
34
    public function __construct(UrlsWithPlaceholders $url)
35
    {
36
        $this->url = $url;
37
        $this->validateProperties();
38
    }
39
40
    public function getUrl(): UrlsWithPlaceholders
41
    {
42
        return $this->url;
43
    }
44
45
    /**
46
     * @return string|null
47
     */
48
    public function getDescription(): ?string
49
    {
50
        return $this->description;
51
    }
52
53
    /**
54
     * @return ServerVariableObjectList|null
55
     */
56
    public function getVariables(): ?ServerVariableObjectList
57
    {
58
        return $this->variables;
59
    }
60
61
    protected function validateProperties(): void
62
    {
63
        $placeholders = $this->url->getPlaceholders();
64
        if (!$this->variables && $placeholders) {
65
            throw new MissingPlaceholderVariables($placeholders);
66
        }
67
        $missing = [];
68
        foreach ($placeholders as $placeholder) {
69
            if (!isset($this->variables[$placeholder])) {
70
                $missing[] = $placeholder;
71
            }
72
        }
73
        if (!empty($missing)) {
74
            throw new MissingPlaceholderVariables($missing);
75
        }
76
    }
77
}
78