Passed
Push — master ( d3bb65...a546ca )
by Tobias
02:11
created

ResponseListener::onKernelResponse()   F

Complexity

Conditions 19
Paths 169

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 54
rs 3.9416
c 0
b 0
f 0
cc 19
nc 169
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Ekino New Relic bundle.
7
 *
8
 * (c) Ekino - Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\NewRelicBundle\Listener;
15
16
use Ekino\NewRelicBundle\NewRelic\Config;
17
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
18
use Ekino\NewRelicBundle\Twig\NewRelicExtension;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpFoundation\StreamedResponse;
21
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
24
class ResponseListener implements EventSubscriberInterface
25
{
26
    private $newRelic;
27
    private $interactor;
28
    private $instrument;
29
    private $symfonyCache;
30
    private $newRelicTwigExtension;
31
32
    public function __construct(
33
        Config $newRelic,
34
        NewRelicInteractorInterface $interactor,
35
        bool $instrument = false,
36
        bool $symfonyCache = false,
37
        NewRelicExtension $newRelicTwigExtension = null
38
    ) {
39
        $this->newRelic = $newRelic;
40
        $this->interactor = $interactor;
41
        $this->instrument = $instrument;
42
        $this->symfonyCache = $symfonyCache;
43
        $this->newRelicTwigExtension = $newRelicTwigExtension;
44
    }
45
46
    public static function getSubscribedEvents(): array
47
    {
48
        return [
49
            KernelEvents::RESPONSE => [
50
                ['onKernelResponse', -255],
51
            ],
52
        ];
53
    }
54
55
    public function onKernelResponse(FilterResponseEvent $event): void
56
    {
57
        if (!$event->isMasterRequest()) {
58
            return;
59
        }
60
61
        if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) {
62
            foreach ($this->newRelic->getCustomMetrics() as $name => $value) {
63
                $this->interactor->addCustomMetric((string) $name, (float) $value);
64
            }
65
66
            foreach ($this->newRelic->getCustomParameters() as $name => $value) {
67
                $this->interactor->addCustomParameter((string) $name, $value);
68
            }
69
        }
70
71
        foreach ($this->newRelic->getCustomEvents() as $name => $events) {
72
            foreach ($events as $attributes) {
73
                $this->interactor->addCustomEvent((string) $name, $attributes);
74
            }
75
        }
76
77
        if ($this->instrument) {
78
            if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) {
79
                $this->interactor->disableAutoRUM();
80
            }
81
82
            // Some requests might not want to get instrumented
83
            if ($event->getRequest()->attributes->get('_instrument', true)) {
84
                $response = $event->getResponse();
85
                if ($response instanceof StreamedResponse) {
86
                    return;
87
                }
88
89
                // We can only instrument HTML responses
90
                if ('text/html' === \substr($response->headers->get('Content-Type', ''), 0, 9)) {
91
                    $responseContent = $response->getContent();
92
                    $response->setContent(''); // free the memory
93
94
                    if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isHeaderCalled()) {
95
                        $responseContent = \preg_replace('|<head>|i', '$0'.$this->interactor->getBrowserTimingHeader(), $responseContent);
96
                    }
97
98
                    if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isFooterCalled()) {
99
                        $responseContent = \preg_replace('|</body>|i', $this->interactor->getBrowserTimingFooter().'$0', $responseContent);
100
                    }
101
102
                    $response->setContent($responseContent);
103
                }
104
            }
105
        }
106
107
        if ($this->symfonyCache) {
108
            $this->interactor->endTransaction();
109
        }
110
    }
111
}
112