Completed
Push — master ( bca24d...1b0d8b )
by John
03:18
created

MessageValidator::validateResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions 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
namespace KleijnWeb\PhpApi\Descriptions;
9
10
use KleijnWeb\PhpApi\Descriptions\Description\Description;
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\DefaultValidator;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator;
13
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\ValidationResult;
14
use KleijnWeb\PhpApi\Descriptions\Request\RequestParameterAssembler;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
18
class MessageValidator
19
{
20
    /**
21
     * @var Description
22
     */
23
    private $description;
24
25
    /**
26
     * @var RequestParameterAssembler
27
     */
28
    private $parameterAssembler;
29
30
    /**
31
     * @var SchemaValidator
32
     */
33
    private $validator;
34
35
    /**
36
     * MessageValidator constructor.
37
     *
38
     * @param Description               $description
39
     * @param RequestParameterAssembler $parameterAssembler
40
     * @param SchemaValidator           $validator
41
     */
42
    public function __construct(
43
        Description $description,
44
        RequestParameterAssembler $parameterAssembler = null,
45
        SchemaValidator $validator = null
46
    ) {
47
        $this->description        = $description;
48
        $this->parameterAssembler = $parameterAssembler ?: new RequestParameterAssembler();
49
        $this->validator          = $validator ?: new DefaultValidator();
50
    }
51
52
    /**
53
     * @param ServerRequestInterface $request
54
     * @param string                 $path
55
     *
56
     * @return ValidationResult
57
     */
58 View Code Duplication
    public function validateRequest(ServerRequestInterface $request, string $path): ValidationResult
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $operation         = $this->description->getPath($path)->getOperation($request->getMethod());
61
        $schema            = $operation->getRequestSchema();
62
        $requestParameters = $this->parameterAssembler->getRequestParameters($request, $operation);
63
64
        return $this->validator->validate($schema, $requestParameters);
65
    }
66
67
    /**
68
     * @param array|object           $body
69
     * @param ServerRequestInterface $request
70
     * @param ResponseInterface      $response
71
     * @param string                 $path
72
     *
73
     * @return ValidationResult
74
     */
75 View Code Duplication
    public function validateResponse(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
        $body,
77
        ServerRequestInterface $request,
78
        ResponseInterface $response,
79
        string $path
80
    ): ValidationResult {
81
    
82
83
84
        $operation = $this->description->getPath($path)->getOperation($request->getMethod());
85
        $schema    = $operation->getResponse($response->getStatusCode())->getSchema();
86
87
        return $this->validator->validate($schema, $body);
88
    }
89
}
90