Passed
Push — master ( 9b5861...5bdc47 )
by Joao
06:11 queued 16s
created

SwaggerRequestBody   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 50
rs 10
c 0
b 0
f 0

1 Method

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