move default criteria to catalog promotion provider

This commit is contained in:
Adam Kasperczak 2022-01-11 08:20:42 +01:00 committed by Rafikooo
parent d6c39332fd
commit 220618fedc
No known key found for this signature in database
GPG key ID: 4A26D8327BC2442B
8 changed files with 25 additions and 50 deletions

View file

@ -24,24 +24,20 @@ final class AllCatalogPromotionsProcessor implements AllCatalogPromotionsProcess
private EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider;
private iterable $defaultCriteria;
public function __construct(
CatalogPromotionClearerInterface $catalogPromotionClearer,
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
iterable $defaultCriteria = []
) {
$this->catalogPromotionClearer = $catalogPromotionClearer;
$this->catalogPromotionProcessor = $catalogPromotionProcessor;
$this->catalogPromotionsProvider = $catalogPromotionsProvider;
$this->defaultCriteria = $defaultCriteria;
}
public function process(): void
{
$this->catalogPromotionClearer->clear();
$eligibleCatalogPromotions = $this->catalogPromotionsProvider->provide($this->defaultCriteria);
$eligibleCatalogPromotions = $this->catalogPromotionsProvider->provide();
foreach ($eligibleCatalogPromotions as $catalogPromotion) {
$this->catalogPromotionProcessor->process($catalogPromotion);

View file

@ -22,7 +22,6 @@
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface" />
<argument type="service" id="Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProviderInterface" />
<argument type="tagged_iterator" tag="sylius.catalog_promotion.criteria" />
</service>
<service

View file

@ -28,15 +28,12 @@ final class AllCatalogPromotionsProcessorSpec extends ObjectBehavior
function let(
CatalogPromotionClearerInterface $catalogPromotionClearer,
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
CriteriaInterface $firstCriteria,
CriteriaInterface $secondCriteria
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider
): void {
$this->beConstructedWith(
$catalogPromotionClearer,
$catalogPromotionProcessor,
$catalogPromotionsProvider,
[$firstCriteria, $secondCriteria]
$catalogPromotionsProvider
);
}
@ -45,16 +42,11 @@ final class AllCatalogPromotionsProcessorSpec extends ObjectBehavior
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
EligibleCatalogPromotionsProviderInterface $catalogPromotionsProvider,
CatalogPromotionInterface $firstCatalogPromotion,
CatalogPromotionInterface $secondCatalogPromotion,
CriteriaInterface $firstCriteria,
CriteriaInterface $secondCriteria
CatalogPromotionInterface $secondCatalogPromotion
): void {
$catalogPromotionClearer->clear()->shouldBeCalled();
$catalogPromotionsProvider
->provide([$firstCriteria, $secondCriteria])
->willReturn([$firstCatalogPromotion, $secondCatalogPromotion])
;
$catalogPromotionsProvider->provide()->willReturn([$firstCatalogPromotion, $secondCatalogPromotion]);
$catalogPromotionProcessor->process($firstCatalogPromotion)->shouldBeCalled();
$catalogPromotionProcessor->process($secondCatalogPromotion)->shouldBeCalled();

View file

@ -13,21 +13,24 @@ declare(strict_types=1);
namespace Sylius\Bundle\PromotionBundle\Provider;
use Sylius\Bundle\PromotionBundle\Criteria\CriteriaInterface;
use Sylius\Component\Promotion\Repository\CatalogPromotionRepositoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
final class EligibleCatalogPromotionsProvider implements EligibleCatalogPromotionsProviderInterface
{
private CatalogPromotionRepositoryInterface $catalogPromotionRepository;
public function __construct(CatalogPromotionRepositoryInterface $catalogPromotionRepository)
{
private iterable $defaultCriteria;
public function __construct(
CatalogPromotionRepositoryInterface $catalogPromotionRepository,
iterable $defaultCriteria = []
) {
$this->catalogPromotionRepository = $catalogPromotionRepository;
$this->defaultCriteria = $defaultCriteria;
}
public function provide(iterable $criteria = []): array
public function provide(): array
{
return $this->catalogPromotionRepository->findByCriteria($criteria);
return $this->catalogPromotionRepository->findByCriteria($this->defaultCriteria);
}
}

View file

@ -10,9 +10,7 @@ use Sylius\Component\Core\Model\CatalogPromotionInterface;
interface EligibleCatalogPromotionsProviderInterface
{
/**
* @param iterable|CriteriaInterface[] $criteria
*
* @return array|CatalogPromotionInterface[]
*/
public function provide(iterable $criteria = []): array;
public function provide(): array;
}

View file

@ -78,6 +78,7 @@
class="Sylius\Bundle\PromotionBundle\Provider\EligibleCatalogPromotionsProvider"
>
<argument type="service" id="sylius.repository.catalog_promotion" />
<argument type="tagged_iterator" tag="sylius.catalog_promotion.criteria" />
</service>
<service id="Sylius\Component\Promotion\Provider\DateTimeProviderInterface" class="Sylius\Component\Promotion\Provider\Calendar" />

View file

@ -21,9 +21,12 @@ use Sylius\Component\Promotion\Repository\CatalogPromotionRepositoryInterface;
final class EligibleCatalogPromotionsProviderSpec extends ObjectBehavior
{
function let(CatalogPromotionRepositoryInterface $catalogPromotionRepository): void
{
$this->beConstructedWith($catalogPromotionRepository);
function let(
CatalogPromotionRepositoryInterface $catalogPromotionRepository,
CriteriaInterface $firstCriterion,
CriteriaInterface $secondCriterion
): void {
$this->beConstructedWith($catalogPromotionRepository, [$firstCriterion, $secondCriterion]);
}
function it_implements_eligible_catalog_promotions_provider_interface(): void
@ -31,19 +34,6 @@ final class EligibleCatalogPromotionsProviderSpec extends ObjectBehavior
$this->shouldImplement(EligibleCatalogPromotionsProviderInterface::class);
}
function it_provides_all_catalog_promotions_when_no_criteria_is_specified(
CatalogPromotionRepositoryInterface $catalogPromotionRepository,
CatalogPromotionInterface $firstCatalogPromotion,
CatalogPromotionInterface $secondCatalogPromotion
): void {
$catalogPromotionRepository
->findByCriteria([])
->willReturn([$firstCatalogPromotion, $secondCatalogPromotion])
;
$this->provide()->shouldReturn([$firstCatalogPromotion, $secondCatalogPromotion]);
}
function it_provides_catalog_promotions_based_on_criteria(
CatalogPromotionRepositoryInterface $catalogPromotionRepository,
CriteriaInterface $firstCriterion,
@ -57,7 +47,7 @@ final class EligibleCatalogPromotionsProviderSpec extends ObjectBehavior
;
$this
->provide([$firstCriterion, $secondCriterion])
->provide()
->shouldReturn([$firstCatalogPromotion, $secondCatalogPromotion])
;
}

View file

@ -49,9 +49,7 @@ class EligibleCatalogPromotionsProcessorTest extends WebTestCase
file_put_contents(self::$kernel->getProjectDir() . '/var/temporaryDate.txt', '2021-10-12 00:00:02');
$dateRangeCriteria = self::$container->get('sylius.catalog_promotion.criteria.date_range');
$eligibleCatalogPromotions = $eligibleCatalogPromotionsProvider->provide([$dateRangeCriteria]);
$eligibleCatalogPromotions = $eligibleCatalogPromotionsProvider->provide();
$expectedDateTimes = [
new \DateTime('2021-10-12 00:00:00'),
@ -79,9 +77,7 @@ class EligibleCatalogPromotionsProcessorTest extends WebTestCase
file_put_contents(self::$kernel->getProjectDir() . '/var/temporaryDate.txt', '2021-10-12 23:59:58');
$dateRangeCriteria = self::$container->get('sylius.catalog_promotion.criteria.date_range');
$eligibleCatalogPromotions = $eligibleCatalogPromotionsProvider->provide([$dateRangeCriteria]);
$eligibleCatalogPromotions = $eligibleCatalogPromotionsProvider->provide();
$expectedDateTimes = [
new \DateTime('2021-10-12 23:59:59'),