Passed
Push — master ( cce2e1...993520 )
by Dawid
02:22
created

XmlCheckSchemaSubscriber::validateSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
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\Event\ResponseSchemaCheck\Events;
9
use Spiechu\SymfonyCommonsBundle\Service\XmlSchemaValidatorFactory;
10
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
13 View Code Duplication
class XmlCheckSchemaSubscriber implements EventSubscriberInterface
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * @var XmlSchemaValidatorFactory
17
     */
18
    protected $xmlSchemaValidatorFactory;
19
20
    /**
21
     * @param XmlSchemaValidatorFactory $xmlSchemaValidatorFactory
22
     */
23 11
    public function __construct(XmlSchemaValidatorFactory $xmlSchemaValidatorFactory)
24
    {
25 11
        $this->xmlSchemaValidatorFactory = $xmlSchemaValidatorFactory;
26 11
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 5
    public static function getSubscribedEvents(): array
32
    {
33
        return [
34 5
            Events::getCheckSchemaEventNameFor('xml') => 'validateSchema',
35
        ];
36
    }
37
38
    /**
39
     * @param CheckRequest $checkRequest
40
     *
41
     * @throws \RuntimeException
42
     * @throws \InvalidArgumentException
43
     * @throws FileLocatorFileNotFoundException
44
     */
45 1
    public function validateSchema(CheckRequest $checkRequest): void
46
    {
47 1
        $schemaLocation = $checkRequest->getResponseSchemaLocation();
48
49 1
        if (!$this->xmlSchemaValidatorFactory->hasSchema($schemaLocation)) {
50 1
            $this->xmlSchemaValidatorFactory->registerSchema($schemaLocation, $schemaLocation);
51
        }
52
53 1
        $schemaValidator = $this->xmlSchemaValidatorFactory->getValidator($schemaLocation);
54
55 1
        $checkRequest->setValidationResult($schemaValidator->validate($checkRequest->getContent()));
56 1
    }
57
}
58