From b8da61408078b49ca521cfc5653d55d97b6bdbe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 9 Jan 2023 09:29:30 +0100 Subject: [PATCH] Remove the cached provider functionality --- .../Compiler/RegisterProvidersPass.php | 14 -- src/DependencyInjection/Configuration.php | 11 -- .../SetonoSyliusPickupPointExtension.php | 16 --- src/Provider/CachedProvider.php | 132 ------------------ .../packages/setono_sylius_pickup_point.yaml | 3 - 5 files changed, 176 deletions(-) delete mode 100644 src/Provider/CachedProvider.php diff --git a/src/DependencyInjection/Compiler/RegisterProvidersPass.php b/src/DependencyInjection/Compiler/RegisterProvidersPass.php index 2675d7f..c6a96e7 100644 --- a/src/DependencyInjection/Compiler/RegisterProvidersPass.php +++ b/src/DependencyInjection/Compiler/RegisterProvidersPass.php @@ -5,7 +5,6 @@ namespace Setono\SyliusPickupPointPlugin\DependencyInjection\Compiler; use InvalidArgumentException; -use Setono\SyliusPickupPointPlugin\Provider\CachedProvider; use Setono\SyliusPickupPointPlugin\Provider\LocalProvider; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -21,7 +20,6 @@ public function process(ContainerBuilder $container): void } $registry = $container->getDefinition('setono_sylius_pickup_point.registry.provider'); - $cacheEnabled = $container->getParameter('setono_sylius_pickup_point.cache.enabled') === true; $localEnabled = $container->getParameter('setono_sylius_pickup_point.local') === true; $typeToLabelMap = []; @@ -33,18 +31,6 @@ public function process(ContainerBuilder $container): void $typeToLabelMap[$attributes['code']] = $attributes['label']; - if ($cacheEnabled) { - $decoratedId = $id; - $id .= '.cached'; // overwrite the id - $cachedDefinition = new Definition(CachedProvider::class, [ - new Reference('setono_sylius_pickup_point.cache'), - new Reference($id . '.inner'), - ]); - $cachedDefinition->setDecoratedService($decoratedId, null, 256); - - $container->setDefinition($id, $cachedDefinition); - } - if ($localEnabled) { $decoratedId = $id; $id .= '.local'; // overwrite the id diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index e6cbedf..ff68faf 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -29,17 +29,6 @@ public function getConfigTreeBuilder(): TreeBuilder ->addDefaultsIfNotSet() ->children() ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end() - ->arrayNode('cache') - ->addDefaultsIfNotSet() - ->children() - ->booleanNode('enabled') - ->defaultFalse() - ->end() - ->scalarNode('pool') - ->defaultNull() - ->end() - ->end() - ->end() ->booleanNode('local') ->defaultValue(true) ->info('Whether to use the local database when timeouts occur in third party HTTP calls. Remember to run the setono-sylius-pickup-point:load-pickup-points command periodically to populate the local database with pickup points') diff --git a/src/DependencyInjection/SetonoSyliusPickupPointExtension.php b/src/DependencyInjection/SetonoSyliusPickupPointExtension.php index bff3c93..22a5d09 100644 --- a/src/DependencyInjection/SetonoSyliusPickupPointExtension.php +++ b/src/DependencyInjection/SetonoSyliusPickupPointExtension.php @@ -6,7 +6,6 @@ use LogicException; use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension; -use Symfony\Component\Cache\Adapter\AdapterInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; @@ -28,21 +27,6 @@ public function load(array $configs, ContainerBuilder $container): void $bundles = $container->hasParameter('kernel.bundles') ? $container->getParameter('kernel.bundles') : []; Assert::isArray($bundles); - $cacheEnabled = $config['cache']['enabled']; - if ($cacheEnabled) { - if (!interface_exists(AdapterInterface::class)) { - throw new LogicException('Using cache is only supported when symfony/cache is installed.'); - } - - if (null === $config['cache']['pool']) { - throw new LogicException('You should specify pool in order to use cache for pickup point providers.'); - } - - $container->setAlias('setono_sylius_pickup_point.cache', $config['cache']['pool']); - } - - $container->setParameter('setono_sylius_pickup_point.cache.enabled', $cacheEnabled); - if ($config['providers']['faker']) { if ('prod' === $container->getParameter('kernel.environment')) { throw new LogicException("You can't use faker provider in production environment."); diff --git a/src/Provider/CachedProvider.php b/src/Provider/CachedProvider.php deleted file mode 100644 index 78f39ea..0000000 --- a/src/Provider/CachedProvider.php +++ /dev/null @@ -1,132 +0,0 @@ -cacheItemPool = $cacheItemPool; - $this->provider = $provider; - } - - public function findPickupPoints(OrderInterface $order): iterable - { - $orderCacheKey = $this->buildOrderCacheKey($order); - if (!$this->cacheItemPool->hasItem($orderCacheKey)) { - $pickupPoints = $this->provider->findPickupPoints($order); - - if ($pickupPoints instanceof Generator) { - $pickupPoints = iterator_to_array($pickupPoints); - } - - $pickupPointsCacheItem = $this->cacheItemPool->getItem($orderCacheKey); - $pickupPointsCacheItem->set($pickupPoints); - $this->cacheItemPool->save($pickupPointsCacheItem); - - // Store separate PickupPoints to retrieve at findOnePickupPointById - /** @var PickupPointInterface $pickupPoint */ - foreach ($pickupPoints as $pickupPoint) { - $pickupPointCacheKey = $this->buildPickupPointIdCacheKey($pickupPoint->getCode()); - $pickupPointCacheItem = $this->cacheItemPool->getItem($pickupPointCacheKey); - $pickupPointCacheItem->set($pickupPoint); - $this->cacheItemPool->save($pickupPointCacheItem); - } - } - - /** @var PickupPointInterface[] $pickupPoints */ - $pickupPoints = $this->cacheItemPool->getItem($orderCacheKey)->get(); - - return $pickupPoints; - } - - public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface - { - $pickupPointCacheKey = $this->buildPickupPointIdCacheKey($code); - if (!$this->cacheItemPool->hasItem($pickupPointCacheKey)) { - $pickupPoint = $this->provider->findPickupPoint($code); - if (null === $pickupPoint) { - // Do not cache PickupPoint that wasn't found - return null; - } - - $pickupPointCacheItem = $this->cacheItemPool->getItem($pickupPointCacheKey); - $pickupPointCacheItem->set($pickupPoint); - $this->cacheItemPool->save($pickupPointCacheItem); - } - - /** @var PickupPointInterface $pickupPoint */ - $pickupPoint = $this->cacheItemPool->getItem($pickupPointCacheKey)->get(); - - return $pickupPoint; - } - - public function findAllPickupPoints(): iterable - { - yield from $this->provider->findAllPickupPoints(); - } - - public function getCode(): string - { - return $this->provider->getCode(); - } - - public function getName(): string - { - return $this->provider->getName(); - } - - private function buildOrderCacheKey(OrderInterface $order): string - { - $shippingAddress = $order->getShippingAddress(); - if (!$shippingAddress instanceof AddressInterface) { - throw new RuntimeException(sprintf( - 'Shipping address was not found for order #%s', - $order->getNumber() - )); - } - - $countryCode = $shippingAddress->getCountryCode(); - Assert::notNull($countryCode); - - $postCode = $shippingAddress->getPostcode(); - Assert::notNull($postCode); - - $street = $shippingAddress->getStreet(); - Assert::notNull($street); - - $slugger = new AsciiSlugger(); - - // As far as DAO/Gls/PostNord using only these 3 fields to - // search for pickup points, we should build cache key based on them only - return sprintf( - '%s-%s-%s-%s', - $this->getCode(), - $slugger->slug($countryCode)->toString(), - $slugger->slug($postCode)->toString(), - $slugger->slug($street)->toString() - ); - } - - private function buildPickupPointIdCacheKey(PickupPointCode $id): string - { - return $id->getValue(); - } -} diff --git a/tests/Application/config/packages/setono_sylius_pickup_point.yaml b/tests/Application/config/packages/setono_sylius_pickup_point.yaml index 0265651..5243a51 100644 --- a/tests/Application/config/packages/setono_sylius_pickup_point.yaml +++ b/tests/Application/config/packages/setono_sylius_pickup_point.yaml @@ -9,9 +9,6 @@ framework: adapter: cache.app setono_sylius_pickup_point: - cache: - enabled: true - pool: setono_sylius_pickup_point.provider_cache_pool local: true providers: faker: true