Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/code/Magento/NewRelicReporting/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\NewRelicReporting\Model;

/**
* NewRelic configuration model
*/
class Config
{
/**#@+
Expand Down
45 changes: 23 additions & 22 deletions app/code/Magento/NewRelicReporting/Plugin/StatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\NewRelicReporting\Plugin;

use Magento\Framework\App\State;
Expand All @@ -11,6 +13,9 @@
use Magento\NewRelicReporting\Model\NewRelicWrapper;
use Psr\Log\LoggerInterface;

/**
* Handles setting which, when enabled, reports frontend and adminhtml as separate apps to New Relic.
*/
class StatePlugin
{
/**
Expand All @@ -31,6 +36,7 @@ class StatePlugin
/**
* @param Config $config
* @param NewRelicWrapper $newRelicWrapper
* @param LoggerInterface $logger
*/
public function __construct(
Config $config,
Expand All @@ -46,32 +52,33 @@ public function __construct(
* Set separate appname
*
* @param State $subject
* @param null $result
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @param mixed $result
* @return mixed
*/
public function afterSetAreaCode(State $state, $result)
public function afterSetAreaCode(State $subject, $result)
{
if (!$this->shouldSetAppName()) {
return $result;
}

try {
$this->newRelicWrapper->setAppName($this->appName($state));
$this->newRelicWrapper->setAppName($this->appName($subject));
} catch (LocalizedException $e) {
$this->logger->critical($e);
return $result;
}

return $result;
}

/**
* @param State $state
* Format appName.
*
* @param State $state
* @return string
* @throws LocalizedException
*/
private function appName(State $state)
private function appName(State $state): string
{
$code = $state->getAreaCode();
$current = $this->config->getNewRelicAppName();
Expand All @@ -80,22 +87,16 @@ private function appName(State $state)
}

/**
* Check if app name should be set.
*
* @return bool
*/
private function shouldSetAppName()
private function shouldSetAppName(): bool
{
if (!$this->config->isNewRelicEnabled()) {
return false;
}

if (!$this->config->getNewRelicAppName()) {
return false;
}

if (!$this->config->isSeparateApps()) {
return false;
}

return true;
return (
$this->config->isSeparateApps() &&
$this->config->getNewRelicAppName() &&
$this->config->isNewRelicEnabled()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\NewRelicReporting\Plugin;

use Magento\Framework\App\State;
use Magento\NewRelicReporting\Model\NewRelicWrapper;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\Helper\Bootstrap;

/**
* Class SeparateAppsTest
*/
class SeparateAppsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
Expand Down Expand Up @@ -44,4 +52,25 @@ public function testAppNameIsSetWhenConfiguredCorrectly()

$state->setAreaCode('90210');
}

/**
* @magentoConfigFixture default/newrelicreporting/general/enable 1
* @magentoConfigFixture default/newrelicreporting/general/app_name beverly_hills
* @magentoConfigFixture default/newrelicreporting/general/separate_apps 0
*/
public function testAppNameIsNotSetWhenDisabled()
{
$newRelicWrapper = $this->getMockBuilder(NewRelicWrapper::class)
->setMethods(['setAppName'])
->getMock();

$this->objectManager->configure([NewRelicWrapper::class => ['shared' => true]]);
$this->objectManager->addSharedInstance($newRelicWrapper, NewRelicWrapper::class);

$newRelicWrapper->expects($this->never())->method('setAppName');

$state = $this->objectManager->get(State::class);

$state->setAreaCode('90210');
}
}