OperationResponses::getDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aweapi\Openapi\Objects;
6
7
use Aweapi\Openapi\Extensions;
8
use Aweapi\Openapi\ValueObject;
9
10
final class OperationResponses extends ValueObject
11
{
12
    use Properties\OptionalExtensions;
13
14
    private $default;
15
16
    private $responses = [];
17
18 10
    public function __construct(
19
        iterable $responses,
20
        Response $default = null,
21
        Extensions $extensions = null
22
    ) {
23 10
        $this->default = $default;
24
25 10
        foreach ($responses as $httpStatus => $response) {
26
            // Force cast to string, because of PHP juggling,
27
            // which converts HTTP status numbers into int.
28 6
            $this->setResponse((string) $httpStatus, $response);
29
        }
30
31 10
        $this->extensions = $extensions;
32
    }
33
34 4
    public function getDefault(): Response
35
    {
36 4
        return $this->default;
37
    }
38
39
    /**
40
     * @return Response[]
41
     */
42 10
    public function getResponses(): array
43
    {
44 10
        return $this->responses;
45
    }
46
47 10
    public function hasDefault(): bool
48
    {
49 10
        return isset($this->default);
50
    }
51
52 10
    public function jsonSerialize(): ?array
53
    {
54 10
        return $this->extendedProperties(parent::jsonSerialize());
55
    }
56
57 10
    protected function normalizeOptionalProperties(): array
58
    {
59 10
        $responses = [];
60
61 10
        foreach ($this->getResponses() as $httpStatus => $response) {
62 6
            $responses[(string) $httpStatus] = $response->jsonSerialize();
63
        }
64
65 10
        if ($this->hasDefault()) {
66 4
            $responses['default'] = $this->getDefault()->jsonSerialize();
67
        }
68
69 10
        return $responses;
70
    }
71
72 6
    private function setResponse(string $httpStatus, Response $response): void
73
    {
74 6
        $this->responses[$httpStatus] = $response;
75
    }
76
}
77