Skip to content

Add possibility To Delete Stocks in Admin Panel #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 27, 2017
Merged

Conversation

bartekszymanski
Copy link
Contributor

Description

Fixed Issues (if relevant)

  1. magento/magento2#<issue_number>: Issue title
  2. ...

Manual testing scenarios

  1. ...
  2. ...

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

{
$resultRedirect = $this->resultRedirectFactory->create();
$stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID);
if ($stockId !== null) {
Copy link
Member

@larsroettig larsroettig Sep 17, 2017

Choose a reason for hiding this comment

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

if ($stockId=== null) {
$this->messageManager->addErrorMessage(__('Wrong request.'));
$resultRedirect->setPath('*/*');
return $resultRedirect;

it is shorter and better maintainable @see https://phpmd.org/rules/cleancode.html#elseexpression

*/
public function execute()
{
if ($this->getRequest()->isPost()) {
Copy link
Member

Choose a reason for hiding this comment

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

You can also simplify with

if ($this->getRequest()->isPost() !== true) {
    $this->messageManager->addErrorMessage(__('Wrong request.'));
    return $this->resultRedirectFactory->create()->setPath('*/*');
}

Then you don't nee the else  @see https://phpmd.org/rules/cleancode.html#elseexpression 

@bartekszymanski
Copy link
Contributor Author

app/code/Magento/Inventory/Controller/Adminhtml/Stock/Delete.php and
app/code/Magento/Inventory/Controller/Adminhtml/Stock/MassDelete.php refactoring according to @larsroettig comments

private $stockRepository;

/**
* @param Context $context
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, don't use such alignments
just make a space after type before the variable declaration

public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID);
Copy link
Contributor

Choose a reason for hiding this comment

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

don't try to make the fancy alignment.
It's hard to read and in any case it would be broken along the code evolution

@@ -49,7 +58,7 @@ public function execute()
$stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID);
try {
$stock = $this->stockRepository->get($stockId);

$this->registry->register('stock',$stock);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's preferable to use explicit Extension Points than Registry.
Because usage of Registry is forbidden for newly created code
as it introduces mutable global state

/**
* @param Context $context
* @param StockRepositoryInterface $stockRepository
* @param Filter $massActionFilter
Copy link
Contributor

Choose a reason for hiding this comment

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

please fix the formatting

/**
* @return array
*/
public function getButtonData()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please provide a description for all public methods

use Magento\Ui\Component\MassAction\Filter as BaseFilter;

/**
* @author naydav <[email protected]>
Copy link
Contributor

Choose a reason for hiding this comment

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

please fix the author PHP Doc Block

$component = $this->filter->getComponent();
$dataProvider = $component->getContext()->getDataProvider();
$searchResult = $dataProvider->getSearchResult();
$ids = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

please fix formatting

$ids = [];
foreach ($searchResult->getItems() as $item) {
$ids[] = $item->getId();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer to use next syntax :

array_map(function(\Magento\Framework\Api\Search\DocumentInterface $item) {
    return $item->getId();
}, $searchResult->getItems());

Doing that way you can ensure the type validation of the array returned

<item name="url" xsi:type="url" path="inventory/stock/massDelete"/>
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Delete items</item>
<item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
Copy link
Contributor

Choose a reason for hiding this comment

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

wan't -> want


/**
* @author naydav <[email protected]>
* @todo remove this class after code from Engine\MagentoFix\Ui\Component\MassAction\Filter was deploy to production
Copy link
Contributor

Choose a reason for hiding this comment

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

was -> would be

/**
* @param Context $context
*/
public function __construct(Context $context)
Copy link
Contributor

Choose a reason for hiding this comment

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

dependency on Context is not recommended. As Context is a container for other objects.

Use dependencies on the precise components you are dependent on

*/
public function getId()
{
return $this->context->getRequest()->getParam(StockInterface::STOCK_ID) ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

This code
$this->context->getRequest()->getParam(StockInterface::STOCK_ID)
violates the Law of Demeter https://en.wikipedia.org/wiki/Law_of_Demeter
which is one of the good design guidelince for developing Object Oriented software

/**
* Class DeleteButton
*/
class DeleteButton extends GenericButton implements ButtonProviderInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

it's preferable to use Composition over Inheritance,
so try to not inherit from GenericButton, but just inject it as Constructor Dependency

$this->filter->applySelectionOnTargetProvider();
$component = $this->filter->getComponent();
$dataProvider = $component->getContext()->getDataProvider();
$searchResult = $dataProvider->getSearchResult();
Copy link
Contributor

Choose a reason for hiding this comment

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

I see a violation of encapsulation and Law of Demeter here.
Because current code is equivalent to
$this->filter->getComponent()->getContext()->getDataProvider()->getSearchResult()

@naydav is it really recommended way of applying Filtering?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GitHub issue number magento/magento2#10988

/**
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Inventory::stock';
Copy link
Contributor

Choose a reason for hiding this comment

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

in both Delete and MassDelete we have a duplication of this constant, that means that it should be declared in another place.
Because currently, such code violates DRY

} catch (CouldNotDeleteException $e) {
$errorMessage = __('[ID: %1] ', $id) . $e->getMessage();
$this->messageManager->addErrorMessage($errorMessage);
}
Copy link
Member

Choose a reason for hiding this comment

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

What about NoSuchEntityException?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that it is not necessary because deleteById method throw CouldNotDeleteException and this scenario is served. See Magento\InventoryApi\Api\StockRepositoryInterface.

Copy link
Contributor

Choose a reason for hiding this comment

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

@naydav naydav merged commit 0cc081f into develop Sep 27, 2017
@maghamed maghamed deleted the delete-stock branch December 11, 2018 18:13
magento-cicd2 pushed a commit that referenced this pull request Apr 1, 2021
[Sidecar] MQE-2528: Create automated test for: [MSI] "Tables data updated when SKU of Simple product has been changed from Admin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants