-
Notifications
You must be signed in to change notification settings - Fork 253
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
Conversation
{ | ||
$resultRedirect = $this->resultRedirectFactory->create(); | ||
$stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID); | ||
if ($stockId !== null) { |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
3971aa7
to
3f7f271
Compare
|
private $stockRepository; | ||
|
||
/** | ||
* @param Context $context |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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]> |
There was a problem hiding this comment.
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 = []; |
There was a problem hiding this comment.
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(); | ||
} |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was -> would be
3f7f271
to
f6fb89e
Compare
/** | ||
* @param Context $context | ||
*/ | ||
public function __construct(Context $context) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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
f6fb89e
to
9300183
Compare
} catch (CouldNotDeleteException $e) { | ||
$errorMessage = __('[ID: %1] ', $id) . $e->getMessage(); | ||
$this->messageManager->addErrorMessage($errorMessage); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about NoSuchEntityException?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bartekszymanski correct
-- slight refactoring
-- slight refactoring
-- code style fixes
[Sidecar] MQE-2528: Create automated test for: [MSI] "Tables data updated when SKU of Simple product has been changed from Admin"
Description
Fixed Issues (if relevant)
Manual testing scenarios
Contribution checklist