Skip to content

Simplify code product alert email #16571

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 2 commits into from
Sep 12, 2018
Merged

Simplify code product alert email #16571

merged 2 commits into from
Sep 12, 2018

Conversation

arnoudhgz
Copy link
Contributor

@arnoudhgz arnoudhgz commented Jul 5, 2018

The send method was complex and had some duplication in code. This is improved by using private methods for small parts of this method and making the if/else statements more logical (else isn't even used anymore).

As result the PHPMD suppress warning annotations could be removed.

For better string comparison the currently defined types (price and stock) are set as constants.

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)

@magento-engcom-team magento-engcom-team added Partner: MediaCT Pull Request is created by partner MediaCT partners-contribution Pull Request is created by Magento Partner labels Jul 5, 2018
@magento-engcom-team
Copy link
Contributor

Hi @arnoudhgz. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me {$VERSION} instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

return self::XML_PATH_EMAIL_PRICE_TEMPLATE;
}

return self::XML_PATH_EMAIL_STOCK_TEMPLATE;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please replace all such conditionals with ternary operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@orlangur thank you for your feedback, I changed it...

@VladimirZaets
Copy link
Contributor

Hi @arnoudhgz, thank you for collaboration. Please enable strict type mode for this uses declare(strict_types=1) declaration.

*
* @return StoreInterface|Store
*/
private function getStore($customerStoreId)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use type hints for the method arguments.

*
* @return string
*/
private function getTemplateConfigPath()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use type hints for the method arguments.

*
* @return array
*/
private function getProducts()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use type hints for the method arguments.

*
* @return Price|Stock
*/
private function getBlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use type hints for the method arguments.

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets I have added the strict types and included type hinting where possible.

I saw some existing protected methods like setWebsiteId where I could not define a strict type, because the input can be also string in dependend code....

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets I see that with the strict types give now errors in the unit tests. I will first fix this before you have to check again.

@arnoudhgz arnoudhgz closed this Jul 10, 2018
@arnoudhgz arnoudhgz reopened this Jul 10, 2018
@arnoudhgz
Copy link
Contributor Author

@VladimirZaets I will fix the errors in the unit test first before you have to check again.

@VladimirZaets
Copy link
Contributor

Hi @arnoudhgz, maybe you need any help?

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets Yes, I would like that.
I am struggling with the strict type for the setCustomerData method. First I had CustomerInterface but that did not workout, DataObject worked for the unit tests, but now it fails on the integration test.

@VladimirZaets
Copy link
Contributor

@arnoudhgz as I see, the $customer should be CustomerInterface type. I searched usages of this method but I can't find any place where was passed param with other type. Can you please explain what the problem when you add CustomerInterface as a type?

@arnoudhgz
Copy link
Contributor Author

arnoudhgz commented Jul 12, 2018

@VladimirZaets I had this first as CustomerInterface but than the unit tests said that a DataObject was given. The CustomerRepositoryInterface has a return type of CustomerInterface but it actually returns a DataObject.

That is why I changed it to DataObject.

When using CustomerInterface:
image

@VladimirZaets
Copy link
Contributor

@arnaucoll I think the problem is in the unit test that returns DataObject. What test is failed?

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets please see my updated comment with screenshot ^^

@VladimirZaets
Copy link
Contributor

@arnoudhgz The problem is in the test.
In this line uses DataObject instead of CustomerInterface to create the customer mock.

Can you please fix it?

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets it seems solved now. There is only the timeout in Travis.

@VladimirZaets
Copy link
Contributor

@arnoudhgz Thanks for fixes

@VladimirZaets
Copy link
Contributor

Hi @arnoudhgz, can you please make refactoring and use private properties instead of constants.

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets I can, but it seems wrong, I think the class Magento\ProductAlert\Model\Observer should use EMAIL::TYPE_PRICE abd EMAIL::TYPE_STOCK instead of hardcoded strings in $email->setType('price'); and $email->setType('stock');.

Do you think I should maybe refactor this Observer also to make the code better (remove complexities)?

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets are you really sure I should use private properties? These values are constants imho, they can't be changed with setters or inside other functions.

According to the defnition of constants on the PHP website:

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script

@VladimirZaets
Copy link
Contributor

Hi @arnoudhgz. I discussed this question and we take decisions what we should do in the similar cases.
We should use constants if value uses in the few classes. If their value uses in the scope of the class we should use private static declaration.

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets thank you for the explanation, after I changed it to private static variables some test failed in Travis. It was about having to many variables on the class (the maximum is 15). So I changed the code to compare on strings (price or stock).

@VladimirZaets
Copy link
Contributor

Hi @arnoudhgz. I discussed this issue with my teammates and we take the decision that we can't add strict type and type checking to the already existing files due to backward-compatible reason. It's my fail, sorry. Can you please revert the changes related to strict types. Thanks

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets I will change it back.... 👍

@arnoudhgz
Copy link
Contributor Author

@VladimirZaets I removed the strict types checking + the return types and parameter typehinting for existing functions.

The send method was complex and had some duplication in code. This is
improved by using private methods for small parts of this method and
making the if/else statements more logical (else isn't even used
anymore).

As result the PHPMD suppress warning annotations could be removed.
@arnoudhgz
Copy link
Contributor Author

@VladimirZaets do I still need to update the code? Could you provide some feedback?

@VladimirZaets
Copy link
Contributor

Hi @arnoudhgz. Sorry for the delay, we took your PR for delivery

@magento-engcom-team magento-engcom-team merged commit 316634c into magento:2.3-develop Sep 12, 2018
@magento-engcom-team
Copy link
Contributor

Hi @arnoudhgz. Thank you for your contribution.
We will aim to release these changes as part of 2.3.0.
Please check the release notes for final confirmation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: ProductAlert Partner: MediaCT Pull Request is created by partner MediaCT partners-contribution Pull Request is created by Magento Partner Progress: accept Release Line: 2.3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants