Completed
Push — master ( 566e39...89d1b0 )
by Jérémy
19s queued 12s
created

RequestListener::setApplicationName()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.6111
c 1
b 0
f 0
cc 5
nc 6
nop 1
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\TransactionNamingStrategy\TransactionNamingStrategyInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...\Event\GetResponseEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Symfony\Component\HttpKernel\Event\ResponseEvent;
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(KernelRequestEvent $event): void
0 ignored issues
show
Bug introduced by
The type Ekino\NewRelicBundle\Listener\KernelRequestEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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(KernelRequestEvent $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(KernelRequestEvent $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(KernelRequestEvent $event): bool
114
    {
115
        return HttpKernelInterface::MASTER_REQUEST === $event->getRequestType();
116
    }
117
}
118
119
if (\class_exists(ResponseEvent::class)) {
120
    \class_alias(ResponseEvent::class, KernelRequestEvent::class);
121
} else {
122
    \class_alias(GetResponseEvent::class, KernelRequestEvent::class);
123
}
124