Skip to content

Commit

Permalink
Rework create or update
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Aug 7, 2024
1 parent 12914e2 commit be3c29d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
8 changes: 7 additions & 1 deletion Observer/SequenceCreatorObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Api\Data\StoreInterface;
use Opengento\SalesSequence\Service\SequenceManagement;

Expand All @@ -18,13 +20,17 @@ class SequenceCreatorObserver implements ObserverInterface
public function __construct(private SequenceManagement $sequenceManagement) {}

/**
* @param EventObserver $observer
* @return void
* @throws AlreadyExistsException
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function execute(EventObserver $observer): void
{
$store = $observer->getData('store');
if ($store instanceof StoreInterface) {
$this->sequenceManagement->create((int)$store->getId());
$this->sequenceManagement->createOrUpdate((int)$store->getId());
}
}
}
2 changes: 1 addition & 1 deletion Observer/SequenceUpdaterObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function execute(EventObserver $observer): void
{
$storeCode = (string)$observer->getData('store');
if ($storeCode !== '') {
$this->sequenceManagement->update((int)$this->storeManager->getStore($storeCode)->getId());
$this->sequenceManagement->createOrUpdate((int)$this->storeManager->getStore($storeCode)->getId());
}
}
}
53 changes: 27 additions & 26 deletions Service/SequenceManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\SalesSequence\Model\Builder;
use Magento\SalesSequence\Model\EntityPool;
use Magento\SalesSequence\Model\Meta;
use Magento\SalesSequence\Model\ResourceModel\Meta as MetaResource;
use Opengento\SalesSequence\Model\Config;

Expand All @@ -26,47 +26,48 @@ public function __construct(

/**
* @throws AlreadyExistsException
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function create(int $storeId): void
public function createOrUpdate(int $storeId): void
{
foreach ($this->entityPool->getEntities() as $entityType) {
$this->sequenceBuilder
->setEntityType($entityType)
->setStoreId($storeId)
->setPrefix($this->config->getPrefix($entityType, $storeId))
->setSuffix($this->config->getSuffix($entityType, $storeId))
->setStartValue($this->config->getStartValue($entityType, $storeId))
->setStep($this->config->getStep($entityType, $storeId))
->setWarningValue($this->config->getWarningValue($entityType, $storeId))
->setMaxValue($this->config->getMaxValue($entityType, $storeId))
->create();
// Create is not processed if the sequence table already exists.
// It is invoked only to ensure that the table exists.
$this->create($entityType, $storeId);
$this->update($entityType, $storeId);
}
}

/**
* @throws AlreadyExistsException
* @throws LocalizedException
*/
public function update(int $storeId): void
private function create(string $entityType, int $storeId): void
{
foreach ($this->entityPool->getEntities() as $entityType) {
$meta = $this->metaResource->loadByEntityTypeAndStore($entityType, $storeId);
if ($meta->getEntityId()) {
$this->updateMeta($meta);
} else {
// Prevent case where sequence was deleted
$this->create($storeId);
}
}
$this->sequenceBuilder
->setEntityType($entityType)
->setStoreId($storeId)
->setPrefix($this->config->getPrefix($entityType, $storeId))
->setSuffix($this->config->getSuffix($entityType, $storeId))
->setStartValue($this->config->getStartValue($entityType, $storeId))
->setStep($this->config->getStep($entityType, $storeId))
->setWarningValue($this->config->getWarningValue($entityType, $storeId))
->setMaxValue($this->config->getMaxValue($entityType, $storeId))
->create();
}

/**
* @throws AlreadyExistsException
* @throws NoSuchEntityException
* @throws LocalizedException
*/
private function updateMeta(Meta $meta): void
private function update(string $entityType, int $storeId): void
{
$entityType = $meta->getData('entity_type');
$storeId = $meta->getData('store_id');
$meta = $this->metaResource->loadByEntityTypeAndStore($entityType, $storeId);
if (!$meta->getEntityId()) {
throw NoSuchEntityException::doubleField('entity_type', $entityType, 'store_id', $storeId);
}

$meta->addData([
'prefix' => $this->config->getPrefix($entityType, $storeId),
'suffix' => $this->config->getSuffix($entityType, $storeId),
Expand Down
6 changes: 5 additions & 1 deletion Setup/RecurringData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Opengento\SalesSequence\Setup;

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
Expand All @@ -23,11 +25,13 @@ public function __construct(

/**
* @throws AlreadyExistsException
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context): void
{
foreach ($this->storeRepository->getList() as $store) {
$this->sequenceManagement->create((int)$store->getId());
$this->sequenceManagement->createOrUpdate((int)$store->getId());
}
}
}

0 comments on commit be3c29d

Please sign in to comment.