Passed
Push — master ( da9ebc...eab3b6 )
by Tobias
02:14
created

NewRelicExtension   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 94
rs 10
c 0
b 0
f 0

8 Methods

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