Passed
Push — master ( f9279d...87874e )
by Joao
01:35 queued 11s
created

SwaggerRequestBody   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C match() 0 27 14
1
<?php
2
3
namespace ByJG\ApiTools\Swagger;
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 SwaggerRequestBody 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
        $hasFormData = false;
28
        foreach ($this->structure as $parameter) {
29
            if ($parameter['in'] === "body") {
30
                if (isset($parameter['required']) && $parameter['required'] === true && empty($body)) {
31
                    throw new RequiredArgumentNotFound('The body is required but it is empty');
32
                }
33
                return $this->matchSchema($this->name, $parameter['schema'], $body);
34
            }
35
            if ($parameter['in'] === "formData") {
36
                $hasFormData = true;
37
                if (isset($parameter['required']) && $parameter['required'] === true && !isset($body[$parameter['name']])) {
38
                    throw new RequiredArgumentNotFound("The formData parameter '${parameter['name']}' is required but it isn't found. ");
39
                }
40
                if (!$this->matchTypes($parameter['name'], $parameter, (isset($body[$parameter['name']]) ? $body[$parameter['name']] : null))) {
41
                    throw new NotMatchedException("The formData parameter '${parameter['name']}' not match with the specification");
42
                }
43
            }
44
        }
45
46
        if (!empty($body) && !$hasFormData) {
47
            throw new InvalidDefinitionException('Body is passed but there is no request body definition');
48
        }
49
50
        return false;
51
    }
52
}
53