Skip to content

Commit ccc355a

Browse files
committed
Add Ability To Separate Frontend / Adminhtml in New Relic
- Minor improvements
1 parent b8e2558 commit ccc355a

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

app/code/Magento/NewRelicReporting/Plugin/StatePlugin.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\NewRelicReporting\Model\NewRelicWrapper;
1414
use Psr\Log\LoggerInterface;
1515

16+
/**
17+
* Handles setting which, when enabled, reports frontend and adminhtml as separate apps to New Relic.
18+
*/
1619
class StatePlugin
1720
{
1821
/**
@@ -33,6 +36,7 @@ class StatePlugin
3336
/**
3437
* @param Config $config
3538
* @param NewRelicWrapper $newRelicWrapper
39+
* @param LoggerInterface $logger
3640
*/
3741
public function __construct(
3842
Config $config,
@@ -49,46 +53,48 @@ public function __construct(
4953
*
5054
* @param State $subject
5155
* @param null $result
52-
* @return void
53-
*
54-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
* @return mixed
5557
*/
56-
public function afterSetAreaCode(State $state, $result)
58+
public function afterSetAreaCode(State $subject, $result)
5759
{
5860
if (!$this->shouldSetAppName()) {
5961
return $result;
6062
}
6163

6264
try {
63-
$this->newRelicWrapper->setAppName($this->appName($state));
65+
$this->newRelicWrapper->setAppName($this->appName($subject));
6466
} catch (LocalizedException $e) {
6567
$this->logger->critical($e);
6668
return $result;
6769
}
70+
71+
return $result;
6872
}
6973

70-
private function appName(State $state)
74+
/**
75+
* @param State $state
76+
* @return string
77+
* @throws LocalizedException
78+
*/
79+
private function appName(State $state): string
7180
{
7281
$code = $state->getAreaCode();
7382
$current = $this->config->getNewRelicAppName();
7483

7584
return $current . ';' . $current . '_' . $code;
7685
}
7786

78-
private function shouldSetAppName()
87+
/**
88+
* Check if app name should be set.
89+
*
90+
* @return bool
91+
*/
92+
private function shouldSetAppName(): bool
7993
{
80-
if (!$this->config->isNewRelicEnabled()) {
81-
return false;
82-
}
83-
84-
if (!$this->config->getNewRelicAppName()) {
85-
return false;
86-
}
87-
88-
if (!$this->config->isSeparateApps()) {
89-
return false;
90-
}
91-
92-
return true;
94+
return (
95+
$this->config->isSeparateApps() &&
96+
$this->config->getNewRelicAppName() &&
97+
$this->config->isNewRelicEnabled()
98+
);
9399
}
94100
}

dev/tests/integration/testsuite/Magento/NewRelicReporting/Plugin/SeparateAppsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
use Magento\TestFramework\ObjectManager;
1313
use Magento\TestFramework\Helper\Bootstrap;
1414

15+
/**
16+
* Class SeparateAppsTest
17+
*/
1518
class SeparateAppsTest extends \PHPUnit\Framework\TestCase
1619
{
1720
/**
1821
* @var ObjectManager
1922
*/
2023
private $objectManager;
2124

25+
/**
26+
* @inheritdoc
27+
*/
2228
protected function setUp()
2329
{
2430
$this->objectManager = Bootstrap::getObjectManager();
@@ -46,4 +52,25 @@ public function testAppNameIsSetWhenConfiguredCorrectly()
4652

4753
$state->setAreaCode('90210');
4854
}
55+
56+
/**
57+
* @magentoConfigFixture default/newrelicreporting/general/enable 1
58+
* @magentoConfigFixture default/newrelicreporting/general/app_name beverly_hills
59+
* @magentoConfigFixture default/newrelicreporting/general/separate_apps 0
60+
*/
61+
public function testAppNameIsNotSetWhenDisabled()
62+
{
63+
$newRelicWrapper = $this->getMockBuilder(NewRelicWrapper::class)
64+
->setMethods(['setAppName'])
65+
->getMock();
66+
67+
$this->objectManager->configure([NewRelicWrapper::class => ['shared' => true]]);
68+
$this->objectManager->addSharedInstance($newRelicWrapper, NewRelicWrapper::class);
69+
70+
$newRelicWrapper->expects($this->never())->method('setAppName');
71+
72+
$state = $this->objectManager->get(State::class);
73+
74+
$state->setAreaCode('90210');
75+
}
4976
}

0 commit comments

Comments
 (0)