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

XmlCheckSchemaSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 45
loc 45
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getSubscribedEvents() 6 6 1
A validateSchema() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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