Skip to content

Commit fbd7676

Browse files
authored
LYNX-289: Add property caching for customer_group_excluded_website
* LYNX-289: Add property caching for customer_group_excluded_website * LYNX-289: Remove obsolete SOAP test; CR changes * LYNX-289: CR changes
1 parent 298cbd4 commit fbd7676

File tree

5 files changed

+141
-58
lines changed

5 files changed

+141
-58
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\Customer\Model\Cache;
18+
19+
class GroupExcludedWebsiteCache
20+
{
21+
/**
22+
* @var array
23+
*/
24+
private array $customerGroupExcludedWebsite = [];
25+
26+
/**
27+
* Adds entry to GroupExcludedWebsite cache
28+
*
29+
* @param int $customerGroupId
30+
* @param array $value
31+
*/
32+
public function addToCache(int $customerGroupId, array $value)
33+
{
34+
$this->customerGroupExcludedWebsite[$customerGroupId] = $value;
35+
}
36+
37+
/**
38+
* Gets entry from GroupExcludedWebsite cache
39+
*
40+
* @param int $customerGroupId
41+
* @return array
42+
*/
43+
public function getFromCache(int $customerGroupId): array
44+
{
45+
return $this->customerGroupExcludedWebsite[$customerGroupId] ?? [];
46+
}
47+
48+
/**
49+
* Checks presence of cached customer group in GroupExcludedWebsite cache
50+
*
51+
* @param int $customerGroupId
52+
* @return bool
53+
*/
54+
public function isCached(int $customerGroupId): bool
55+
{
56+
return isset($this->customerGroupExcludedWebsite[$customerGroupId]);
57+
}
58+
59+
/**
60+
* Cleans the cache
61+
*/
62+
public function invalidate()
63+
{
64+
$this->customerGroupExcludedWebsite = [];
65+
}
66+
}

app/code/Magento/Customer/Model/Plugin/SaveCustomerGroupExcludedWebsite.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,4 @@ private function isValueChanged(array $currentValues, array $newValues): bool
145145
return !($currentValues === array_intersect($currentValues, $newValues)
146146
&& $newValues === array_intersect($newValues, $currentValues));
147147
}
148-
149148
}

app/code/Magento/Customer/Model/ResourceModel/GroupExcludedWebsite.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,40 @@
99

1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
12+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
13+
use Magento\Customer\Model\Cache\GroupExcludedWebsiteCache;
14+
use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
15+
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
16+
use Magento\Framework\Model\ResourceModel\Db\Context;
1217

1318
/**
1419
* Excluded customer group website resource model.
1520
*/
16-
class GroupExcludedWebsite extends AbstractDb
21+
class GroupExcludedWebsite extends AbstractDb implements ResetAfterRequestInterface
1722
{
23+
/**
24+
* @var GroupExcludedWebsiteCache $groupExcludedWebsiteCache
25+
*/
26+
private GroupExcludedWebsiteCache $groupExcludedWebsiteCache;
27+
28+
/**
29+
* @param Context $context
30+
* @param Snapshot $entitySnapshot
31+
* @param RelationComposite $entityRelationComposite
32+
* @param GroupExcludedWebsiteCache $groupExcludedWebsiteCache
33+
* @param string $connectionName
34+
*/
35+
public function __construct(
36+
Context $context,
37+
Snapshot $entitySnapshot,
38+
RelationComposite $entityRelationComposite,
39+
GroupExcludedWebsiteCache $groupExcludedWebsiteCache,
40+
$connectionName = null
41+
) {
42+
parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
43+
$this->groupExcludedWebsiteCache = $groupExcludedWebsiteCache;
44+
}
45+
1846
/**
1947
* Resource initialization
2048
*
@@ -25,15 +53,36 @@ protected function _construct()
2553
$this->_init('customer_group_excluded_website', 'entity_id');
2654
}
2755

56+
/**
57+
* @inheritDoc
58+
*/
59+
public function _resetState(): void
60+
{
61+
$this->groupExcludedWebsiteCache->invalidate();
62+
}
63+
64+
/**
65+
* Makes sure ExcludedWebsiteCache is invalidated when excluded websites are modified
66+
*/
67+
public function invalidateCache()
68+
{
69+
$this->_resetState();
70+
}
71+
2872
/**
2973
* Retrieve excluded website ids related to customer group.
3074
*
3175
* @param int $customerGroupId
3276
* @return array
3377
* @throws LocalizedException
3478
*/
79+
3580
public function loadCustomerGroupExcludedWebsites(int $customerGroupId): array
3681
{
82+
if ($this->groupExcludedWebsiteCache->isCached($customerGroupId)) {
83+
return $this->groupExcludedWebsiteCache->getFromCache($customerGroupId);
84+
}
85+
3786
$connection = $this->getConnection();
3887
$bind = ['customer_group_id' => $customerGroupId];
3988

@@ -44,7 +93,8 @@ public function loadCustomerGroupExcludedWebsites(int $customerGroupId): array
4493
'customer_group_id = :customer_group_id'
4594
);
4695

47-
return $connection->fetchCol($select, $bind);
96+
$this->groupExcludedWebsiteCache->addToCache($customerGroupId, $connection->fetchCol($select, $bind));
97+
return $this->groupExcludedWebsiteCache->getFromCache($customerGroupId);
4898
}
4999

