Completed
Pull Request — master (#29)
by Joao
07:15
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
 * User: jg
4
 * Date: 22/05/17
5
 * Time: 10:52
6
 */
7
8
namespace ByJG\Swagger;
9
10
use ByJG\Swagger\Exception\InvalidDefinitionException;
11
use ByJG\Swagger\Exception\RequiredArgumentNotFound;
12
13
class SwaggerRequestBody extends SwaggerBody
14
{
15
    /**
16
     * @param $body
17
     * @return bool
18
     * @throws Exception\DefinitionNotFoundException
19
     * @throws Exception\GenericSwaggerException
20
     * @throws Exception\InvalidRequestException
21
     * @throws Exception\NotMatchedException
22
     * @throws InvalidDefinitionException
23
     * @throws RequiredArgumentNotFound
24
     */
25
    public function match($body)
26
    {
27
        if ($this->swaggerSchema->getSpecificationVersion() === '3') {
28
            if (isset($this->structure['content']) || isset($this->structure['$ref'])) {
29
                if (isset($this->structure['required']) && $this->structure['required'] === true && empty($body)) {
30
                    throw new RequiredArgumentNotFound('The body is required but it is empty');
31
                }
32
33
                if (isset($this->structure['$ref'])) {
34
                    return $this->matchSchema($this->name, $this->structure, $body);
35
                }
36
37
                return $this->matchSchema($this->name, $this->structure['content'][key($this->structure['content'])]['schema'], $body);
38
            }
39
40
            if (!empty($body)) {
41
                throw new InvalidDefinitionException('Body is passed but there is no request body definition');
42
            }
43
        }
44
45
        foreach ($this->structure as $parameter) {
46
            if ($parameter['in'] == "body") {
47
                if (isset($parameter['required']) && $parameter['required'] === true && empty($body)) {
48
                    throw new RequiredArgumentNotFound('The body is required but it is empty');
49
                }
50
                return $this->matchSchema($this->name, $parameter['schema'], $body);
51
            }
52
        }
53
54
        if (!empty($body)) {
55
            throw new InvalidDefinitionException('Body is passed but there is no request body definition');
56
        }
57
58
59
60
        return false;
61
    }
62
}
63