Skip to content

Commit

Permalink
Fix #14958 - remove sales sequence data on store view delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartlomiejsz committed Apr 12, 2019
1 parent 07e57ac commit 8529467
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
44 changes: 43 additions & 1 deletion app/code/Magento/SalesSequence/Model/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
*/
namespace Magento\SalesSequence\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection as AppResource;
use Magento\Framework\DB\Ddl\Sequence as DdlSequence;
use Magento\Framework\Webapi\Exception;
use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMetadata;
use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile;
use Psr\Log\LoggerInterface as Logger;

/**
Expand Down Expand Up @@ -83,28 +85,36 @@ class Builder
*/
protected $logger;

/**
* @var ResourceProfile
*/
private $resourceProfile;

/**
* @param ResourceMetadata $resourceMetadata
* @param MetaFactory $metaFactory
* @param ProfileFactory $profileFactory
* @param AppResource $appResource
* @param DdlSequence $ddlSequence
* @param Logger $logger
* @param ResourceProfile|null $resourceProfile
*/
public function __construct(
ResourceMetadata $resourceMetadata,
MetaFactory $metaFactory,
ProfileFactory $profileFactory,
AppResource $appResource,
DdlSequence $ddlSequence,
Logger $logger
Logger $logger,
ResourceProfile $resourceProfile = null
) {
$this->resourceMetadata = $resourceMetadata;
$this->metaFactory = $metaFactory;
$this->profileFactory = $profileFactory;
$this->appResource = $appResource;
$this->ddlSequence = $ddlSequence;
$this->logger = $logger;
$this->resourceProfile = $resourceProfile ?: ObjectManager::getInstance()->get(ResourceProfile::class);
$this->data = array_flip($this->pattern);
}

Expand Down Expand Up @@ -264,4 +274,36 @@ public function create()
}
$this->data = array_flip($this->pattern);
}

/**
* Deletes all sequence linked entites
*
* @param $storeId
*
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function deleteByStoreId($storeId)
{
$metadataIds = $this->resourceMetadata->getIdsByStore($storeId);
$profileIds = $this->resourceProfile->getProfileIdsByMetadataIds($metadataIds);

$this->appResource->getConnection()->delete(
$this->appResource->getTableName('sales_sequence_profile'),
['profile_id IN (?)' => $profileIds]
);

foreach ($metadataIds as $metadataId) {
$metadata = $this->metaFactory->create();
$this->resourceMetadata->load($metadata, $metadataId);
if (!$metadata->getId()) {
continue;
}

$this->appResource->getConnection()->dropTable(
$metadata->getSequenceTable()
);
$this->resourceMetadata->delete($metadata);
}
}
}
21 changes: 21 additions & 0 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ public function loadByEntityTypeAndStore($entityType, $storeId)
return $meta;
}

/**
* Retrieves Metadata Ids by store id
*
* @param int $storeId
* @return int[]
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getIdsByStore($storeId)
{
$connection = $this->getConnection();
$bind = ['store_id' => $storeId];
$select = $connection->select()->from(
$this->getMainTable(),
[$this->getIdFieldName()]
)->where(
'store_id = :store_id'
);

return $connection->fetchCol($select, $bind);
}

/**
* Using for load sequence profile and setting it into metadata
*
Expand Down
16 changes: 16 additions & 0 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,20 @@ public function loadActiveProfile($metadataId)
}
return $profile;
}

/**
* Get profile ids by metadata ids
* @param int[] $metadataIds
* @return int[]
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getProfileIdsByMetadataIds(array $metadataIds)
{
$connection = $this->getConnection();
$select = $connection->select()
->from($this->getMainTable(), ['profile_id'])
->where('meta_id IN (?)', $metadataIds);

return $connection->fetchCol($select);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\SalesSequence\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\SalesSequence\Model\Builder;

/**
* Class SequenceRemovalObserver
*/
class SequenceRemovalObserver implements ObserverInterface
{
/**
* @var Builder
*/
private $sequenceBuilder;

/**
* @param Builder $sequenceBuilder
*/
public function __construct(
Builder $sequenceBuilder
) {
$this->sequenceBuilder = $sequenceBuilder;
}

/**
* @param EventObserver $observer
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(EventObserver $observer)
{
$storeId = $observer->getData('store')->getId();
$this->sequenceBuilder->deleteByStoreId($storeId);

return $this;
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/SalesSequence/etc/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="store_delete">
<observer name="magento_sequence" instance="Magento\SalesSequence\Observer\SequenceRemovalObserver" />
</event>
</config>

0 comments on commit 8529467

Please sign in to comment.