50100
/**
@@ -76,6 +126,7 @@ public function delete($customerGroupId)
76126
{
77127
$connection = $this->getConnection();
78128
$connection->beginTransaction();
129+
$this->invalidateCache();
79130
try {
80131
$where = $connection->quoteInto('customer_group_id = ?', $customerGroupId);
81132
$connection->delete(
@@ -87,7 +138,6 @@ public function delete($customerGroupId)
87138
$connection->rollBack();
88139
throw $e;
89140
}
90-
91141
return $this;
92142
}
93143

app/code/Magento/Customer/Model/ResourceModel/GroupExcludedWebsiteRepository.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class GroupExcludedWebsiteRepository implements GroupExcludedWebsiteRepositoryIn
2626
* @param GroupExcludedWebsite $groupExcludedWebsiteResourceModel
2727
*/
2828
public function __construct(
29-
GroupExcludedWebsite $groupExcludedWebsiteResourceModel
29+
GroupExcludedWebsite $groupExcludedWebsiteResourceModel,
3030
) {
3131
$this->groupExcludedWebsiteResourceModel = $groupExcludedWebsiteResourceModel;
3232
}
@@ -43,7 +43,6 @@ public function save(GroupExcludedWebsiteInterface $groupExcludedWebsite): Group
4343
__('Could not save customer group website to exclude from customer group: "%1"', $e->getMessage())
4444
);
4545
}
46-
4746
return $groupExcludedWebsite;
4847
}
4948

@@ -78,8 +77,8 @@ public function getAllExcludedWebsites(): array
7877

