Passed
Pull Request — master (#50)
by Joao
02:00
created

SwaggerRequestBody::match()   C

Complexity

Conditions 14
Paths 10

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 6.2666
c 0
b 0
f 0
cc 14
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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