Completed
Pull Request — master (#6)
by Veaceslav
11:19
created

PathBuilder::getParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aweapi\Openapi\Builders;
6
7
use Aweapi\Openapi\Objects;
8
use InvalidArgumentException;
9
10
final class PathBuilder implements Objects\PathFactory
11
{
12
    use Properties\OptionalExtensions;
13
14
    private $description;
15
16
    private $operations = [
17
        Objects\Path::METHOD_GET => null,
18
        Objects\Path::METHOD_PUT => null,
19
        Objects\Path::METHOD_POST => null,
20
        Objects\Path::METHOD_DELETE => null,
21
        Objects\Path::METHOD_PATCH => null,
22
        Objects\Path::METHOD_OPTIONS => null,
23
        Objects\Path::METHOD_HEAD => null,
24
        Objects\Path::METHOD_TRACE => null,
25
    ];
26
27
    private $parameters;
28
29
    private $reference;
30
31
    private $servers;
32
33
    private $summary;
34
35
    public function createPath(): Objects\Path
36
    {
37
        return new Objects\Path(
38
            $this->getSummary(),
39
            $this->getReference() ? $this->getReference()->createReference() : null,
40
            $this->getDescription(),
41
            $this->getOperation(Objects\Path::METHOD_GET)
42
                ? $this->getOperation(Objects\Path::METHOD_GET)->createOperation()
43
                : null,
44
            $this->getOperation(Objects\Path::METHOD_PUT)
45
                ? $this->getOperation(Objects\Path::METHOD_PUT)->createOperation()
46
                : null,
47
            $this->getOperation(Objects\Path::METHOD_POST)
48
                ? $this->getOperation(Objects\Path::METHOD_POST)->createOperation()
49
                : null,
50
            $this->getOperation(Objects\Path::METHOD_DELETE)
51
                ? $this->getOperation(Objects\Path::METHOD_DELETE)->createOperation()
52
                : null,
53
            $this->getOperation(Objects\Path::METHOD_PATCH)
54
                ? $this->getOperation(Objects\Path::METHOD_PATCH)->createOperation()
55
                : null,
56
            $this->getOperation(Objects\Path::METHOD_OPTIONS)
57
                ? $this->getOperation(Objects\Path::METHOD_OPTIONS)->createOperation()
58
                : null,
59
            $this->getOperation(Objects\Path::METHOD_HEAD)
60
                ? $this->getOperation(Objects\Path::METHOD_HEAD)->createOperation()
61
                : null,
62
            $this->getOperation(Objects\Path::METHOD_TRACE)
63
                ? $this->getOperation(Objects\Path::METHOD_TRACE)->createOperation()
64
                : null,
65
            $this->getServers() ? $this->getServers()->createServerCollection() : null,
66
            $this->getParameters() ? $this->getParameters()->createParameterCollection() : null,
67
            $this->getExtensions()
68
        );
69
    }
70
71
    public function setDescription(string $description): self
72
    {
73
        $this->description = $description;
74
75
        return $this;
76
    }
77
78
    public function setOperation(string $method, Objects\OperationFactory $operation): self
79
    {
80
        $method = mb_strtolower($method);
81
82
        if (!array_key_exists($method, $this->operations)) {
83
            throw new InvalidArgumentException(sprintf('The method "%s" is not supported', $method));
84
        }
85
86
        $this->operations[$method] = $operation;
87
88
        return $this;
89
    }
90
91
    public function setParameters(Objects\ParameterCollectionFactory $parameters): self
92
    {
93
        $this->parameters = $parameters;
94
95
        return $this;
96
    }
97
98
    public function setReference(Objects\ReferenceFactory $reference): self
99
    {
100
        $this->reference = $reference;
101
102
        return $this;
103
    }
104
105
    public function setServers(Objects\ServerCollectionFactory $servers): self
106
    {
107
        $this->servers = $servers;
108
109
        return $this;
110
    }
111
112
    public function setSummary(string $summary): self
113
    {
114
        $this->summary = $summary;
115
116
        return $this;
117
    }
118
119
    private function getDescription(): ?string
120
    {
121
        return $this->description;
122
    }
123
124
    private function getOperation(string $method): ?Objects\OperationFactory
125
    {
126
        return $this->operations[mb_strtolower($method)];
127
    }
128
129
    private function getParameters(): ?Objects\ParameterCollectionFactory
130
    {
131
        return $this->parameters;
132
    }
133
134
    private function getReference(): ?Objects\ReferenceFactory
135
    {
136
        return $this->reference;
137
    }
138
139
    private function getServers(): ?Objects\ServerCollectionFactory
140
    {
141
        return $this->servers;
142
    }
143
144
    private function getSummary(): ?string
145
    {
146
        return $this->summary;
147
    }
148
}
149