Completed
Pull Request — master (#191)
by Jérémy
02:29
created

ResponseListener::onKernelResponse()   C

Complexity

Conditions 20
Paths 157

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 5.1763
c 0
b 0
f 0
cc 20
eloc 25
nc 157
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\HttpKernel\Event\FilterResponseEvent;
21
use Symfony\Component\HttpKernel\KernelEvents;
22
23
class ResponseListener implements EventSubscriberInterface
24
{
25
    private $newRelic;
26
    private $interactor;
27
    private $instrument;
28
    private $symfonyCache;
29
    private $newRelicTwigExtension;
30
31
    public function __construct(
32
        Config $newRelic,
33
        NewRelicInteractorInterface $interactor,
34
        bool $instrument = false,
35
        bool $symfonyCache = false,
36
        NewRelicExtension $newRelicTwigExtension = null
37
    ) {
38
        $this->newRelic = $newRelic;
39
        $this->interactor = $interactor;
40
        $this->instrument = $instrument;
41
        $this->symfonyCache = $symfonyCache;
42
        $this->newRelicTwigExtension = $newRelicTwigExtension;
43
    }
44
45
    public static function getSubscribedEvents(): array
46
    {
47
        return [
48
            KernelEvents::RESPONSE => [
49
                ['onKernelResponse', -255],
50
            ],
51
        ];
52
    }
53
54
    public function onKernelResponse(FilterResponseEvent $event): void
55
    {
56
        if (!$event->isMasterRequest()) {
57
            return;
58
        }
59
60
        if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) {
61
            foreach ($this->newRelic->getCustomMetrics() as $name => $value) {
62
                $this->interactor->addCustomMetric((string) $name, (float) $value);
63
            }
64
65
            foreach ($this->newRelic->getCustomParameters() as $name => $value) {
66
                $this->interactor->addCustomParameter((string) $name, $value);
67
            }
68
        }
69
70
        foreach ($this->newRelic->getCustomEvents() as $name => $events) {
71
            foreach ($events as $attributes) {
72
                $this->interactor->addCustomEvent((string) $name, $attributes);
73
            }
74
        }
75
76
        if ($this->instrument) {
77
            if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) {
78
                $this->interactor->disableAutoRUM();
79
            }
80
81
            // Some requests might not want to get instrumented
82
            if ($event->getRequest()->attributes->get('_instrument', true)) {
83
                $response = $event->getResponse();
84
85
                // We can only instrument HTML responses
86
                if ('text/html' === \substr($response->headers->get('Content-Type'), 0, 9)) {
87
                    $responseContent = $response->getContent();
88
                    $response->setContent(''); // free the memory
89
90
                    if ((null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isHeaderCalled()) && false !== ($pos = stripos($responseContent, '<head>'))) {
91
                        $responseContent = substr($responseContent, 0, $pos + 6).$this->interactor->getBrowserTimingHeader().substr($responseContent, $pos + 6);
92
                    }
93
94
                    if ((null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isFooterCalled()) && false !== ($pos = stripos($responseContent, '</body>'))) {
95
                        $responseContent = substr($responseContent, 0, $pos).$this->interactor->getBrowserTimingFooter().substr($responseContent, $pos);
96
                    }
97
98
                    $response->setContent($responseContent);
99
                }
100
            }
101
        }
102
103
        if ($this->symfonyCache) {
104
            $this->interactor->endTransaction();
105
        }
106
    }
107
}
108