Skip to content
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

Catalog promotion channel pricing relation #13375

Merged
merged 1 commit into from Dec 14, 2021
Merged

Catalog promotion channel pricing relation #13375

merged 1 commit into from Dec 14, 2021

Conversation

Rafikooo
Copy link
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 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

foreach ($channelPricings as $channelPricing) {
$catalogPromotions = array_merge($catalogPromotions, $channelPricing->getAppliedPromotions()->toArray());
Copy link
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
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
Member

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
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
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
Contributor

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


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

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
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
Member

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
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
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.

None yet

4 participants