7978
if (!empty($allExcludedWebsites)) {
8079
foreach ($allExcludedWebsites as $allExcludedWebsite) {
81-
$customerGroupId = (int)$allExcludedWebsite['customer_group_id'];
82-
$websiteId = (int)$allExcludedWebsite['website_id'];
80+
$customerGroupId = (int) $allExcludedWebsite['customer_group_id'];
81+
$websiteId = (int) $allExcludedWebsite['website_id'];
8382
$excludedWebsites[$customerGroupId][] = $websiteId;
8483
}
8584
}
@@ -109,7 +108,7 @@ public function delete(int $customerGroupId): bool
109108
public function deleteByWebsite(int $websiteId): bool
110109
{
111110
try {
112-
return (bool)$this->groupExcludedWebsiteResourceModel->deleteByWebsite($websiteId);
111+
return (bool) $this->groupExcludedWebsiteResourceModel->deleteByWebsite($websiteId);
113112
} catch (LocalizedException $e) {
114113
throw new LocalizedException(
115114
__('Could not delete customer group excluded website by id.')

dev/tests/api-functional/testsuite/Magento/Customer/Api/GroupRepositoryTest.php

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
*/
2727
class GroupRepositoryTest extends WebapiAbstract
2828
{
29-
const SERVICE_NAME = "customerGroupRepositoryV1";
30-
const SERVICE_VERSION = "V1";
31-
const RESOURCE_PATH = "/V1/customerGroups";
29+
private const SERVICE_NAME = "customerGroupRepositoryV1";
30+
private const SERVICE_VERSION = "V1";
31+
private const RESOURCE_PATH = "/V1/customerGroups";
3232

3333
/**
3434
* @var GroupRegistry
@@ -512,16 +512,27 @@ public function testUpdateGroupWithExcludedWebsiteRest(): void
512512

513513
self::assertEquals($groupId, $this->_webApiCall($serviceInfo, $requestData)[CustomerGroup::ID]);
514514

515-
$group = $this->groupRepository->getById($groupId);
516-
self::assertEquals($groupData[CustomerGroup::CODE], $group->getCode(), 'The group code did not change.');
515+
$serviceInfo = [
516+
'rest' => [
517+
'resourcePath' => self::RESOURCE_PATH . "/$groupId",
518+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
519+
],
520+
];
521+
522+
$group = $this->_webApiCall($serviceInfo);
523+
self::assertEquals(
524+
$groupData[CustomerGroup::CODE],
525+
$group['code'],
526+
'The group code did not change.'
527+
);
517528
self::assertEquals(
518529
$groupData[CustomerGroup::TAX_CLASS_ID],
519-
$group->getTaxClassId(),
530+
$group['tax_class_id'],
520531
'The group tax class id did not change'
521532
);
522533
self::assertEquals(
523534
['1'],
524-
$group->getExtensionAttributes()->getExcludeWebsiteIds(),
535+
$group['extension_attributes']['exclude_website_ids'],
525536
'The group excluded websites do not match.'
526537
);
527538
}
@@ -850,48 +861,6 @@ public function testUpdateGroupSoap()
850861
);
851862
}
852863

853-
/**
854-
* Verify that updating an existing group with excluded website works via SOAP.
855-
*/
856-
public function testUpdateGroupWithExcludedWebsiteSoap(): void
857-
{
858-
$this->_markTestAsSoapOnly();
859-
$group = $this->customerGroupFactory->create();
860-
$group->setId(null);
861-
$group->setCode('New Group with Exclude SOAP');
862-
$group->setTaxClassId(3);
863-
$groupId = $this->createGroup($group);
864-
865-
$serviceInfo = [
866-
'soap' => [
867-
'service' => self::SERVICE_NAME,
868-
'serviceVersion' => self::SERVICE_VERSION,
869-
'operation' => 'customerGroupRepositoryV1Save',
870-
],
871-
];
872-
873-
$groupData = [
874-
CustomerGroup::ID => $groupId,
875-
CustomerGroup::CODE => 'Updated Group with Exclude SOAP',
876-
'taxClassId' => 3,
877-
'extension_attributes' => ['exclude_website_ids' => ['1']]
878-
];
879-
$this->_webApiCall($serviceInfo, ['group' => $groupData]);
880-
881-
$group = $this->groupRepository->getById($groupId);
882-
self::assertEquals($groupData[CustomerGroup::CODE], $group->getCode(), 'The group code did not change.');
883-
self::assertEquals(
884-
$groupData['taxClassId'],
885-
$group->getTaxClassId(),
886-
'The group tax class id did not change'
887-
);
888-
self::assertEquals(
889-
['1'],
890-
$group->getExtensionAttributes()->getExcludeWebsiteIds(),
891-
'The group excluded websites do not match.'
892-
);
893-
}
894-
895864
/**
896865
* Verify that updating a non-existing group throws an exception via SOAP.
897866
*/

0 commit comments

Comments
 (0)