Permalink
Browse files

OXDEV-1284 Refactor ModuleServicesActivationServiceInterface

  • Loading branch information...
iegupov committed Dec 24, 2018
1 parent 752732e commit 671c09656cbcaf1fef7e8cd857b0a45718f4226a
@@ -143,7 +143,7 @@ private function activateShopAwareServices(\OxidEsales\Eshop\Core\Module\Module
{
/** @var ModuleServicesActivationServiceInterface $shopActivationService */
$shopActivationService = ContainerFactory::getInstance()->getContainer()->get(ModuleServicesActivationServiceInterface::class);
$shopActivationService->activateServicesForShops($module->getModuleFullPath(), [Registry::getConfig()->getShopId()]);
$shopActivationService->activateModuleServices($module->getId(), Registry::getConfig()->getShopId());
}
/**
@@ -153,7 +153,7 @@ private function deactivateShopAwareServices(\OxidEsales\Eshop\Core\Module\Modul
{
/** @var ModuleServicesActivationServiceInterface $shopActivationService */
$shopActivationService = ContainerFactory::getInstance()->getContainer()->get(ModuleServicesActivationServiceInterface::class);
$shopActivationService->deactivateServicesForShops($module->getModuleFullPath(), [Registry::getConfig()->getShopId()]);
$shopActivationService->deactivateModuleServices($module->getId(), Registry::getConfig()->getShopId());
}
/**
@@ -6,6 +6,7 @@
namespace OxidEsales\EshopCommunity\Internal\Module\Setup\Service;
use OxidEsales\EshopCommunity\Internal\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Application\Dao\ProjectYamlDaoInterface;
use OxidEsales\EshopCommunity\Internal\Application\DataObject\DIConfigWrapper;
use OxidEsales\EshopCommunity\Internal\Application\DataObject\DIServiceWrapper;
@@ -20,26 +21,33 @@ class ModuleServicesActivationService implements ModuleServicesActivationService
/**
* @var ProjectYamlDaoInterface $dao
*/
public $dao;
private $dao;
/**
* ShopAwareServiceActivationService constructor.
*
* @var ShopAdapterInterface
*/
private $shopAdapter;
/**
* ModuleServicesActivationService constructor.
* @param ProjectYamlDaoInterface $dao
* @param ShopAdapterInterface $shopAdapter
*/
public function __construct(ProjectYamlDaoInterface $dao)
public function __construct(ProjectYamlDaoInterface $dao, ShopAdapterInterface $shopAdapter)
{
$this->dao = $dao;
$this->shopAdapter = $shopAdapter;
}
/**
* @param string $moduleDir
* @param array $shopIds
* @param string $moduleId
* @param int $shopId
* @return void
* @throws \OxidEsales\EshopCommunity\Internal\Application\Exception\MissingServiceException
*/
public function activateServicesForShops(string $moduleDir, array $shopIds)
public function activateModuleServices(string $moduleId, int $shopId)
{
$moduleConfigFile = $moduleDir . DIRECTORY_SEPARATOR . 'services.yaml';
$moduleConfigFile = $this->getModuleServicesFilePath($moduleId);
try {
$moduleConfig = $this->getModuleConfig($moduleConfigFile);
} catch (NoServiceYamlException $e) {
@@ -57,7 +65,7 @@ public function activateServicesForShops(string $moduleDir, array $shopIds)
if ($projectConfig->hasService($service->getKey())) {
$service = $projectConfig->getService($service->getKey());
}
$service->addActiveShops($shopIds);
$service->addActiveShops([$shopId]);
$projectConfig->addOrUpdateService($service);
}
@@ -66,13 +74,14 @@ public function activateServicesForShops(string $moduleDir, array $shopIds)
/**
* @param string $moduleDir
* @param array $shopIds
* @param string $moduleId
* @param int $shopId
* @return void
* @throws \OxidEsales\EshopCommunity\Internal\Application\Exception\MissingServiceException
*/
public function deactivateServicesForShops(string $moduleDir, array $shopIds)
public function deactivateModuleServices(string $moduleId, int $shopId)
{
$moduleConfigFile = $moduleDir . DIRECTORY_SEPARATOR . 'services.yaml';
$moduleConfigFile = $this->getModuleServicesFilePath($moduleId);
try {
$moduleConfig = $this->getModuleConfig($moduleConfigFile);
} catch (NoServiceYamlException $e) {
@@ -86,7 +95,7 @@ public function deactivateServicesForShops(string $moduleDir, array $shopIds)
continue;
}
$service = $projectConfig->getService($service->getKey());
$service->removeActiveShops($shopIds);
$service->removeActiveShops([$shopId]);
$projectConfig->addOrUpdateService($service);
}
$this->dao->saveProjectConfigFile($projectConfig);
@@ -105,4 +114,13 @@ private function getModuleConfig(string $moduleConfigFile): DIConfigWrapper
}
return $this->dao->loadDIConfigFile($moduleConfigFile);
}
/**
* @param string $moduleId
* @return string
*/
private function getModuleServicesFilePath(string $moduleId): string
{
return $this->shopAdapter->getModuleFullPath($moduleId) . DIRECTORY_SEPARATOR . 'services.yaml';
}
}
@@ -11,20 +11,17 @@
*/
interface ModuleServicesActivationServiceInterface
{
/**
* @param string $moduleDir
* @param array $shopIds
*
* @param string $moduleId
* @param int $shopId
* @return void
*/
public function activateServicesForShops(string $moduleDir, array $shopIds);
public function activateModuleServices(string $moduleId, int $shopId);
/**
* @param string $moduleDir
* @param array $shopIds
*
* @param string $moduleId
* @param int $shopId
* @return void
*/
public function deactivateServicesForShops(string $moduleDir, array $shopIds);
public function deactivateModuleServices(string $moduleId, int $shopId);
}
@@ -6,6 +6,7 @@
namespace OxidEsales\EshopCommunity\Tests\Unit\Internal\Module\Setup\Service;
use OxidEsales\EshopCommunity\Internal\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Application\Dao\ProjectYamlDaoInterface;
use OxidEsales\EshopCommunity\Internal\Application\DataObject\DIConfigWrapper;
use OxidEsales\EshopCommunity\Internal\Application\DataObject\DIServiceWrapper;
@@ -18,6 +19,8 @@
class ModuleServicesActivationServiceTest extends TestCase
{
private $testModuleId = 'testModuleId';
private $testModuleDirectory = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestModule';
/**
@@ -39,7 +42,12 @@ public function setUp()
->method('saveProjectConfigFile')
->willReturnCallback([$this, 'saveProjectYaml']);
$this->shopActivationService = new ModuleServicesActivationService($this->projectYamlDao);
$shopAdapter = $this->getMockBuilder(ShopAdapterInterface::class)->getMock();
$shopAdapter
->method('getModuleFullPath')
->willReturn($this->testModuleDirectory);
$this->shopActivationService = new ModuleServicesActivationService($this->projectYamlDao, $shopAdapter);
}
public function saveProjectYaml(DIConfigWrapper $config)
@@ -61,7 +69,9 @@ public function testActivateServicesForShops()
$this->projectYamlDao->method('loadProjectConfigFile')->willReturn($projectConfig);
$this->projectYamlDao->method('loadDIConfigFile')->willReturn($moduleConfig);
$this->shopActivationService->activateServicesForShops($this->testModuleDirectory, [1, 4, 5]);
$this->shopActivationService->activateModuleServices($this->testModuleId, 1);
$this->shopActivationService->activateModuleServices($this->testModuleId, 4);
$this->shopActivationService->activateModuleServices($this->testModuleId, 5);
$this->assertProjectYamlHasImport($this->getTestModuleServiceYamlPath());
$this->assertModuleServiceIsActiveForShops('testEventSubscriber', [1,4,5]);
@@ -102,7 +112,7 @@ public function testDeactivateServicesForShops()
$this->projectYamlDao->method('loadProjectConfigFile')->willReturn($projectConfig);
$this->projectYamlDao->method('loadDIConfigFile')->willReturn($moduleConfig);
$this->shopActivationService->deactivateServicesForShops($this->testModuleDirectory, [1]);
$this->shopActivationService->deactivateModuleServices($this->testModuleId, 1);
$this->assertProjectYamlHasImport($this->getTestModuleServiceYamlPath());
$this->assertModuleServiceIsActiveForShops('testEventSubscriber', [5]);
@@ -111,7 +121,6 @@ public function testDeactivateServicesForShops()
public function testDeactivateServicesForAllShops()
{
$shopAwareService = TestEventSubscriber::class;
$otherService = OtherService::class;
$projectConfig = new DIConfigWrapper([
'imports' => [
@@ -137,14 +146,15 @@ public function testDeactivateServicesForAllShops()
$moduleConfig = new DIConfigWrapper([
'services' => [
'testEventSubscriber' => ['class' => $shopAwareService],
'otherService' => ['class' => $otherService],
'otherService' => ['class' => OtherService::class],
]
]);
$this->projectYamlDao->method('loadProjectConfigFile')->willReturn($projectConfig);
$this->projectYamlDao->method('loadDIConfigFile')->willReturn($moduleConfig);
$this->shopActivationService->deactivateServicesForShops($this->testModuleDirectory, [5,1]);
$this->shopActivationService->deactivateModuleServices($this->testModuleId, 1);
$this->shopActivationService->deactivateModuleServices($this->testModuleId, 5);
$this->assertArrayHasKey('imports', $this->projectYamlArray);
$this->assertArrayNotHasKey('services', $this->projectYamlArray);

0 comments on commit 671c096

Please sign in to comment.