RequestListener   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 79
ccs 37
cts 37
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onCoreResponse() 0 17 4
A __construct() 0 5 1
D onCoreRequest() 0 30 10
1
<?php
2
3
namespace Jns\Bundle\XhprofBundle\EventListener;
4
5
use Jns\Bundle\XhprofBundle\DataCollector\XhprofCollector;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\HttpKernel\HttpKernelInterface;
8
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
9
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10
11
/**
12
 * A web request listener to profile requests.
13
 *
14
 * The methods must be connected to the kernel.request and kernel.response
15
 * events.
16
 *
17
 * @author Jonas Wouters <[email protected]>
18
 */
19
class RequestListener
20
{
21
    protected $collector;
22
    private $container;
23
24 12
    public function __construct(XhprofCollector $collector, ContainerInterface $container)
25
    {
26 12
        $this->collector = $collector;
27 12
        $this->container = $container;
28 12
    }
29
30
    /**
31
     * Start the profiler if
32
     * - this is not a sub-request but the master request
33
     * - we are not on the _wdt or _profiler url
34
     * - if the query argument name is configured, only if it is present in the request
35
     * - if the url does not match one of the exclude patterns
36
     *
37
     * @param GetResponseEvent $event
38
     */
39 8
    public function onCoreRequest(GetResponseEvent $event)
40
    {
41 8
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
42 1
            return;
43
        }
44
45 7
        $request = $event->getRequest();
46 7
        $requestQueryArgument = $this->container->getParameter('jns_xhprof.request_query_argument');
47 7
        if ($requestQueryArgument && is_null($request->query->get($requestQueryArgument))) {
48 1
            return;
49 6
        } elseif ($requestQueryArgument) {
50 1
            $request->query->remove($requestQueryArgument);
51 1
        }
52
53 6
        $uri = $request->getRequestUri();
54
55 6
        if (false !== strpos($uri, "_wdt") || false !== strpos($uri, "_profiler")) {
56 4
            return;
57
        }
58
59 2
        if ($excludePatterns = $this->container->getParameter('jns_xhprof.exclude_patterns')) {
60 2
            foreach ($excludePatterns as $exclude) {
61 2
                if (preg_match('@' . $exclude . '@', $uri)) {
62 1
                    return;
63
                }
64 1
            }
65 1
        }
66
67 1
        $this->collector->startProfiling($request);
68 1
    }
69
70
    /**
71
     * Trigger ending the profiling if we end the master request. If the debug
72
     * toolbar is active, this happens after XhprofCollector::collect, and thus
73
     * the collector will return false.
74
     *
75
     * If the collector returns something, we put that into the special header
76
     * to let the user identify the profiler run.
77
     *
78
     * @param FilterResponseEvent $event
79
     */
80 4
    public function onCoreResponse(FilterResponseEvent $event)
81
    {
82 4
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
83 1
            return;
84
        }
85 3
        $request = $event->getRequest();
86 3
        $link = $this->collector->stopProfiling($request->getHost(), $request->getUri());
87
88 3
        if (false === $link) {
89 1
            return;
90
        }
91
92 2
        $headerName = $this->container->getParameter('jns_xhprof.response_header');
93 2
        if ($headerName) {
94 1
            $event->getResponse()->headers->set($headerName, $this->collector->getXhprofUrl());
95 1
        }
96 2
    }
97
}
98