Validator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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