Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Console/Command/ImportCmsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ImportCmsData extends \Symfony\Component\Console\Command\Command
private const INPUT_TYPE_VALUES = ['block', 'page', 'all'];
private const INPUT_KEY_IDENTIFIER = 'identifier';
private const INPUT_KEY_IMPORT_ALL = 'importAll';
private const INPUT_KEY_STORE = 'store';
private \RocketWeb\CmsImportExport\Model\Service\ImportCmsDataService $importCmsDataService;

public function __construct(
Expand Down Expand Up @@ -60,6 +61,12 @@ protected function configure()
'a',
InputOption::VALUE_NONE,
'Flag to import all files'
),
new InputOption(
self::INPUT_KEY_STORE,
's',
InputOption::VALUE_OPTIONAL,
'Specific Store Code'
)
]);
parent::configure();
Expand Down Expand Up @@ -88,7 +95,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$identifiers = explode(',', $identifiers);
}

$this->importCmsDataService->execute($types, $identifiers, $importAll);
$storeCode = empty($input->getOption(self::INPUT_KEY_STORE)) ?
null :
$input->getOption(self::INPUT_KEY_STORE);

$this->importCmsDataService->execute($types, $identifiers, $importAll, $storeCode);

return 0;
}
Expand Down
82 changes: 66 additions & 16 deletions Model/Service/ImportCmsDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public function __construct(
\Magento\Framework\Filesystem\DirectoryList $directoryList,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Serialize\SerializerInterface $serializer,
\Magento\Store\Api\StoreRepositoryInterface $storeRepository
\Magento\Store\Api\StoreRepositoryInterface $storeRepository,
private readonly \Magento\Cms\Api\GetBlockByIdentifierInterface $getBlockByIdentifier,
private readonly \Magento\Cms\Api\GetPageByIdentifierInterface $getPageByIdentifier
) {
$this->pageRepository = $pageRepository;
$this->blockRepository = $blockRepository;
Expand All @@ -51,7 +53,7 @@ public function __construct(
$this->storeRepository = $storeRepository;
}

public function execute(array $types, ?array $identifiers, bool $importAll)
public function execute(array $types, ?array $identifiers, bool $importAll, ?string $storeCode)
{
$workingDirPath = 'sync_cms_data';

Expand All @@ -70,9 +72,9 @@ public function execute(array $types, ?array $identifiers, bool $importAll)
}

if ($type == 'block') {
$this->importBlocks($typeDirPath, $identifiers);
$this->importBlocks($typeDirPath, $identifiers, $storeCode);
} else if ($type == 'page') {
$this->importPages($typeDirPath, $identifiers);
$this->importPages($typeDirPath, $identifiers, $storeCode);
}
}
}
Expand All @@ -97,7 +99,7 @@ private function getStoreIds($storeCodes): array
return $storeIds;
}

private function importBlocks(string $dirPath, ?array $identifiers): void
private function importBlocks(string $dirPath, ?array $identifiers, ?string $storeCode): void
{
$filePaths = $this->directoryRead->read($this->varPath . $dirPath);
foreach ($filePaths as $filePath) {
Expand All @@ -112,12 +114,11 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
// If we have a list of items, we skip if its not in the list
continue;
}

try {
$block = $this->blockRepository->getById($identifier);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$block = $this->blockFactory->create();
if ($storeCode !== null && ($this->getStoreCode($filePath) !== $storeCode)) {
// Skip identifiers not assigned to specific store when storeCode parameter is set
continue;
}

$content = $this->directoryRead->readFile($filePath);
$jsonData = $this->directoryRead->readFile(str_replace('.html', '.json', $filePath));
$jsonData = $this->serializer->unserialize($jsonData);
Expand All @@ -128,6 +129,13 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
'is_active' => $block->isActive()
];*/
$storeIds = $this->getStoreIds($jsonData['stores']);
try {
$block = $this->getBlockByIdentifier->execute($identifier, (int)reset($storeIds));
$this->validateStoreAssociation($filePath, $block, $storeIds, 'Block');
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$block = $this->blockFactory->create();
}

$block->setTitle($jsonData['title']);
$block->setContent($content);
$block->setIdentifier($jsonData['identifier']);
Expand All @@ -145,7 +153,7 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
}
}

private function importPages(string $dirPath, ?array $identifiers): void
private function importPages(string $dirPath, ?array $identifiers, ?string $storeCode): void
{
$filePaths = $this->directoryRead->read($this->varPath . $dirPath);
foreach ($filePaths as $filePath) {
Expand All @@ -155,23 +163,29 @@ private function importPages(string $dirPath, ?array $identifiers): void
}
$identifier = str_replace($dirPath, '', $filePath);
$identifier = str_replace('.html', '', $identifier);
$identifier = substr_replace($identifier, '', strpos($identifier, '---'));
$identifier = substr_replace($identifier, '', strrpos($identifier, '---'));
$identifier = str_replace('---', '/', $identifier);
$identifier = str_replace('_html', '.html', $identifier);
if ($identifiers !== null && !in_array($identifier, $identifiers)) {
// If we have a list of items, we skip if its not in the list
continue;
}

try {
$page = $this->pageRepository->getById($identifier);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$page = $this->pageFactory->create();
if ($storeCode !== null && ($this->getStoreCode($filePath) !== $storeCode)) {
// Skip identifiers not assigned to specific store when storeCode parameter is set
continue;
}

$content = $this->directoryRead->readFile($filePath);
$jsonData = $this->directoryRead->readFile(str_replace('.html', '.json', $filePath));
$jsonData = $this->serializer->unserialize($jsonData);
$storeIds = $this->getStoreIds($jsonData['stores']);
try {
$page = $this->getPageByIdentifier->execute($identifier, (int)reset($storeIds));
$this->validateStoreAssociation($filePath, $page, $storeIds, 'Page');
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$page = $this->pageFactory->create();
}
/*$jsonContent = [
'title' => $page->getTitle(),
'is_active' => $page->isActive(),
Expand All @@ -197,4 +211,40 @@ private function importPages(string $dirPath, ?array $identifiers): void
}
}
}

private function validateStoreAssociation(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docblocks, thanks! also add a small readme of how this should work from top to bottom using the store parameter, like how to export and how to import. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if i was confusing about the README, i actually meant, create a small docblock comment but add a .README file to the module which explains how the store parameter works (not in the docblocks :P )

string $filePath,
mixed $entity,
array $storeIds,
string $entityType
) : void {
$exceptionMessage = sprintf('%s with path %s has incosistent store data', $entityType, $filePath);
if (count($storeIds) > 1) {
throw new \LogicException($exceptionMessage);
}
$storeCode = $this->getStoreCode($filePath);
$storeId = (int)reset($storeIds);
$currentStoreIds = $entity->getStoreId();
if ($storeCode === '_all_') {
if ($storeId !== 0 || count($currentStoreIds) > 1 || (int)reset($currentStoreIds) !== 0) {
throw new \LogicException($exceptionMessage);
}
return ;
}
$store = $this->storeRepository->get($storeId);
if ($store->getCode() !== $storeCode) {
throw new \LogicException($exceptionMessage);
}

if (array_diff($currentStoreIds, $storeIds) !== []) {
throw new \LogicException($exceptionMessage);
}
}

private function getStoreCode(string $filePath) : string
{
$storeCode = str_replace('.html', '', $filePath);
$storeCode = substr($storeCode, strrpos($storeCode, '---') + 3);
return $storeCode;
}
}
Loading