Skip to content

Commit

Permalink
Added RUMControlFactory for HeaderControl and FooterControl
Browse files Browse the repository at this point in the history
  • Loading branch information
foxycode committed Mar 8, 2021
1 parent 2ccd4e8 commit 7d59b61
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 30 deletions.
13 changes: 3 additions & 10 deletions src/DI/NewRelicExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
use Contributte\NewRelic\Config\TransactionTracerConfig;
use Contributte\NewRelic\Environment;
use Contributte\NewRelic\Formatters\DefaultWebTransactionNameFormatter;
use Contributte\NewRelic\RUM\FooterControl;
use Contributte\NewRelic\RUM\HeaderControl;
use Contributte\NewRelic\RUM\RUMControlFactory;
use Contributte\NewRelic\Tracy\Logger;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\ServiceDefinition;
Expand Down Expand Up @@ -217,14 +216,8 @@ private function setupRUM(): void

$rumEnabled = $this->enabled && $config->rum->enabled === true;

$builder->addDefinition($this->prefix('rum.headerControl'))
->setFactory(HeaderControl::class, [
'@' . $this->prefix('agent'),
$rumEnabled,
]);

$builder->addDefinition($this->prefix('rum.footerControl'))
->setFactory(FooterControl::class, [
$builder->addDefinition($this->prefix('rum.controlFactory'))
->setFactory(RUMControlFactory::class, [
'@' . $this->prefix('agent'),
$rumEnabled,
]);
Expand Down
12 changes: 3 additions & 9 deletions src/RUM/FooterControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@ class FooterControl extends Control
/**
* @var bool
*/
private $withScriptTag = true;
private $withScriptTag;

public function __construct(Agent $agent, bool $enabled = true)
public function __construct(Agent $agent, bool $enabled = true, bool $withScriptTag = true)
{
$this->agent = $agent;
$this->enabled = $enabled;
}

public function disableScriptTag(): self
{
$this->withScriptTag = false;

return $this;
$this->withScriptTag = $withScriptTag;
}

public function render(): void
Expand Down
12 changes: 3 additions & 9 deletions src/RUM/HeaderControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@ class HeaderControl extends Control
/**
* @var bool
*/
private $withScriptTag = true;
private $withScriptTag;

public function __construct(Agent $agent, bool $enabled = true)
public function __construct(Agent $agent, bool $enabled = true, bool $withScriptTag = true)
{
$this->agent = $agent;
$this->enabled = $enabled;
}

public function disableScriptTag(): self
{
$this->withScriptTag = false;

return $this;
$this->withScriptTag = $withScriptTag;
}

public function render(): void
Expand Down
38 changes: 38 additions & 0 deletions src/RUM/RUMControlFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Contributte\NewRelic\RUM;

use Contributte\NewRelic\Agent\Agent;

final class RUMControlFactory
{

/**
* @var Agent
*/
private $agent;

/**
* @var bool
*/
private $enabled;

public function __construct(Agent $agent, bool $enabled = true)
{
$this->agent = $agent;
$this->enabled = $enabled;
}

public function createHeader(bool $withScriptTag = true): HeaderControl
{
return new HeaderControl($this->agent, $this->enabled, $withScriptTag);
}

public function createFooter(bool $withScriptTag = true): FooterControl
{
return new FooterControl($this->agent, $this->enabled, $withScriptTag);
}

}
30 changes: 30 additions & 0 deletions src/RUM/RUMControlTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Contributte\NewRelic\RUM;

trait RUMControlTrait
{

/**
* @var RUMControlFactory
*/
protected $rumControlFactory;

public function injectRUMControlFactory(RUMControlFactory $rumControlFactory): void
{
$this->rumControlFactory = $rumControlFactory;
}

protected function createComponentNewRelicHeader(): HeaderControl
{
return $this->rumControlFactory->createHeader();
}

protected function createComponentNewRelicFooter(): FooterControl
{
return $this->rumControlFactory->createFooter();
}

}
6 changes: 4 additions & 2 deletions tests/Cases/DI/NewRelicExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use Contributte\NewRelic\DI\NewRelicConsoleExtension;
use Contributte\NewRelic\DI\NewRelicExtension;
use Contributte\NewRelic\RUM\FooterControl;
use Contributte\NewRelic\RUM\HeaderControl;
use Contributte\NewRelic\RUM\RUMControlFactory;
use Contributte\NewRelic\Tracy\Logger;
use ContributteTests\NewRelic\Libs\TestCase;
use ContributteTests\NewRelic\Mocks\ApplicationExtension;
Expand Down Expand Up @@ -83,8 +84,9 @@ final class NewRelicExtensionTest extends TestCase
Assert::type(Logger::class, $container->getService('newrelic.logger'));
Assert::type(OnRequestCallback::class, $container->getService('newrelic.onRequestCallback'));
Assert::type(OnErrorCallback::class, $container->getService('newrelic.onErrorCallback'));
Assert::type(HeaderControl::class, $container->getService('newrelic.rum.headerControl'));
Assert::type(FooterControl::class, $container->getService('newrelic.rum.footerControl'));
Assert::type(RUMControlFactory::class, $container->getService('newrelic.rum.controlFactory'));
Assert::type(HeaderControl::class, $container->getService('newrelic.rum.controlFactory')->createHeader());
Assert::type(FooterControl::class, $container->getService('newrelic.rum.controlFactory')->createFooter());
}

}
Expand Down
3 changes: 3 additions & 0 deletions tests/phpcs-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<rule ref="./vendor/ninjify/coding-standard/ruleset-gamee.xml">
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLineDocComment.MultiLineDocComment"/>
</rule>
<rule ref="SlevomatCodingStandard.Classes.SuperfluousTraitNaming.SuperfluousSuffix">
<exclude-pattern>/src/RUM/RUMControlTrait.php</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
<exclude-pattern>/src/Tracy/Logger.php</exclude-pattern>
</rule>
Expand Down

0 comments on commit 7d59b61

Please sign in to comment.