Completed
Push — master ( ae46cd...5f7f67 )
by Steevan
02:24
created

ValidateSchemaListener::validateSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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