OpenApiRequestBody   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 11
c 0
b 0
f 0
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B match() 0 19 8
1
<?php
2
3
namespace ByJG\ApiTools\OpenApi;
4
5
use ByJG\ApiTools\Base\Body;
6
use ByJG\ApiTools\Exception\InvalidDefinitionException;
7
use ByJG\ApiTools\Exception\RequiredArgumentNotFound;
8
9
class OpenApiRequestBody extends Body
10
{
11
    /**
12
     * @inheritDoc
13
     */
14
    public function match(mixed $body): bool
15
    {
16
        if (isset($this->structure['content']) || isset($this->structure['$ref'])) {
17
            if (isset($this->structure['required']) && $this->structure['required'] === true && empty($body)) {
18
                throw new RequiredArgumentNotFound('The body is required but it is empty');
19
            }
20
21
            if (isset($this->structure['$ref'])) {
22
                return $this->matchSchema($this->name, $this->structure, $body) ?? false;
23
            }
24
25
            return $this->matchSchema($this->name, $this->structure['content'][key($this->structure['content'])]['schema'], $body) ?? false;
26
        }
27
28
        if (!empty($body)) {
29
            throw new InvalidDefinitionException('Body is passed but there is no request body definition');
30
        }
31
32
        return false;
33
    }
34
}
35