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

ValidateSchemaListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validateSchema() 0 6 2
A needValidate() 0 17 4
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