Skip to content

Commit bcff704

Browse files
committed
Refactored getCustomerGroupCode method
1 parent 9c6a1c3 commit bcff704

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Model/AbstractDataLayer.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Magento\Framework\App\ObjectManager;
2020
use Magento\Customer\Model\Session;
2121
use Magento\Customer\Model\ResourceModel\GroupRepository;
22-
use Psr\Log\LoggerInterface;
2322

2423
class AbstractDataLayer
2524
{
@@ -59,9 +58,9 @@ class AbstractDataLayer
5958
protected $groupRepository;
6059

6160
/**
62-
* @var LoggerInterface
61+
* @var string
6362
*/
64-
protected $logger;
63+
protected $customerGroupCode;
6564

6665
/**
6766
* AbstractDataLayer constructor.
@@ -77,8 +76,7 @@ public function __construct(
7776
RequestInterface $request = null,
7877
Registry $registry = null,
7978
Session $session = null,
80-
GroupRepository $groupRepository = null,
81-
LoggerInterface $logger = null
79+
GroupRepository $groupRepository = null
8280
) {
8381
$this->config = $config;
8482
$this->storeManager = $storeManager;
@@ -95,9 +93,6 @@ public function __construct(
9593
$this->groupRepository = $groupRepository ?: ObjectManager::getInstance()->get(
9694
GroupRepository::class
9795
);
98-
$this->logger = $logger ?: ObjectManager::getInstance()->get(
99-
LoggerInterface::class
100-
);
10196
}
10297

10398
/**
@@ -238,15 +233,21 @@ protected function getProductAttributeValue(Product $product, ?string $attribute
238233
* @throws NoSuchEntityException
239234
* @throws \Magento\Framework\Exception\LocalizedException
240235
*/
241-
protected function getCustomerGroupCode() {
242-
$customerGroupId = $this->session->getCustomerGroupId();
243-
if ($customerGroupId) {
244-
try {
245-
$this->groupRepository->getById($customerGroupId)->getCode();
246-
} catch (\Magento\Setup\Exception $e) {
247-
$this->logger->critical($e->getMessage());
236+
protected function getCustomerGroupCode(): string
237+
{
238+
if (null === $this->customerGroupCode) {
239+
$this->customerGroupCode = '';
240+
$customerGroupId = $this->session->getCustomerGroupId();
241+
if ($customerGroupId) {
242+
try {
243+
$group = $this->groupRepository->getById($customerGroupId);
244+
$this->customerGroupCode = (string)$group->getCode();
245+
} catch (NoSuchEntityException $e) {
246+
/* Do nothing */
247+
}
248248
}
249249
}
250-
return '';
250+
251+
return $this->customerGroupCode;
251252
}
252253
}

0 commit comments

Comments
 (0)