Skip to content

Commit 4da87b0

Browse files
committed
Merge branch 'B2B-2255' into B2B-2258
2 parents defe989 + 66d027c commit 4da87b0

File tree

7 files changed

+955
-10
lines changed

7 files changed

+955
-10
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy;
9+
10+
use Magento\DirectoryGraphQl\Model\Resolver\Currency\Identity;
11+
use Magento\Framework\App\Cache\Tag\StrategyInterface;
12+
use Magento\Framework\App\Config\ValueInterface;
13+
use Magento\Store\Model\ScopeInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
16+
/**
17+
* Produce cache tags for currency config.
18+
*/
19+
class CurrencyConfig implements StrategyInterface
20+
{
21+
/**
22+
* @var string[]
23+
*/
24+
private $currencyConfigPaths = [
25+
'currency/options/base',
26+
'currency/options/default',
27+
'currency/options/allow',
28+
'currency/options/customsymbol'
29+
];
30+
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* @param StoreManagerInterface $storeManager
38+
*/
39+
public function __construct(
40+
StoreManagerInterface $storeManager
41+
) {
42+
$this->storeManager = $storeManager;
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function getTags($object): array
49+
{
50+
if (!is_object($object)) {
51+
throw new \InvalidArgumentException('Provided argument is not an object');
52+
}
53+
54+
if ($object instanceof ValueInterface
55+
&& in_array($object->getPath(), $this->currencyConfigPaths)
56+
&& $object->isValueChanged()
57+
) {
58+
if ($object->getScope() == ScopeInterface::SCOPE_WEBSITES) {
59+
$website = $this->storeManager->getWebsite($object->getScopeId());
60+
$storeIds = $website->getStoreIds();
61+
} elseif ($object->getScope() == ScopeInterface::SCOPE_STORES) {
62+
$storeIds = [$object->getScopeId()];
63+
} else {
64+
$storeIds = array_keys($this->storeManager->getStores());
65+
}
66+
$ids = [];
67+
foreach ($storeIds as $storeId) {
68+
$ids[] = sprintf('%s_%s', Identity::CACHE_TAG, $storeId);
69+
}
70+
return $ids;
71+
}
72+
73+
return [];
74+
}
75+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DirectoryGraphQl\Model\Resolver\Currency;
9+
10+
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
class Identity implements IdentityInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
public const CACHE_TAG = 'gql_currency';
19+
20+
/**
21+
* @var StoreManagerInterface
22+
*/
23+
private $storeManager;
24+
25+
/**
26+
* @param StoreManagerInterface $storeManager
27+
*/
28+
public function __construct(StoreManagerInterface $storeManager)
29+
{
30+
$this->storeManager = $storeManager;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function getIdentities(array $resolvedData): array
37+
{
38+
if (empty($resolvedData)) {
39+
return [];
40+
}
41+
$storeId = $this->storeManager->getStore()->getId();
42+
return [self::CACHE_TAG, sprintf('%s_%s', self::CACHE_TAG, $storeId)];
43+
}
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\DirectoryGraphQl\Plugin;
9+
10+
use Magento\DirectoryGraphQl\Model\Resolver\Currency\Identity;
11+
use Magento\Framework\DataObject\IdentityInterface;
12+
use Magento\Framework\Event\ManagerInterface;
13+
use Magento\Directory\Model\Currency as CurrencyModel;
14+
15+
/**
16+
* Currency plugin triggers clean page cache and provides currency cache identities
17+
*/
18+
class Currency implements IdentityInterface
19+
{
20+
/**
21+
* Application Event Dispatcher
22+
*
23+
* @var ManagerInterface
24+
*/
25+
private $eventManager;
26+
27+
/**
28+
* @param ManagerInterface $eventManager
29+
*/
30+
public function __construct(ManagerInterface $eventManager)
31+
{
32+
$this->eventManager = $eventManager;
33+
}
34+
35+
/**
36+
* Trigger clean cache by tags after save rates
37+
*
38+
* @param CurrencyModel $subject
39+
* @param CurrencyModel $result
40+
* @return CurrencyModel
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
42+
*/
43+
public function afterSaveRates(CurrencyModel $subject, CurrencyModel $result): CurrencyModel
44+
{
45+
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
46+
return $result;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function getIdentities()
53+
{
54+
return [Identity::CACHE_TAG];
55+
}
56+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Framework\App\Cache\Tag\Strategy\Factory">
10+
<arguments>
11+
<argument name="customStrategies" xsi:type="array">
12+
<item name="Magento\Framework\App\Config\ValueInterface" xsi:type="object">
13+
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\CurrencyConfig
14+
</item>
15+
</argument>
16+
</arguments>
17+
</type>
18+
<type name="Magento\Directory\Model\Currency">
19+
<plugin name="afterSaveRate" type="Magento\DirectoryGraphQl\Plugin\Currency" />
20+
</type>
21+
</config>

app/code/Magento/DirectoryGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# See COPYING.txt for license details.
33

44
type Query {
5-
currency: Currency @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Currency") @doc(description: "Return information about the store's currency.") @cache(cacheable: false)
5+
currency: Currency @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Currency") @doc(description: "Return information about the store's currency.") @cache(cacheIdentity: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Currency\\Identity")
66
countries: [Country] @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Countries") @doc(description: "The countries query provides information for all countries.") @cache(cacheable: false)
77
country (id: String): Country @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Country") @doc(description: "The countries query provides information for a single country.") @cache(cacheable: false)
88
}

0 commit comments

Comments
 (0)