ValidateSchemaListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace steevanb\DevBundle\EventListener;
6
7
use steevanb\DevBundle\Service\ValidateSchemaService;
8
use Symfony\Component\HttpKernel\{
9
    Event\GetResponseEvent,
10
    HttpKernelInterface
11
};
12
13
class ValidateSchemaListener
14
{
15
    /** @var ValidateSchemaService */
16
    protected $validateSchema;
17
18
    /** @var array */
19
    protected $disabledUrls = array();
20
21
    public function __construct(ValidateSchemaService $validateSchema, array $disabledUrls)
22
    {
23
        $this->validateSchema = $validateSchema;
24
        $this->disabledUrls = $disabledUrls;
25
    }
26
27
    public function validateSchema(GetResponseEvent $event): void
28
    {
29
        if ($this->needValidate($event)) {
30
            $this->validateSchema->assertSchemaIsValid();
31
        }
32
    }
33
34
    protected function needValidate(GetResponseEvent $event): bool
35
    {
36
        $return = false;
37
38
        if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
39
            $return = true;
40
            $urlParts = parse_url($event->getRequest()->getUri());
41
            foreach ($this->disabledUrls as $disabledUrl) {
42
                if (substr($urlParts['path'], 0, strlen($disabledUrl)) === $disabledUrl) {
43
                    $return = false;
44
                    continue;
45
                }
46
            }
47
        }
48
49
        return $return;
50
    }
51
}
52