Skip to content

Commit

Permalink
Merge pull request Sylius#7809 from lchrusciel/product-taxons
Browse files Browse the repository at this point in the history
[Promotion] Small cleanup related to product taxon proxy
  • Loading branch information
pjedrzejewski committed Mar 23, 2017
2 parents c0f5738 + 40c9f0a commit fc03108
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
Expand Up @@ -13,6 +13,7 @@

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
Expand All @@ -39,10 +40,25 @@ public function isEligible(PromotionSubjectInterface $subject, array $configurat

/* @var $item OrderItemInterface */
foreach ($subject->getItems() as $item) {
foreach ($item->getProduct()->getProductTaxons() as $productTaxon) {
if (in_array($productTaxon->getTaxon()->getCode(), $configuration['taxons'], true)) {
if ($this->hasProductValidTaxon($item->getProduct(), $configuration)) {
return true;
}
}

return false;
}

/**
* @param ProductInterface $product
* @param array $configuration
*
* @return bool
*/
private function hasProductValidTaxon(ProductInterface $product, array $configuration)
{
foreach ($product->getTaxons() as $taxon) {
if (in_array($taxon->getCode(), $configuration['taxons'], true)) {
return true;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Component/Core/Promotion/Filter/TaxonFilter.php
Expand Up @@ -45,8 +45,8 @@ public function filter(array $items, array $configuration)
*/
private function hasProductValidTaxon(ProductInterface $product, array $taxons)
{
foreach ($product->getProductTaxons() as $productTaxon) {
if (in_array($productTaxon->getTaxon()->getCode(), $taxons)) {
foreach ($product->getTaxons() as $taxon) {
if (in_array($taxon->getCode(), $taxons, true)) {
return true;
}
}
Expand Down
Expand Up @@ -43,14 +43,12 @@ function it_recognizes_a_subject_as_eligible_if_product_taxon_is_matched(
OrderInterface $subject,
OrderItemInterface $item,
ProductInterface $bastardSword,
ProductTaxonInterface $bastardSwordProductTaxon,
TaxonInterface $swords
) {
$configuration = ['taxons' => ['swords']];

$swords->getCode()->willReturn('swords');
$bastardSword->getProductTaxons()->willReturn([$bastardSwordProductTaxon]);
$bastardSwordProductTaxon->getTaxon()->willReturn($swords);
$bastardSword->getTaxons()->willReturn([$swords]);
$item->getProduct()->willReturn($bastardSword);
$subject->getItems()->willReturn([$item]);

Expand All @@ -67,8 +65,7 @@ function it_recognizes_a_subject_as_eligible_if_a_product_taxon_is_matched_to_on
$configuration = ['taxons' => ['swords', 'axes']];

$swords->getCode()->willReturn('swords');
$bastardSword->getProductTaxons()->willReturn([$bastardSwordProductTaxon]);
$bastardSwordProductTaxon->getTaxon()->willReturn($swords);
$bastardSword->getTaxons()->willReturn([$swords]);
$item->getProduct()->willReturn($bastardSword);
$subject->getItems()->willReturn([$item]);

Expand All @@ -85,8 +82,7 @@ function it_recognizes_a_subject_as_not_eligible_if_a_product_taxon_is_not_match
$configuration = ['taxons' => ['swords', 'axes']];

$bows->getCode()->willReturn('bows');
$reflexBow->getProductTaxons()->willReturn([$reflexBowProductTaxon]);
$reflexBowProductTaxon->getTaxon()->willReturn($bows);
$reflexBow->getTaxons()->willReturn([$bows]);
$item->getProduct()->willReturn($reflexBow);
$subject->getItems()->willReturn([$item]);

Expand Down
Expand Up @@ -40,18 +40,14 @@ function it_filters_passed_order_items_with_given_configuration(
ProductInterface $product1,
ProductInterface $product2,
TaxonInterface $taxon1,
TaxonInterface $taxon2,
ProductTaxonInterface $productTaxon1,
ProductTaxonInterface $productTaxon2
TaxonInterface $taxon2
) {
$item1->getProduct()->willReturn($product1);
$product1->getProductTaxons()->willReturn([$productTaxon1]);
$productTaxon1->getTaxon()->willReturn($taxon1);
$product1->getTaxons()->willReturn([$taxon1]);
$taxon1->getCode()->willReturn('taxon1');

$item2->getProduct()->willReturn($product2);
$product2->getProductTaxons()->willReturn([$productTaxon2]);
$productTaxon2->getTaxon()->willReturn($taxon2);
$product2->getTaxons()->willReturn([$taxon2]);
$taxon2->getCode()->willReturn('taxon2');

$this->filter([$item1, $item2], ['filters' => ['taxons_filter' => ['taxons' => ['taxon1']]]])->shouldReturn([$item1]);
Expand Down

0 comments on commit fc03108

Please sign in to comment.