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

RequestListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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\TransactionNamingStrategy\TransactionNamingStrategyInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
22
use Symfony\Component\HttpKernel\HttpKernelInterface;
23
use Symfony\Component\HttpKernel\KernelEvents;
24
25
class RequestListener implements EventSubscriberInterface
26
{
27
    private $ignoredRoutes;
28
    private $ignoredPaths;
29
    private $config;
30
    private $interactor;
31
    private $transactionNamingStrategy;
32
    private $symfonyCache;
33
34
    public function __construct(
35
        Config $config,
36
        NewRelicInteractorInterface $interactor,
37
        array $ignoreRoutes,
38
        array $ignoredPaths,
39
        TransactionNamingStrategyInterface $transactionNamingStrategy,
40
        bool $symfonyCache = false
41
    ) {
42
        $this->config = $config;
43
        $this->interactor = $interactor;
44
        $this->ignoredRoutes = $ignoreRoutes;
45
        $this->ignoredPaths = $ignoredPaths;
46
        $this->transactionNamingStrategy = $transactionNamingStrategy;
47
        $this->symfonyCache = $symfonyCache;
48
    }
49
50
    public static function getSubscribedEvents(): array
51
    {
52
        return [
53
            KernelEvents::REQUEST => [
54
                 ['setApplicationName', 255],
55
                 ['setIgnoreTransaction', 31],
56
                 ['setTransactionName', -10],
57
            ],
58
        ];
59
    }
60
61
    public function setApplicationName(GetResponseEvent $event): void
62
    {
63
        if (!$this->isEventValid($event)) {
64
            return;
65
        }
66
67
        $appName = $this->config->getName();
68
69
        if (!$appName) {
70
            return;
71
        }
72
73
        if ($this->symfonyCache) {
74
            $this->interactor->startTransaction($appName);
75
        }
76
77
        // Set application name if different from ini configuration
78
        if ($appName !== ini_get('newrelic.appname')) {
79
            $this->interactor->setApplicationName($appName, $this->config->getLicenseKey(), $this->config->getXmit());
80
        }
81
    }
82
83
    public function setTransactionName(GetResponseEvent $event): void
84
    {
85
        if (!$this->isEventValid($event)) {
86
            return;
87
        }
88
89
        $transactionName = $this->transactionNamingStrategy->getTransactionName($event->getRequest());
90
91
        $this->interactor->setTransactionName($transactionName);
92
    }
93
94
    public function setIgnoreTransaction(GetResponseEvent $event): void
95
    {
96
        if (!$this->isEventValid($event)) {
97
            return;
98
        }
99
100
        $request = $event->getRequest();
101
        if (in_array($request->get('_route'), $this->ignoredRoutes, true)) {
102
            $this->interactor->ignoreTransaction();
103
        }
104
105
        if (in_array($request->getPathInfo(), $this->ignoredPaths, true)) {
106
            $this->interactor->ignoreTransaction();
107
        }
108
    }
109
110
    /**
111
     * Make sure we should consider this event. Example: make sure it is a master request.
112
     */
113
    private function isEventValid(GetResponseEvent $event): bool
114
    {
115
        return HttpKernelInterface::MASTER_REQUEST === $event->getRequestType();
116
    }
117
}
118