Completed
Push — master ( 3c91a9...7f2ec8 )
by Jérémy
11s
created

ResponseListener::onKernelResponse()   D

Complexity

Conditions 19
Paths 253

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 25
nc 253
nop 1
dl 0
loc 52
rs 4.3979
c 0
b 0
f 0

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\Bundle\NewRelicBundle\Listener;
15
16
use Ekino\Bundle\NewRelicBundle\NewRelic\Config;
17
use Ekino\Bundle\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
18
use Ekino\Bundle\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
89
                    if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isHeaderCalled()) {
90
                        $responseContent = preg_replace('/<\s*head\s*>/', '$0'.$this->interactor->getBrowserTimingHeader(), $responseContent);
91
                    }
92
93
                    if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isFooterCalled()) {
94
                        $responseContent = preg_replace('/<\s*\/\s*body\s*>/', $this->interactor->getBrowserTimingFooter().'$0', $responseContent);
95
                    }
96
97
                    if ($responseContent) {
98
                        $response->setContent($responseContent);
99
                    }
100
                }
101
            }
102
        }
103
104
        if ($this->symfonyCache) {
105
            $this->interactor->endTransaction();
106
        }
107
    }
108
}
109