Completed
Pull Request — master (#49)
by John
02:41
created

RequestValidator::setOperationObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Request;
10
11
use KleijnWeb\SwaggerBundle\Document\OperationObject;
12
use KleijnWeb\SwaggerBundle\Exception\UnsupportedException;
13
use Symfony\Component\HttpFoundation\Request;
14
use JsonSchema\Validator;
15
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
class RequestValidator
21
{
22
    /**
23
     * @var OperationObject
24
     */
25
    private $operationObject;
26
27
    /**
28
     * @param OperationObject $operationObject
29
     */
30
    public function __construct(OperationObject $operationObject = null)
31
    {
32
        if ($operationObject) {
33
            $this->setOperationObject($operationObject);
34
        }
35
    }
36
37
    /**
38
     * @param OperationObject $operationObject
39
     *
40
     * @return $this
41
     */
42
    public function setOperationObject(OperationObject $operationObject)
43
    {
44
        $this->operationObject = $operationObject;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param Request $request
51
     *
52
     * @throws InvalidParametersException
53
     * @throws UnsupportedException
54
     */
55
    public function validateRequest(Request $request)
56
    {
57
        $validator = new Validator();
58
59
        $validator->check(
60
            $this->assembleParameterDataForValidation($request),
61
            $this->operationObject->getRequestSchema()
62
        );
63
64
        if (!$validator->isValid()) {
65
            throw new InvalidParametersException(
66
                "Parameters incompatible with operation schema: "
67
                . implode(', ', $validator->getErrors()[0]),
68
                $validator->getErrors()
69
            );
70
        }
71
    }
72
73
    /**
74
     * @param Request $request
75
     *
76
     * @return \stdClass
77
     * @throws UnsupportedException
78
     */
79
    private function assembleParameterDataForValidation(Request $request)
80
    {
81
        /**
82
         * TODO Hack
83
         * @see https://github.com/kleijnweb/swagger-bundle/issues/24
84
         */
85
        $content = null;
86
        if ($request->getContent()) {
87
            $content = json_decode($request->getContent());
88
            //TODO UT this
89
            $content = (is_array($content) && isset($content[0])) ? $content : (object)$content;
90
        }
91
92
        $parameters = new \stdClass;
93
94
        foreach ($this->operationObject->getDefinition()->parameters as $paramDefinition) {
95
            $paramName = $paramDefinition->name;
96
97
            if (!$request->attributes->has($paramName)) {
98
                continue;
99
            }
100
            if ($paramDefinition->in === 'body' && $content !== null) {
101
                $parameters->$paramName = $content;
102
                continue;
103
            }
104
            $parameters->$paramName = $request->attributes->get($paramName);
105
106
            /**
107
             * If value already coerced into \DateTime object, use any non-empty value for validation
108
             */
109
            if ($parameters->$paramName instanceof \DateTime) {
110
                if ($paramDefinition->format === 'date') {
111
                    $parameters->$paramName = '1970-01-01';
112
                }
113
                if ($paramDefinition->format === 'date-time') {
114
                    $parameters->$paramName = '1970-01-01T00:00:00Z';
115
                }
116
            }
117
        }
118
119
        return $parameters;
120
    }
121
}
122