Skip to content

Msi source export #147

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 22 commits into from
Oct 29, 2017
Merged

Msi source export #147

merged 22 commits into from
Oct 29, 2017

Conversation

larsroettig
Copy link
Member

@larsroettig larsroettig commented Oct 25, 2017

Description

Provides export functionality for Stock Sources

Manual testing scenarios

  1. Go to System > Export in Magento backend
  2. Select Stock Sources as entity type
  3. Try different filtering options and export CSV files

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)

@@ -0,0 +1,40 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

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

Please pay attention that Travis builds are red.
There is a failure in Static Tests - https://travis-ci.org/magento-engcom/msi/jobs/294045656

$quantityAttribute->setBackendType('decimal');
$quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY);
$quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY);
$this->_attributeCollection->addItem($quantityAttribute);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's move this Attribute Collection building process into the separate class.
Doing so we will improve Single Responsibility as well as will introduce additional extension point for Attribute Collection building.
Thus, if 3rd party developer would like to inject additional attribute or provide own collection of attributes with filters he suppose to overwrite just this Builder


/**
* @inheritdoc
* @throws \Exception
Copy link
Contributor

Choose a reason for hiding this comment

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

do we really throw an Exception out of this method?

Copy link
Contributor

Choose a reason for hiding this comment

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

Exception is thrown by an internal method.

* Export process
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like there is no throwing of \Magento\Framework\Exception\LocalizedException out of current function

Copy link
Contributor

Choose a reason for hiding this comment

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

In the previouse implementation this exception was thrown by an internal method. In current implementation the method really thow this exception by it self.

}

/**
* Export process
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 more meaningful description. As I suppose current function is the main for the exporting process

$this->$filterMethodName($collection, $columnName, $value);
} else {
$this->applyDefaultFilter($collection, $columnName, $value);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Try to avoid dynamic PHP behavior, as it most of the time leads to some magic under the hood and bugs which are really hard to debug.

What we usually do in similar cases when there are some processors (with the same contract/interfaces) and depending on the type given you need to apply specific one - we apply Strategy Pattern

In your case that will look like:

interface FilterProcessorInterface
{
     public function process($collection, $columnName, $value);
}

class FilterProcessorAggregator
{
     // accept an array configured through the DI 
     // ['handler' => 'filter/handler/class']
     //  'filter/handler/class' must be an instance of FilterProcessorInterface
     public function __construct($filterHandlers)

    // $type is one of the possible handlers  Int|String|Decimal|etc
    public function process($type, $collection, $columnName, $value)
    {
         if (!isset($this->hadlers[$type])) {
             throw new Exception();
         }
         $this->hadlers[$type]->process($collection, $columnName, $value);
    }
}

you have a dedicated class for each filter

class IntFilter implements FilterProcessorInterface
class StringFilter implements FilterProcessorInterface
class DecimalFilter implements FilterProcessorInterface

after these changes, your business logic will include just FilterProcessorAggregator instance.
and you will call:
$this->filterProcessor->($type, $collection, $columnName, $value);

Also that will give us an ability to easily add new processors later

$to = $value[1] ?? null;

if (is_scalar($from) && !empty($from)) {
$date = (new \DateTime($from))->format('m/d/Y');
Copy link
Contributor

Choose a reason for hiding this comment

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

better not to use DateTime object directly

but Magento\Framework\Stdlib\DateTime instead

-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/export.xsd">
<entity name="stock_sources" label="Stock Sources" model="Magento\InventoryImportExport\Model\Export\Sources" entityAttributeFilterType="stock_sources"/>
</config>
Copy link
Contributor

Choose a reason for hiding this comment

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

additional blank line should be added at the end

Copy link
Contributor

Choose a reason for hiding this comment

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

done

}
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Both of these refactoring applied will help you to decrease the number of external dependencies for this class

As currently we have a failure in Static Tests:
The class Sources has a coupling between objects value of 15. Consider to reduce the number of dependencies under 13.

@vadimjustus vadimjustus self-assigned this Oct 28, 2017
@maghamed maghamed removed the WIP label Oct 28, 2017
*/
interface SourceItemCollectionFactoryInterface
{
/**
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 of what this Factory creates

*/
interface FilterProcessorInterface
{
/**
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 of the Filter Processing in comment

/**
* @param AttributeCollection $attributeCollection
* @param array $filters
* @return array
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use @inheritdoc if you don't change the comment in the implementation

$to = $value[1] ?? null;

if (is_scalar($from) && !empty($from)) {
$date = (new \DateTime($from))->format('m/d/Y');
Copy link
Contributor

Choose a reason for hiding this comment

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

you are still using new \DateTime but not Magento\Framework\Stdlib\DateTime

@maghamed maghamed merged commit 709ec81 into msi-source-import-export Oct 29, 2017
@maghamed
Copy link
Contributor

Fantastic contribution!! Thanks, guys @vadimjustus and @larsroettig

This contribution really matters!
Striking out 10 Story Points out of the MSI Backlog

@maghamed maghamed deleted the msi-source-export branch December 11, 2018 18:15
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.

4 participants