Skip to content

Commit

Permalink
MSI: Task 290 import export source code
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Dec 26, 2017
1 parent 5e93ef5 commit 580e823
Show file tree
Hide file tree
Showing 83 changed files with 582 additions and 572 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
*/
public function execute(): ResultInterface
{
$sourceCode = $this->getRequest()->getParam(SourceInterface::CODE);
$sourceCode = $this->getRequest()->getParam(SourceInterface::SOURCE_CODE);
try {
$source = $this->sourceRepository->get($sourceCode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function execute(): ResultInterface
if ($request->isXmlHttpRequest() && $request->isPost() && $requestData) {
foreach ($requestData as $itemData) {
try {
$sourceCode = $itemData[SourceInterface::CODE];
$sourceCode = $itemData[SourceInterface::SOURCE_CODE];
$source = $this->sourceRepository->get($sourceCode);
$this->dataObjectHelper->populateWithArray($source, $itemData, SourceInterface::class);
$this->sourceRepository->save($source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public function process(int $stockId, array $stockSourceLinksData)
$sourceCodesForDelete = [];

foreach ($assignedSources as $assignedSource) {
if (array_key_exists($assignedSource->getCode(), $sourceCodesForSave)) {
unset($sourceCodesForSave[$assignedSource->getCode()]);
if (array_key_exists($assignedSource->getSourceCode(), $sourceCodesForSave)) {
unset($sourceCodesForSave[$assignedSource->getSourceCode()]);
} else {
$sourceCodesForDelete[] = $assignedSource->getCode();
$sourceCodesForDelete[] = $assignedSource->getSourceCode();
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Inventory/Indexer/SelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function execute($stockId): Select

// find all enabled sources
$select = $connection->select()
->from($sourceTable, [SourceInterface::CODE])
->from($sourceTable, [SourceInterface::SOURCE_CODE])
->where(SourceInterface::ENABLED . ' = ?', 1);
$sourceCodes = $connection->fetchCol($select);

Expand Down
26 changes: 4 additions & 22 deletions app/code/Magento/Inventory/Indexer/Source/GetAssignedStockIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
namespace Magento\Inventory\Indexer\Source;

use Magento\Framework\App\ResourceConnection;
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
use Magento\Inventory\Model\ResourceModel\StockSourceLink as StockSourceLinkResourceModel;
use Magento\Inventory\Model\StockSourceLink;
use Magento\Inventory\Model\ResourceModel\Source;
use Magento\InventoryApi\Api\Data\SourceInterface;

/**
* Returns assigned Stock ids by given Source ids
Expand All @@ -34,35 +31,20 @@ public function __construct(
}

/**
* @param string[] $sourceIds
* @param string[] $sourceCodes
* @return int[]
*/
public function execute(array $sourceIds): array
public function execute(array $sourceCodes): array
{
$connection = $this->resourceConnection->getConnection();
$sourceStockLinkTable = $this->resourceConnection->getTableName(
StockSourceLinkResourceModel::TABLE_NAME_STOCK_SOURCE_LINK
);
$sourceTable = $this->resourceConnection->getTableName(
Source::TABLE_NAME_SOURCE
);

$select = $connection
->select()
->from(
['sourceStockLink' => $sourceStockLinkTable],
StockSourceLink::STOCK_ID
)
->joinInner(
['source' => $sourceTable],
sprintf(
'sourceStockLink.%s = source.%s',
StockSourceLink::SOURCE_CODE,
SourceInterface::CODE
),
[]
)
->where(SourceResourceModel::SOURCE_ID_FIELD . ' IN (?)', $sourceIds)
->from($sourceStockLinkTable, StockSourceLink::STOCK_ID)
->where(StockSourceLink::SOURCE_CODE . ' IN (?)', $sourceCodes)
->group(StockSourceLink::STOCK_ID);

$stockIds = $connection->fetchCol($select);
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/Inventory/Indexer/Source/SourceIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public function executeFull()
/**
* @inheritdoc
*/
public function executeRow($sourceId)
public function executeRow($sourceCode)
{
$this->executeList([$sourceId]);
$this->executeList([$sourceCode]);
}

/**
* @inheritdoc
*/
public function executeList(array $sourceIds)
public function executeList(array $sourceCodes)
{
$stockIds = $this->getAssignedStockIds->execute($sourceIds);
$stockIds = $this->getAssignedStockIds->execute($sourceCodes);
$this->stockIndexer->executeList($stockIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Magento\Framework\MultiDimensionalIndex\IndexHandlerInterface;
use Magento\Framework\MultiDimensionalIndex\IndexNameBuilder;
use Magento\Framework\MultiDimensionalIndex\IndexStructureInterface;
use Magento\Inventory\Indexer\IndexDataProvider;
use Magento\Inventory\Indexer\Stock\StockIndexer;

/**
Expand Down
10 changes: 8 additions & 2 deletions app/code/Magento/Inventory/Model/ResourceModel/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ class Source extends AbstractDb
*/
const TABLE_NAME_SOURCE = 'inventory_source';
/**#@-*/
const SOURCE_ID_FIELD = 'source_id';

/**
* Primary key auto increment flag
*
* @var bool
*/
protected $_isPkAutoIncrement = false;

/**
* @var SourceCarrierLinkManagementInterface
Expand All @@ -55,7 +61,7 @@ public function __construct(
*/
protected function _construct()
{
$this->_init(self::TABLE_NAME_SOURCE, SourceInterface::CODE);
$this->_init(self::TABLE_NAME_SOURCE, SourceInterface::SOURCE_CODE);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/Inventory/Model/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ protected function _construct()
/**
* @inheritdoc
*/
public function getCode()
public function getSourceCode()
{
return $this->getData(self::CODE);
return $this->getData(self::SOURCE_CODE);
}

/**
* @inheritdoc
*/
public function setCode($code)
public function setSourceCode($sourceCode)
{
return $this->setData(self::CODE, $code);
$this->setData(self::SOURCE_CODE, $sourceCode);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/Inventory/Model/Source/Command/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(string $code): SourceInterface
public function execute(string $sourceCode): SourceInterface
{
/** @var SourceInterface $source */
$source = $this->sourceFactory->create();
$this->sourceResource->load($source, $code, SourceInterface::CODE);
$this->sourceResource->load($source, $sourceCode, SourceInterface::SOURCE_CODE);

if (null === $source->getCode()) {
throw new NoSuchEntityException(__('Source with code "%value" does not exist.', ['value' => $code]));
if (null === $source->getSourceCode()) {
throw new NoSuchEntityException(__('Source with code "%value" does not exist.', ['value' => $sourceCode]));
}
return $source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ interface GetInterface
/**
* Get Source data by given code
*
* @param string $code
* @param string $sourceCode
* @return SourceInterface
* @throws NoSuchEntityException
*/
public function execute(string $code): SourceInterface;
public function execute(string $sourceCode): SourceInterface;
}
3 changes: 2 additions & 1 deletion app/code/Magento/Inventory/Model/Source/Command/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function execute(SourceInterface $source)
}

try {
return $this->sourceResource->save($source);
$this->sourceResource->save($source);
$source->getSourceCode();

This comment has been minimized.

Copy link
@snowcore

snowcore May 7, 2019

@naydav seems like this call is not needed. Can we remove this line?

} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new CouldNotSaveException(__('Could not save Source'), $e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ public function __construct(ValidationResultFactory $validationResultFactory)
*/
public function validate(SourceInterface $source): ValidationResult
{
$value = (string)$source->getCode();
$value = (string)$source->getSourceCode();

if ('' === trim($value)) {
$errors[] = __('"%field" can not be empty.', ['field' => SourceInterface::CODE]);
$errors[] = __('"%field" can not be empty.', ['field' => SourceInterface::SOURCE_CODE]);
} elseif (preg_match('/\s/', $value)) {
$errors[] = __('"%field" can not contain whitespaces.', ['field' => SourceInterface::CODE]);
$errors[] = __('"%field" can not contain whitespaces.', ['field' => SourceInterface::SOURCE_CODE]);
} else {
$errors = [];
}

return $this->validationResultFactory->create(['errors' => $errors]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(ValidationResultFactory $validationResultFactory)
*/
public function validate(SourceInterface $source): ValidationResult
{
$value = $source->getCountryId();
$value = (string)$source->getCountryId();

if ('' === trim($value)) {
$errors[] = __('"%field" can not be empty.', ['field' => SourceInterface::COUNTRY_ID]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function deleteCurrentCarrierLinks(SourceInterface $source)
$connection = $this->resourceConnection->getConnection();
$connection->delete(
$this->resourceConnection->getTableName(SourceCarrierLink::TABLE_NAME_SOURCE_CARRIER_LINK),
$connection->quoteInto('source_code = ?', $source->getCode())
$connection->quoteInto('source_code = ?', $source->getSourceCode())
);
}

Expand All @@ -103,7 +103,7 @@ private function saveNewCarrierLinks(SourceInterface $source)
$carrierLinkData = [];
foreach ($source->getCarrierLinks() as $carrierLink) {
$carrierLinkData[] = [
'source_code' => $source->getCode(),
'source_code' => $source->getSourceCode(),
SourceCarrierLinkInterface::CARRIER_CODE => $carrierLink->getCarrierCode(),
SourceCarrierLinkInterface::POSITION => $carrierLink->getPosition(),
];
Expand All @@ -121,7 +121,7 @@ private function saveNewCarrierLinks(SourceInterface $source)
public function loadCarrierLinksBySource(SourceInterface $source)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(SourceCarrierLinkInterface::SOURCE_CODE, $source->getCode())
->addFilter(SourceCarrierLinkInterface::SOURCE_CODE, $source->getSourceCode())
->create();

/** @var Collection $collection */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Magento\InventoryApi\Api\Data\SourceItemInterface;

/**
* Check that source id is valid
* Check that source code is valid
*/
class SourceCodeValidator implements SourceItemValidatorInterface
{
Expand All @@ -34,14 +34,15 @@ public function __construct(ValidationResultFactory $validationResultFactory)
*/
public function validate(SourceItemInterface $source): ValidationResult
{
$errors = [];
if (!is_string($source->getSourceCode())) {
$errors[] = __(
'"%field" should be string.',
['field' => SourceItemInterface::SOURCE_CODE]
);
}
$value = (string)$source->getSourceCode();

if ('' === trim($value)) {
$errors[] = __('"%field" can not be empty.', ['field' => SourceItemInterface::SOURCE_CODE]);
} elseif (preg_match('/\s/', $value)) {
$errors[] = __('"%field" can not contain whitespaces.', ['field' => SourceItemInterface::SOURCE_CODE]);
} else {
$errors = [];
}
return $this->validationResultFactory->create(['errors' => $errors]);
}
}
6 changes: 3 additions & 3 deletions app/code/Magento/Inventory/Model/SourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public function __construct(
*/
public function save(SourceInterface $source)
{
return $this->commandSave->execute($source);
$this->commandSave->execute($source);
}

/**
* @inheritdoc
*/
public function get(string $code): SourceInterface
public function get(string $sourceCode): SourceInterface
{
return $this->commandGet->execute($code);
return $this->commandGet->execute($sourceCode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function execute(int $stockId): array
$sourceCodes = $this->getAssignedSourceCodes($stockId);

$searchCriteria = $this->searchCriteriaBuilder
->addFilter(SourceInterface::CODE, $sourceCodes, 'in')
->addFilter(SourceInterface::SOURCE_CODE, $sourceCodes, 'in')
->create();
$searchResult = $this->sourceRepository->getList($searchCriteria);
return $searchResult->getItems();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ private function createSourceCarrierLinkTable(SchemaSetupInterface $setup): Tabl
$sourceCarrierLinkTable,
SourceCarrierLinkInterface::SOURCE_CODE,
$sourceTable,
SourceInterface::CODE
SourceInterface::SOURCE_CODE
),
SourceCarrierLinkInterface::SOURCE_CODE,
$sourceTable,
SourceInterface::CODE,
SourceInterface::SOURCE_CODE,
AdapterInterface::FK_ACTION_CASCADE
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ private function createSourceItemTable(SchemaSetupInterface $setup): Table
$sourceItemTable,
SourceItemInterface::SOURCE_CODE,
$sourceTable,
SourceInterface::CODE
SourceInterface::SOURCE_CODE
),
SourceItemInterface::SOURCE_CODE,
$sourceTable,
SourceInterface::CODE,
SourceInterface::SOURCE_CODE,
AdapterInterface::FK_ACTION_CASCADE
)->addIndex(
$setup->getIdxName(
Expand Down
Loading

0 comments on commit 580e823

Please sign in to comment.