Validator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 47
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate() 0 10 4
A getSchemaManager() 0 4 1
A getValidator() 0 4 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