Completed
Push — master ( 993520...f97157 )
by Dawid
06:13
created

AbstractCheckSchemaSubscriber::validateSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\EventListener;
6
7
use Spiechu\SymfonyCommonsBundle\Event\ResponseSchemaCheck\CheckRequest;
8
use Spiechu\SymfonyCommonsBundle\Service\SchemaValidator\SchemaValidatorFactoryInterface;
9
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
10
11
abstract class AbstractCheckSchemaSubscriber
12
{
13
    /**
14
     * @param CheckRequest $checkRequest
15
     *
16
     * @throws \RuntimeException
17
     * @throws \InvalidArgumentException
18
     * @throws FileLocatorFileNotFoundException
19
     */
20 2
    public function validateSchema(CheckRequest $checkRequest): void
21
    {
22 2
        $schemaLocation = $checkRequest->getResponseSchemaLocation();
23 2
        $schemaValidatorFactory = $this->getSchemaValidatorFactory();
24
25 2
        if (!$schemaValidatorFactory->hasSchema($schemaLocation)) {
26 2
            $schemaValidatorFactory->registerSchema($schemaLocation, $schemaLocation);
27
        }
28
29 2
        $schemaValidator = $schemaValidatorFactory->getValidator($schemaLocation);
30
31 2
        $checkRequest->setValidationResult($schemaValidator->validate($checkRequest->getContent()));
32 2
    }
33
34
    /**
35
     * @return SchemaValidatorFactoryInterface
36
     */
37
    abstract protected function getSchemaValidatorFactory(): SchemaValidatorFactoryInterface;
38
}
39