|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\NewRelicReporting\Plugin; |
| 7 | + |
| 8 | +use Magento\Framework\App\State; |
| 9 | +use Magento\Framework\Exception\LocalizedException; |
| 10 | +use Magento\NewRelicReporting\Model\Config; |
| 11 | +use Magento\NewRelicReporting\Model\NewRelicWrapper; |
| 12 | + |
| 13 | +class StatePlugin |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var Config |
| 17 | + */ |
| 18 | + private $config; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var NewRelicWrapper |
| 22 | + */ |
| 23 | + private $newRelicWrapper; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param Config $config |
| 27 | + * @param NewRelicWrapper $newRelicWrapper |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + Config $config, |
| 31 | + NewRelicWrapper $newRelicWrapper |
| 32 | + ) { |
| 33 | + $this->config = $config; |
| 34 | + $this->newRelicWrapper = $newRelicWrapper; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Set separate appname |
| 39 | + * |
| 40 | + * @param State $subject |
| 41 | + * @param null $result |
| 42 | + * @return void |
| 43 | + * |
| 44 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 45 | + */ |
| 46 | + public function afterSetAreaCode(State $state, $result) |
| 47 | + { |
| 48 | + if (!$this->shouldSetAppName()) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + $this->newRelicWrapper->setAppName($this->appName($state)); |
| 54 | + } catch (LocalizedException $e) { |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private function appName(State $state) |
| 60 | + { |
| 61 | + $code = $state->getAreaCode(); |
| 62 | + $current = $this->config->getNewRelicAppName(); |
| 63 | + |
| 64 | + return $current . ';' . $current . '_' . $code; |
| 65 | + } |
| 66 | + |
| 67 | + private function shouldSetAppName() |
| 68 | + { |
| 69 | + if (!$this->config->isNewRelicEnabled()) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + if (!$this->config->getNewRelicAppName()) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + if (!$this->config->isSeparateApps()) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + return true; |
| 82 | + } |
| 83 | +} |
0 commit comments