Passed
Push — master ( 430ca3...c3ebe1 )
by Daniel
11:57 queued 27s
created

MethodValidator::validateInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usox\JsonSchemaApi\Dispatch;
6
7
use Opis\JsonSchema\Errors\ErrorFormatter;
8
use Opis\JsonSchema\Helper;
9
use Opis\JsonSchema\Validator;
10
use stdClass;
11
use Teapot\StatusCode;
12
use Usox\JsonSchemaApi\Exception\RequestMalformedException;
13
use Usox\JsonSchemaApi\Exception\ResponseMalformedException;
14
15
/**
16
 * Validates input and output against the json schema to ensure valid requests/responses
17
 */
18
final class MethodValidator implements MethodValidatorInterface
19
{
20
    public function __construct(
21
        private Validator $schemaValidator,
22
        private ErrorFormatter $errorFormatter
23
    ) {
24
    }
25
26
    /**
27
     * @throws RequestMalformedException
28
     */
29 2
    public function validateInput(
30
        stdClass $methodSchemaContent,
31
        stdClass $input
32
    ): void {
33
        // Validate the input parameter against the parameter definition in method schema
34 2
        $validationResult = $this->schemaValidator->validate(
35 2
            $input->parameter,
36 2
            $methodSchemaContent->properties->parameter
37
        );
38
39
        // Throw exception if the input does not validate against the basic request schema
40 2
        if ($validationResult->isValid() === false) {
41 1
            throw new RequestMalformedException(
42
                'Bad Request',
43
                StatusCode::BAD_REQUEST
44
            );
45
        }
46
    }
47
48
    /**
49
     * @param array<mixed> $output
50
     *
51
     * @throws ResponseMalformedException
52
     */
53 2
    public function validateOutput(
54
        stdClass $methodSchemaContent,
55
        array $output
56
    ): void {
57 2
        if (property_exists($methodSchemaContent->properties, 'response') === true) {
58 2
            $data = new stdClass();
59 2
            $data->data = Helper::toJSON($output);
60
61
            // Wrap the response schema
62 2
            $response = (object) [
63
                'type' => 'object',
64
                'properties' => (object) [
65 2
                    'data' => $methodSchemaContent->properties->response,
66
                ],
67
                'required' => ['data']
68
            ];
69
70
            // Validate the response against the response definition in method schema
71 2
            $validationResult = $this->schemaValidator->validate(
72
                $data,
73
                $response
74
            );
75
76 2
            $error = $validationResult->error();
77
            // Throw exception if the input does not validate against the basic request schema
78 2
            if ($error !== null) {
79 1
                throw new ResponseMalformedException(
80
                    'Internal Server Error',
81
                    StatusCode::INTERNAL_SERVER_ERROR,
82
                    null,
83 1
                    $this->errorFormatter->format($error)
84
                );
85
            }
86
        }
87
    }
88
}
89