Skip to content

Commit

Permalink
OXDEV-7771 Add refactoring suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
liulka-oxid committed May 15, 2024
1 parent 9e9e46e commit 50d1aab
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 173 deletions.
22 changes: 4 additions & 18 deletions source/Internal/Framework/Cache/Command/ClearCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache\Command;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ItemPoolFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Cache\ShopCacheCleanerInterface;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Service\ContainerCacheInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -22,12 +19,9 @@
class ClearCacheCommand extends Command
{
public function __construct(
private readonly ShopAdapterInterface $shopAdapter,
private readonly ShopTemplateCacheServiceInterface $shopTemplateCacheService,
private readonly ContainerCacheInterface $containerCache,
private readonly ContextInterface $context,
private readonly ItemPoolFactoryInterface $itemPoolFactory,
private readonly ShopPoolFactoryInterface $shopPoolFactory
private readonly ShopCacheCleanerInterface $shopCacheCleaner,
) {
parent::__construct();
}
Expand All @@ -39,19 +33,11 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->shopTemplateCacheService->invalidateAllShopsCache();
$this->shopAdapter->invalidateModulesCache();

$this->shopCacheCleaner->clearAll();
foreach ($this->context->getAllShopIds() as $shopId) {
$this->containerCache->invalidate($shopId);

$this
->shopPoolFactory
->create($this->itemPoolFactory->create($shopId))
->invalidate($shopId);
}

$output->writeln("<info>Cleared cache files</info>");
$output->writeln('<info>Cleared cache files</info>');

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

use Psr\Cache\CacheItemPoolInterface;

interface ItemPoolFactoryInterface
interface CacheItemPoolFactoryInterface
{
public function create(int $shopId): CacheItemPoolInterface;

public function createForCurrentShop(): CacheItemPoolInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Filesystem\Path;

class ItemPoolFactory implements ItemPoolFactoryInterface
class FilesystemCacheItemPoolFactory implements CacheItemPoolFactoryInterface
{
public function __construct(private readonly ContextInterface $context)
{
Expand All @@ -22,16 +23,8 @@ public function __construct(private readonly ContextInterface $context)
public function create(int $shopId): CacheItemPoolInterface
{
return new FilesystemAdapter(
namespace: ShopPoolNameFactory::get($shopId),
directory: $this->context->getCacheDirectory()
namespace: "cache_items_shop_$shopId",
directory: Path::join($this->context->getCacheDirectory(), 'pool',)
);
}

public function createForCurrentShop(): CacheItemPoolInterface
{
return new FilesystemAdapter(
namespace: ShopPoolNameFactory::get($this->context->getCurrentShopId()),
directory: $this->context->getCacheDirectory()
);
}
}
}
28 changes: 0 additions & 28 deletions source/Internal/Framework/Cache/Pool/ShopPoolFactory.php

This file was deleted.

15 changes: 0 additions & 15 deletions source/Internal/Framework/Cache/Pool/ShopPoolFactoryInterface.php

This file was deleted.

28 changes: 0 additions & 28 deletions source/Internal/Framework/Cache/Pool/ShopPoolNameFactory.php

This file was deleted.

31 changes: 0 additions & 31 deletions source/Internal/Framework/Cache/Pool/ShopPoolService.php

This file was deleted.

13 changes: 0 additions & 13 deletions source/Internal/Framework/Cache/Pool/ShopPoolServiceInterface.php

This file was deleted.

17 changes: 4 additions & 13 deletions source/Internal/Framework/Cache/Pool/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ services:
_defaults:
autowire: true

OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ItemPoolFactoryInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ItemPoolFactory
OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CacheItemPoolFactoryInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\FilesystemCacheItemPoolFactory

Psr\Cache\CacheItemPoolInterface:
factory: ['@OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ItemPoolFactoryInterface', 'createForCurrentShop']

Symfony\Contracts\Cache\CacheInterface:
factory: ['@OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ItemPoolFactoryInterface', 'createForCurrentShop']

OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolFactoryInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolFactory

OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolServiceInterface:
factory: ['@OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolFactoryInterface', 'create']
public: true
factory: ['@OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CacheItemPoolFactoryInterface', 'create']
arguments: ['@=service("OxidEsales\\EshopCommunity\\Internal\\Transition\\Utility\\ContextInterface").getCurrentShopId()']
15 changes: 15 additions & 0 deletions source/Internal/Framework/Cache/ShopCacheCleanerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache;

interface ShopCacheCleanerInterface
{
public function clear(int $shopId): void;

public function clearAll(): void;
}
42 changes: 42 additions & 0 deletions source/Internal/Framework/Cache/ShopCacheFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CacheItemPoolFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;

class ShopCacheFacade implements ShopCacheCleanerInterface
{
public function __construct(
private readonly ContextInterface $context,
private readonly CacheItemPoolFactoryInterface $cacheItemPoolFactory,
private readonly ShopAdapterInterface $shopAdapter,
private readonly ShopTemplateCacheServiceInterface $templateCacheService,
) {
}

public function clear(int $shopId): void
{
$this->shopAdapter->invalidateModulesCache();
$this->templateCacheService->invalidateCache($shopId);
$this->cacheItemPoolFactory->create($shopId)->clear();
}

public function clearAll(): void
{
$this->shopAdapter->invalidateModulesCache();
$this->templateCacheService->invalidateAllShopsCache();
foreach ($this->context->getAllShopIds() as $shopId) {
$this->cacheItemPoolFactory->create($shopId)->clear();
}
}
}
8 changes: 8 additions & 0 deletions source/Internal/Framework/Cache/services.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
imports:
- { resource: Pool/services.yaml }
- { resource: Command/services.yaml }

services:
_defaults:
autowire: true

OxidEsales\EshopCommunity\Internal\Framework\Cache\ShopCacheCleanerInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Cache\ShopCacheFacade
public: true
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\Module\Cache;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Cache\ShopCacheCleanerInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Event\ModuleConfigurationChangedEvent;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Event\FinalizingModuleActivationEvent;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Event\FinalizingModuleDeactivationEvent;
Expand All @@ -18,13 +18,13 @@

class InvalidateModuleCacheEventSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly ShopPoolServiceInterface $shopPoolService)
public function __construct(private readonly ShopCacheCleanerInterface $shopCacheCleaner)
{
}

public function invalidateModuleCache(ModuleSetupEvent|ModuleConfigurationChangedEvent $event): void
{
$this->shopPoolService->invalidate($event->getShopId());
$this->shopCacheCleaner->clear($event->getShopId());
}

public static function getSubscribedEvents(): array
Expand Down
11 changes: 3 additions & 8 deletions source/Internal/Framework/Theme/Command/ThemeActivateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\Theme\Command;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\ShopPoolServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Cache\ShopCacheCleanerInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -26,9 +24,7 @@ class ThemeActivateCommand extends Command

public function __construct(
private readonly ShopAdapterInterface $shopAdapter,
private readonly ShopTemplateCacheServiceInterface $shopTemplateCacheService,
private readonly ShopPoolServiceInterface $shopPoolService,
private readonly BasicContextInterface $context
private readonly ShopCacheCleanerInterface $shopCacheCleaner,
) {
parent::__construct();
}
Expand Down Expand Up @@ -59,8 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->shopAdapter->activateTheme($themeId);
$this->shopPoolService->invalidate($this->context->getCurrentShopId());
$this->shopTemplateCacheService->invalidateAllShopsCache();
$this->shopCacheCleaner->clearAll();
$output->writeLn('<info>' . sprintf(self::MESSAGE_THEME_ACTIVATED, $themeId) . '</info>');

return Command::SUCCESS;
Expand Down

0 comments on commit 50d1aab

Please sign in to comment.