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

Skip to content

Commit b0035a0

Browse files
committed
Create Catalog Promotions provider and use it
1 parent 376e496 commit b0035a0

9 files changed

Lines changed: 120 additions & 21 deletions

File tree

src/Sylius/Bundle/CoreBundle/Processor/AllCatalogPromotionsProcessor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313

1414
namespace Sylius\Bundle\CoreBundle\Processor;
1515

16-
use Sylius\Component\Resource\Repository\RepositoryInterface;
16+
use Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface;
1717

1818
final class AllCatalogPromotionsProcessor implements AllCatalogPromotionsProcessorInterface
1919
{
2020
private CatalogPromotionClearerInterface $catalogPromotionClearer;
2121

2222
private CatalogPromotionProcessorInterface $catalogPromotionProcessor;
2323

24-
private RepositoryInterface $catalogPromotionRepository;
24+
private EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider;
2525

2626
public function __construct(
2727
CatalogPromotionClearerInterface $catalogPromotionClearer,
2828
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
29-
RepositoryInterface $catalogPromotionRepository
29+
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider
3030
) {
3131
$this->catalogPromotionClearer = $catalogPromotionClearer;
3232
$this->catalogPromotionProcessor = $catalogPromotionProcessor;
33-
$this->catalogPromotionRepository = $catalogPromotionRepository;
33+
$this->catalogPromotionsProvider = $catalogPromotionsProvider;
3434
}
3535

3636
public function process(): void
3737
{
3838
$this->catalogPromotionClearer->clear();
3939

40-
foreach ($this->catalogPromotionRepository->findAll() as $catalogPromotion) {
40+
foreach ($this->catalogPromotionsProvider->provide() as $catalogPromotion) {
4141
$this->catalogPromotionProcessor->process($catalogPromotion);
4242
}
4343
}

src/Sylius/Bundle/CoreBundle/Processor/ProductCatalogPromotionsProcessor.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Sylius\Bundle\CoreBundle\Processor;
1515

1616
use Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface;
17+
use Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface;
1718
use Sylius\Component\Core\Model\CatalogPromotionInterface;
1819
use Sylius\Component\Core\Model\ProductInterface;
1920
use Sylius\Component\Core\Model\ProductVariantInterface;
@@ -22,7 +23,7 @@
2223

2324
final class ProductCatalogPromotionsProcessor implements ProductCatalogPromotionsProcessorInterface
2425
{
25-
private RepositoryInterface $catalogPromotionRepository;
26+
private EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider;
2627

2728
private CatalogPromotionClearerInterface $catalogPromotionClearer;
2829

@@ -31,12 +32,12 @@ final class ProductCatalogPromotionsProcessor implements ProductCatalogPromotion
3132
private CatalogPromotionApplicatorInterface $catalogPromotionApplicator;
3233

3334
public function __construct(
34-
RepositoryInterface $catalogPromotionRepository,
35+
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
3536
CatalogPromotionClearerInterface $catalogPromotionClearer,
3637
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
3738
CatalogPromotionApplicatorInterface $catalogPromotionApplicator
3839
) {
39-
$this->catalogPromotionRepository = $catalogPromotionRepository;
40+
$this->catalogPromotionsProvider = $catalogPromotionsProvider;
4041
$this->catalogPromotionClearer = $catalogPromotionClearer;
4142
$this->catalogPromotionVariantsProvider = $catalogPromotionVariantsProvider;
4243
$this->catalogPromotionApplicator = $catalogPromotionApplicator;
@@ -47,7 +48,7 @@ public function process(ProductInterface $product): void
4748
$variants = $product->getVariants()->toArray();
4849
$this->clearVariants($variants);
4950

50-
foreach ($this->catalogPromotionRepository->findBy(['enabled' => true]) as $catalogPromotion) {
51+
foreach ($this->catalogPromotionsProvider->provide() as $catalogPromotion) {
5152
$this->processCatalogPromotionOnVariants($catalogPromotion, $variants);
5253
}
5354
}

src/Sylius/Bundle/CoreBundle/Resources/config/services/processors.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
>
2222
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface" />
2323
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface" />
24-
<argument type="service" id="sylius.repository.catalog_promotion" />
24+
<argument type="service" id="Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface" />
2525
</service>
2626

2727
<service

src/Sylius/Bundle/CoreBundle/spec/Processor/AllCatalogPromotionsProcessorSpec.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpSpec\ObjectBehavior;
1717
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface;
1818
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface;
19+
use Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface;
1920
use Sylius\Component\Core\Model\CatalogPromotionInterface;
2021
use Sylius\Component\Resource\Repository\RepositoryInterface;
2122

@@ -24,25 +25,25 @@ final class AllCatalogPromotionsProcessorSpec extends ObjectBehavior
2425
function let(
2526
CatalogPromotionClearerInterface $catalogPromotionClearer,
2627
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
27-
RepositoryInterface $catalogPromotionRepository
28+
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider
2829
): void {
2930
$this->beConstructedWith(
3031
$catalogPromotionClearer,
3132
$catalogPromotionProcessor,
32-
$catalogPromotionRepository
33+
$catalogPromotionsProvider
3334
);
3435
}
3536

3637
function it_clears_and_processes_catalog_promotions(
3738
CatalogPromotionClearerInterface $catalogPromotionClearer,
3839
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
39-
RepositoryInterface $catalogPromotionRepository,
40+
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
4041
CatalogPromotionInterface $firstCatalogPromotion,
4142
CatalogPromotionInterface $secondCatalogPromotion
4243
): void {
4344
$catalogPromotionClearer->clear()->shouldBeCalled();
4445

45-
$catalogPromotionRepository->findAll()->willReturn([$firstCatalogPromotion, $secondCatalogPromotion]);
46+
$catalogPromotionsProvider->provide()->willReturn([$firstCatalogPromotion, $secondCatalogPromotion]);
4647

4748
$catalogPromotionProcessor->process($firstCatalogPromotion)->shouldBeCalled();
4849
$catalogPromotionProcessor->process($secondCatalogPromotion)->shouldBeCalled();

src/Sylius/Bundle/CoreBundle/spec/Processor/ProductCatalogPromotionsProcessorSpec.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface;
1919
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface;
2020
use Sylius\Bundle\CoreBundle\Processor\ProductCatalogPromotionsProcessorInterface;
21+
use Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface;
2122
use Sylius\Component\Core\Model\CatalogPromotionInterface;
2223
use Sylius\Component\Core\Model\ProductInterface;
2324
use Sylius\Component\Core\Model\ProductVariantInterface;
@@ -27,13 +28,13 @@
2728
final class ProductCatalogPromotionsProcessorSpec extends ObjectBehavior
2829
{
2930
function let(
30-
RepositoryInterface $catalogPromotionRepository,
31+
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
3132
CatalogPromotionClearerInterface $catalogPromotionClearer,
3233
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
3334
CatalogPromotionApplicatorInterface $catalogPromotionApplicator
3435
): void {
3536
$this->beConstructedWith(
36-
$catalogPromotionRepository,
37+
$catalogPromotionsProvider,
3738
$catalogPromotionClearer,
3839
$catalogPromotionVariantsProvider,
3940
$catalogPromotionApplicator
@@ -46,7 +47,7 @@ function it_implements_product_catalog_promotions_processor_interface(): void
4647
}
4748

4849
function it_reapplies_catalog_promotion_on_products_variants(
49-
RepositoryInterface $catalogPromotionRepository,
50+
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
5051
CatalogPromotionClearerInterface $catalogPromotionClearer,
5152
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
5253
CatalogPromotionApplicatorInterface $catalogPromotionApplicator,
@@ -66,8 +67,8 @@ function it_reapplies_catalog_promotion_on_products_variants(
6667
$catalogPromotionClearer->clearVariant($firstVariant)->shouldBeCalled();
6768
$catalogPromotionClearer->clearVariant($secondVariant)->shouldBeCalled();
6869

69-
$catalogPromotionRepository->findBy(['enabled' => true])->willReturn([$catalogPromotion->getWrappedObject()]);
70-
$catalogPromotionVariantsProvider->provideEligibleVariants($catalogPromotion)->willReturn([$secondVariant->getWrappedObject()]);
70+
$catalogPromotionsProvider->provide()->willReturn([$catalogPromotion]);
71+
$catalogPromotionVariantsProvider->provideEligibleVariants($catalogPromotion)->willReturn([$secondVariant]);
7172

7273
$catalogPromotionApplicator->applyOnVariant($firstVariant, $catalogPromotion)->shouldNotBeCalled();
7374
$catalogPromotionApplicator->applyOnVariant($secondVariant, $catalogPromotion)->shouldBeCalled();
@@ -76,7 +77,7 @@ function it_reapplies_catalog_promotion_on_products_variants(
7677
}
7778

7879
function it_does_not_apply_promotion_if_there_are_no_eligible_variants(
79-
RepositoryInterface $catalogPromotionRepository,
80+
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
8081
CatalogPromotionClearerInterface $catalogPromotionClearer,
8182
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
8283
CatalogPromotionApplicatorInterface $catalogPromotionApplicator,
@@ -96,7 +97,7 @@ function it_does_not_apply_promotion_if_there_are_no_eligible_variants(
9697
$catalogPromotionClearer->clearVariant($firstVariant)->shouldBeCalled();
9798
$catalogPromotionClearer->clearVariant($secondVariant)->shouldBeCalled();
9899

99-
$catalogPromotionRepository->findBy(['enabled' => true])->willReturn([$catalogPromotion->getWrappedObject()]);
100+
$catalogPromotionsProvider->provide()->willReturn([$catalogPromotion]);
100101
$catalogPromotionVariantsProvider->provideEligibleVariants($catalogPromotion)->willReturn([]);
101102

102103
$catalogPromotionApplicator->applyOnVariant($firstVariant, $catalogPromotion)->shouldNotBeCalled();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\PromotionBundle\Provider;
15+
16+
use Sylius\Component\Resource\Repository\RepositoryInterface;
17+
18+
final class EligibleCatalogPromotionsProvider implements EligibleCatalogPromotionsProviderInterface
19+
{
20+
private RepositoryInterface $catalogPromotionRepository;
21+
22+
public function __construct(RepositoryInterface $catalogPromotionRepository)
23+
{
24+
$this->catalogPromotionRepository = $catalogPromotionRepository;
25+
}
26+
27+
public function provide(): array
28+
{
29+
return $this->catalogPromotionRepository->findBy(['enabled' => true]);
30+
}
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\Bundle\PromotionBundle\Provider;
6+
7+
use Sylius\Component\Core\Model\CatalogPromotionInterface;
8+
9+
interface EligibleCatalogPromotionsProviderInterface
10+
{
11+
/** @return array|CatalogPromotionInterface[] */
12+
public function provide(): array;
13+
}

src/Sylius/Bundle/PromotionBundle/Resources/config/services.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,12 @@
7171
<service id="sylius.promotion_coupon_generator.percentage_policy" class="Sylius\Component\Promotion\Generator\PercentageGenerationPolicy">
7272
<argument type="service" id="sylius.repository.promotion_coupon" />
7373
</service>
74+
75+
<service
76+
id="Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface"
77+
class="Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProvider"
78+
>
79+
<argument type="service" id="sylius.repository.catalog_promotion" />
80+
</service>
7481
</services>
7582
</container>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\Bundle\PromotionBundle\Provider;
15+
16+
use PhpSpec\ObjectBehavior;
17+
use Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface;
18+
use Sylius\Component\Promotion\Model\CatalogPromotionInterface;
19+
use Sylius\Component\Resource\Repository\RepositoryInterface;
20+
21+
final class EligibleCatalogPromotionsProviderSpec extends ObjectBehavior
22+
{
23+
function let(RepositoryInterface $catalogPromotionRepository): void
24+
{
25+
$this->beConstructedWith($catalogPromotionRepository);
26+
}
27+
28+
function it_implements_eligible_catalog_promotions_provider_interface(): void
29+
{
30+
$this->shouldImplement(EligibleCatalogPromotionsProviderInterface::class);
31+
}
32+
33+
function it_provides_all_enabled_catalog_promotions_from_repository(
34+
RepositoryInterface $catalogPromotionRepository,
35+
CatalogPromotionInterface $firstCatalogPromotion,
36+
CatalogPromotionInterface $secondCatalogPromotion
37+
): void {
38+
$catalogPromotionRepository
39+
->findBy(['enabled' => true])
40+
->willReturn([$firstCatalogPromotion, $secondCatalogPromotion])
41+
;
42+
43+
$this->provide()->shouldReturn([$firstCatalogPromotion, $secondCatalogPromotion]);
44+
}
45+
}

0 commit comments

Comments
 (0)