NewRelicExtension::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Twig;
15
16
use Ekino\NewRelicBundle\NewRelic\Config;
17
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFunction;
20
21
/**
22
 * Twig extension to manually include BrowserTimingHeader and BrowserTimingFooter into twig templates.
23
 */
24
class NewRelicExtension extends AbstractExtension
25
{
26
    private $newRelic;
27
    private $interactor;
28
    private $instrument;
29
    private $headerCalled = false;
30
    private $footerCalled = false;
31
32
    public function __construct(
33
        Config $newRelic,
34
        NewRelicInteractorInterface $interactor,
35
        bool $instrument = false
36
    ) {
37
        $this->newRelic = $newRelic;
38
        $this->interactor = $interactor;
39
        $this->instrument = $instrument;
40
    }
41
42
    public function getFunctions(): array
43
    {
44
        return [
45
            new TwigFunction('ekino_newrelic_browser_timing_header', [$this, 'getNewrelicBrowserTimingHeader'], ['is_safe' => ['html']]),
46
            new TwigFunction('ekino_newrelic_browser_timing_footer', [$this, 'getNewrelicBrowserTimingFooter'], ['is_safe' => ['html']]),
47
        ];
48
    }
49
50
    /**
51
     * @throws \RuntimeException
52
     */
53
    public function getNewrelicBrowserTimingHeader(): string
54
    {
55
        if ($this->isHeaderCalled()) {
56
            throw new \RuntimeException('Function "ekino_newrelic_browser_timing_header" has already been called');
57
        }
58
59
        $this->prepareInteractor();
60
61
        $this->headerCalled = true;
62
63
        return $this->interactor->getBrowserTimingHeader();
64
    }
65
66
    /**
67
     * @throws \RuntimeException
68
     */
69
    public function getNewrelicBrowserTimingFooter(): string
70
    {
71
        if ($this->isFooterCalled()) {
72
            throw new \RuntimeException('Function "ekino_newrelic_browser_timing_footer" has already been called');
73
        }
74
75
        if (false === $this->isHeaderCalled()) {
76
            $this->prepareInteractor();
77
        }
78
79
        $this->footerCalled = true;
80
81
        return $this->interactor->getBrowserTimingFooter();
82
    }
83
84
    public function isHeaderCalled(): bool
85
    {
86
        return $this->headerCalled;
87
    }
88
89
    public function isFooterCalled(): bool
90
    {
91
        return $this->footerCalled;
92
    }
93
94
    public function isUsed(): bool
95
    {
96
        return $this->isHeaderCalled() || $this->isFooterCalled();
97
    }
98
99
    private function prepareInteractor(): void
100
    {
101
        if ($this->instrument) {
102
            $this->interactor->disableAutoRUM();
103
        }
104
105
        foreach ($this->newRelic->getCustomMetrics() as $name => $value) {
106
            $this->interactor->addCustomMetric((string) $name, (float) $value);
107
        }
108
109
        foreach ($this->newRelic->getCustomParameters() as $name => $value) {
110
            $this->interactor->addCustomParameter((string) $name, $value);
111
        }
112
    }
113
}
114