Test Failed
Pull Request — master (#33)
by Joao
09:10
created

OpenApiRequestBody   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

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