Skip to content

Commit

Permalink
Extract applied promotions information formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Sep 1, 2021
1 parent 1bc17ab commit f59b034
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@

namespace Sylius\Bundle\CoreBundle\Applicator;

use Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatterInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionActionInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionTranslationInterface;

final class CatalogPromotionApplicator implements CatalogPromotionApplicatorInterface
{
private AppliedPromotionInformationFormatterInterface $appliedPromotionInformationFormatter;

public function __construct(AppliedPromotionInformationFormatterInterface $appliedPromotionInformationFormatter)
{
$this->appliedPromotionInformationFormatter = $appliedPromotionInformationFormatter;
}

public function applyCatalogPromotion(
ProductVariantInterface $variant,
CatalogPromotionInterface $catalogPromotion
Expand All @@ -45,20 +52,9 @@ private function applyDiscountFromAction(

$channelPricing->setPrice((int) ($channelPricing->getPrice() - ($channelPricing->getPrice() * $discount)));

$channelPricing->addAppliedPromotion($this->formatAppliedPromotion($catalogPromotion));
$channelPricing->addAppliedPromotion(
$this->appliedPromotionInformationFormatter->format($catalogPromotion)
);
}
}

private function formatAppliedPromotion(CatalogPromotionInterface $catalogPromotion): array
{
/** @var CatalogPromotionTranslationInterface $translation */
$translation = $catalogPromotion->getTranslations()->first();

/** @var string $label */
$label = $translation->getLabel();
/** @var string $code */
$code = $catalogPromotion->getCode();

return [$code => ['name' => $label]];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\CoreBundle\Formatter;

use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionTranslationInterface;

final class AppliedPromotionInformationFormatter implements AppliedPromotionInformationFormatterInterface
{
public function format(CatalogPromotionInterface $catalogPromotion): array
{
/** @var CatalogPromotionTranslationInterface $translation */
$translation = $catalogPromotion->getTranslations()->first();
/** @var string $code */
$code = $catalogPromotion->getCode();
/** @var string $name */
$name = $translation->getLabel();

return [$code => ['name' => $name]];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Sylius\Bundle\CoreBundle\Formatter;

use Sylius\Component\Core\Model\CatalogPromotionInterface;

interface
AppliedPromotionInformationFormatterInterface
{
public function format(CatalogPromotionInterface $catalogPromotion): array;
}
9 changes: 8 additions & 1 deletion src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,17 @@
<argument type="service" id="sylius.email_sender" />
</service>

<service
id="Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatterInterface"
class="Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatter"
/>

<service
id="Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface"
class="Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicator"
/>
>
<argument type="service" id="Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatterInterface" />
</service>

<service
id="Sylius\Component\Core\Provider\CatalogPromotionVariantsProviderInterface"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface;
use Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatterInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
Expand All @@ -25,16 +26,21 @@

final class CatalogPromotionApplicatorSpec extends ObjectBehavior
{
function let(AppliedPromotionInformationFormatterInterface $appliedPromotionInformationFormatter): void
{
$this->beConstructedWith($appliedPromotionInformationFormatter);
}

function it_implements_catalog_promotion_applicator_interface(): void
{
$this->shouldImplement(CatalogPromotionApplicatorInterface::class);
}

function it_applies_percentage_discount_on_all_products_variants(
AppliedPromotionInformationFormatterInterface $appliedPromotionInformationFormatter,
ProductVariantInterface $variant,
CatalogPromotionInterface $catalogPromotion,
CatalogPromotionActionInterface $catalogPromotionAction,
CatalogPromotionTranslationInterface $translation,
ChannelPricingInterface $firstChannelPricing,
ChannelPricingInterface $secondChannelPricing
): void {
Expand All @@ -47,11 +53,8 @@ function it_applies_percentage_discount_on_all_products_variants(
$secondChannelPricing->getWrappedObject(),
]));

$appliedPromotionInformationFormatter->format($catalogPromotion)->willReturn(['winter_sale' => ['name' => 'Winter sale']]);
$catalogPromotionAction->getConfiguration()->willReturn(['amount' => 0.3]);
$catalogPromotion->getLabel()->willReturn('Winter sale');
$catalogPromotion->getTranslations()->willReturn(new ArrayCollection([$translation->getWrappedObject()]));
$translation->getLabel()->willReturn('Winter sale');
$catalogPromotion->getCode()->willReturn('winter_sale');

$firstChannelPricing->getPrice()->willReturn(1000);
$firstChannelPricing->getOriginalPrice()->willReturn(null);
Expand All @@ -69,6 +72,7 @@ function it_applies_percentage_discount_on_all_products_variants(
}

function it_does_not_set_original_price_during_application_if_its_already_there(
AppliedPromotionInformationFormatterInterface $appliedPromotionInformationFormatter,
ProductVariantInterface $variant,
CatalogPromotionInterface $catalogPromotion,
CatalogPromotionActionInterface $catalogPromotionAction,
Expand All @@ -83,13 +87,9 @@ function it_does_not_set_original_price_during_application_if_its_already_there(
$catalogPromotionAction->getWrappedObject(),
]));

$appliedPromotionInformationFormatter->format($catalogPromotion)->willReturn(['winter_sale' => ['name' => 'Winter sale']]);
$catalogPromotionAction->getConfiguration()->willReturn(['amount' => 0.5]);

$catalogPromotion->getLabel()->willReturn('Winter sale');
$catalogPromotion->getTranslations()->willReturn(new ArrayCollection([$translation->getWrappedObject()]));
$translation->getLabel()->willReturn('Winter sale');
$catalogPromotion->getCode()->willReturn('winter_sale');

$firstChannelPricing->getPrice()->willReturn(1000);
$firstChannelPricing->getOriginalPrice()->willReturn(2000);
$firstChannelPricing->setOriginalPrice(Argument::any())->shouldNotBeCalled();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\CoreBundle\Formatter;

use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\CoreBundle\Formatter\AppliedPromotionInformationFormatterInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
use Sylius\Component\Promotion\Model\CatalogPromotionTranslationInterface;

final class AppliedPromotionInformationFormatterSpec extends ObjectBehavior
{
function it_implements_applied_promotion_information_formatter_interface(): void
{
$this->shouldImplement(AppliedPromotionInformationFormatterInterface::class);
}

function it_formats_applied_promotion_information(
CatalogPromotionInterface $catalogPromotion,
CatalogPromotionTranslationInterface $translation
): void {
$catalogPromotion->getTranslations()->willReturn(new ArrayCollection([$translation->getWrappedObject()]));

$catalogPromotion->getCode()->willReturn('winter_sale');
$translation->getLabel()->willReturn('Winter sale');

$this->format($catalogPromotion)->shouldReturn(['winter_sale' => ['name' => 'Winter sale']]);
}
}

0 comments on commit f59b034

Please sign in to comment.