|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Omikron\FactFinder\Shopware6\Config; |
| 6 | + |
| 7 | +use Symfony\Component\Cache\Adapter\AdapterInterface; |
| 8 | + |
| 9 | +class CachedFieldRoles implements FieldRolesInterface |
| 10 | +{ |
| 11 | + private FieldRolesInterface $decorated; |
| 12 | + private AdapterInterface $cache; |
| 13 | + |
| 14 | + public function __construct(FieldRolesInterface $decorated, AdapterInterface $cache) |
| 15 | + { |
| 16 | + $this->decorated = $decorated; |
| 17 | + $this->cache = $cache; |
| 18 | + } |
| 19 | + |
| 20 | + public function getRoles(?string $salesChannelId): array |
| 21 | + { |
| 22 | + $salesChannelId = $salesChannelId ?? ''; |
| 23 | + $cacheKey = $this->getCacheKey($salesChannelId); |
| 24 | + $item = $this->cache->getItem($cacheKey); |
| 25 | + |
| 26 | + if ($item->isHit()) { |
| 27 | + return $item->get(); |
| 28 | + } |
| 29 | + |
| 30 | + $fieldRoles = $this->decorated->getRoles($salesChannelId); |
| 31 | + $item->set($fieldRoles); |
| 32 | + $this->cache->save($item); |
| 33 | + |
| 34 | + return $fieldRoles; |
| 35 | + } |
| 36 | + |
| 37 | + public function update( |
| 38 | + array $fieldRoles, |
| 39 | + ?string $salesChannelId |
| 40 | + ): void { |
| 41 | + $salesChannelId = $salesChannelId ?? ''; |
| 42 | + $this->decorated->update($fieldRoles, $salesChannelId); |
| 43 | + $cacheKey = $this->getCacheKey($salesChannelId); |
| 44 | + |
| 45 | + if ($this->cache->hasItem($cacheKey)) { |
| 46 | + $this->cache->deleteItem($cacheKey); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private function getCacheKey(string $salesChannelId): string |
| 51 | + { |
| 52 | + return sprintf('factfinder-field-roles-%s', $salesChannelId); |
| 53 | + } |
| 54 | +} |
0 commit comments