Validator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\Validator;
4
5
use FR3D\SwaggerAssertions\PhpUnit\SymfonyAssertsTrait;
6
use FR3D\SwaggerAssertions\SchemaManager;
7
use JsonSchema\Constraints\Factory;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class Validator
11
{
12
    use SymfonyAssertsTrait;
13
14
    /** @var SchemaManager */
15
    private $schemaManager;
16
17
    /** @var bool */
18
    private $strict;
19
20
    /** @var Factory */
21
    private $constraintsFactory;
22
23 4
    public function __construct(Factory $constraintsFactory, SchemaManager $schemaManager, $strict)
24
    {
25 4
        $this->schemaManager = $schemaManager;
26 4
        $this->strict = $strict;
27 4
        $this->constraintsFactory = $constraintsFactory;
28 4
    }
29
30 4
    public function validate(Request $request)
31
    {
32
        try {
33 4
            $this->assertRequestMatch($request, $this->schemaManager);
34 4
        } catch (\RuntimeException $e) {
35 3
            if ($this->strict || $e->getMessage() !== 'Request URI does not match with any swagger path definition') {
36 2
                throw $e;
37
            }
38
        }
39 2
    }
40
41
    /**
42
     * @return SchemaManager
43
     */
44
    public function getSchemaManager()
45
    {
46
        return $this->schemaManager;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 2
    protected function getValidator()
53
    {
54 2
        return new \JsonSchema\Validator(\JsonSchema\Validator::CHECK_MODE_NORMAL, null, $this->constraintsFactory);
55
    }
56
}
57