Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Catalog promotion channel pricing relation#13375

Merged
GSadee merged 1 commit into
Sylius:masterfrom
Rafikooo:catalog-promotion-channel-pricing-relation-v3
Dec 14, 2021
Merged

Catalog promotion channel pricing relation#13375
GSadee merged 1 commit into
Sylius:masterfrom
Rafikooo:catalog-promotion-channel-pricing-relation-v3

Conversation

@Rafikooo
Copy link
Copy Markdown
Contributor

@Rafikooo Rafikooo commented Dec 9, 2021

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
License MIT

@Rafikooo Rafikooo added Feature New feature proposals. Admin AdminBundle related issues and PRs. API APIs related issues and PRs. labels Dec 9, 2021
@Rafikooo Rafikooo requested review from AdamKasp and GSadee December 9, 2021 14:55
@Rafikooo Rafikooo requested a review from a team as a code owner December 9, 2021 14:55
@probot-autolabeler probot-autolabeler Bot added the Shop ShopBundle related issues and PRs. label Dec 9, 2021
$catalogPromotions = [];

foreach ($channelPricings as $channelPricing) {
$catalogPromotions = array_merge($catalogPromotions, $channelPricing->getAppliedPromotions()->toArray());
Copy link
Copy Markdown
Contributor Author

@Rafikooo Rafikooo Dec 11, 2021

Choose a reason for hiding this comment

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

I left ->toArray on purpose although foreach below can handle array collection objects.
On line 42 I get appliedPromotions and store them in a variable but in the second step, I clear promotions stored in channelPricing object. After that, it occurs that removing catalog promotions inside channelPricing object also affect local variable $catalogPromotions and clear it also. Alternatively I could use instead of ->toArray clone function which can be more explicit in the future, that we're dealing with a pointer

Copy link
Copy Markdown
Contributor Author

@Rafikooo Rafikooo Dec 13, 2021

Choose a reason for hiding this comment

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

I could write a spec to assure the right behavior

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm ok with this implementation, but if the catalog promotion was deleted this by accident, we should check doctrine mapping, to not delete something by accident. I left a few comments related to cascading on mapping and migration. Can you apply them and check if the error still exists?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I resolved mapping-related stuff. I'm not sure if we are on the same page with my comment above, because the problem happens in the application state rather than DB.

A moment ago I discovered that I was wrong. Indeed, there is that pointer/reference case, but it is in src/Sylius/Bundle/CoreBundle/Processor/CatalogPromotionClearer.php:42. After getting ArrayCollection, storing it in $appliedPromotions and callingclearChannelPricing, foreach is skipped. ->toArray produces deep copy so foreach is executed correctly

Copy link
Copy Markdown
Contributor Author

@Rafikooo Rafikooo left a comment

Choose a reason for hiding this comment

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

Added two notes about the code

@AdamKasp
Copy link
Copy Markdown
Contributor

I think you should squash part of your commits (30 commits IMO is too many)

Comment thread src/Sylius/Behat/Context/Api/Shop/ProductVariantContext.php Outdated
Comment thread src/Sylius/Behat/Context/Api/Shop/ProductVariantContext.php Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Serializer/ProductVariantNormalizer.php Outdated
Comment thread src/Sylius/Bundle/ShopBundle/Resources/private/js/sylius-variants-prices.js Outdated
Comment thread src/Sylius/Bundle/ShopBundle/Resources/views/Product/Box/_content.html.twig Outdated
Comment thread src/Sylius/Bundle/ShopBundle/Resources/views/Product/Show/_price.html.twig Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Serializer/ProductVariantNormalizer.php Outdated
Comment thread src/Sylius/Bundle/CoreBundle/Doctrine/ORM/ChannelPricingRepository.php Outdated
Comment thread src/Sylius/Bundle/CoreBundle/Migrations/Version20211129213836.php
$catalogPromotions = [];

foreach ($channelPricings as $channelPricing) {
$catalogPromotions = array_merge($catalogPromotions, $channelPricing->getAppliedPromotions()->toArray());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm ok with this implementation, but if the catalog promotion was deleted this by accident, we should check doctrine mapping, to not delete something by accident. I left a few comments related to cascading on mapping and migration. Can you apply them and check if the error still exists?

Comment thread src/Sylius/Bundle/CoreBundle/Migrations/Version20211129213836.php Outdated
Comment thread src/Sylius/Bundle/CoreBundle/Migrations/Version20211129213836.php Outdated
Copy link
Copy Markdown
Contributor Author

@Rafikooo Rafikooo left a comment

Choose a reason for hiding this comment

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

Checked if I applied all review changes

Comment on lines +156 to 162
foreach ($this->appliedPromotions as $appliedPromotion) {
if($appliedPromotion->isExclusive()) {
return true;
}
}

return false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

really minor thing

Suggested change
foreach ($this->appliedPromotions as $appliedPromotion) {
if($appliedPromotion->isExclusive()) {
return true;
}
}
return false;
return $this->appliedPromotions->exists(function($key, CatalogPromotionInterface $value): bool {
return $value->isExclusive();
});

or something like this

Suggested change
foreach ($this->appliedPromotions as $appliedPromotion) {
if($appliedPromotion->isExclusive()) {
return true;
}
}
return false;
return $this->appliedPromotions->exists(fn($key, CatalogPromotionInterface $value) => $value->isExclusive());

Comment on lines +50 to +53
* @var ArrayCollection
* @psalm-var ArrayCollection<array-key, CatalogPromotionInterface>
*/
protected $appliedPromotions;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
* @var ArrayCollection
* @psalm-var ArrayCollection<array-key, CatalogPromotionInterface>
*/
protected $appliedPromotions;
* @psalm-var ArrayCollection<array-key, CatalogPromotionInterface>
*/
protected Collection $appliedPromotions;

@GSadee GSadee merged commit a31fe0b into Sylius:master Dec 14, 2021
@GSadee
Copy link
Copy Markdown
Member

GSadee commented Dec 14, 2021

Thank you, Rafał! 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Admin AdminBundle related issues and PRs. API APIs related issues and PRs. Feature New feature proposals. Shop ShopBundle related issues and PRs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants