Skip to content

Commit 98f7569

Browse files
committed
Conditions ready
1 parent 80a725a commit 98f7569

File tree

11 files changed

+193
-122
lines changed

11 files changed

+193
-122
lines changed

code_samples/discounts/src/Discounts/ExpressionProvider/IsAnniversaryResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __invoke(DateTimeInterface $date, int $tolerance = 0): bool
1818
{
1919
$d1 = $this->unifyYear(new DateTimeImmutable());
2020
$d2 = $this->unifyYear($date);
21-
21+
2222
$diff = $d1->diff($d2, true)->days;
2323

2424
// Check if the difference between dates is within the tolerance
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Discounts\Step;
4+
5+
use Ibexa\Contracts\Discounts\Admin\Form\Data\AbstractDiscountStep;
6+
7+
final class AnniversaryConditionStep extends AbstractDiscountStep
8+
{
9+
public const IDENTIFIER = 'anniversary_condition_step';
10+
11+
public ?bool $enabled;
12+
13+
public ?int $tolerance;
14+
15+
public function __construct(?bool $enabled = false, ?int $tolerance = 0)
16+
{
17+
$this->enabled = $enabled;
18+
$this->tolerance = $tolerance;
19+
}
20+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Discounts\Step;
5+
6+
use App\Discounts\Condition\IsAccountAnniversary;
7+
use Ibexa\Contracts\Discounts\Event\CreateDiscountCreateStructEvent;
8+
use Ibexa\Contracts\Discounts\Event\CreateDiscountUpdateStructEvent;
9+
use Ibexa\Contracts\Discounts\Event\CreateFormDataEvent;
10+
use Ibexa\Contracts\Discounts\Event\DiscountStructEventInterface;
11+
use Ibexa\Contracts\Discounts\Event\MapDiscountToFormDataEvent;
12+
use Ibexa\Contracts\Discounts\Value\DiscountType;
13+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14+
use Symfony\Contracts\EventDispatcher\Event;
15+
16+
final class AnniversaryConditionStepEventSubscriber implements EventSubscriberInterface
17+
{
18+
public static function getSubscribedEvents(): array
19+
{
20+
return [
21+
CreateFormDataEvent::class => 'addAnniversaryConditionStep',
22+
MapDiscountToFormDataEvent::class => 'addAnniversaryConditionStep',
23+
CreateDiscountCreateStructEvent::class => 'addStepDataToStruct',
24+
CreateDiscountUpdateStructEvent::class => 'addStepDataToStruct',
25+
];
26+
}
27+
28+
/**
29+
* @param \Ibexa\Contracts\Discounts\Event\CreateFormDataEvent|\Ibexa\Contracts\Discounts\Event\MapDiscountToFormDataEvent $event
30+
*/
31+
public function addAnniversaryConditionStep(Event $event): void
32+
{
33+
$data = $event->getData();
34+
if ($data->getType() !== DiscountType::CART) {
35+
return;
36+
}
37+
38+
/** @var \App\Discounts\Condition\IsAccountAnniversary $discount */
39+
$discount = $event instanceof MapDiscountToFormDataEvent ?
40+
$event->getDiscount()->getConditionByIdentifier(IsAccountAnniversary::IDENTIFIER) :
41+
null;
42+
43+
$conditionStep = $discount !== null ?
44+
new AnniversaryConditionStep(true, $discount->getTolerance()) :
45+
new AnniversaryConditionStep();
46+
47+
$event->setData(
48+
$event->getData()->withStep(
49+
$conditionStep,
50+
AnniversaryConditionStep::IDENTIFIER,
51+
'Anniversary Condition',
52+
-45 // Priority
53+
)
54+
);
55+
}
56+
57+
public function addStepDataToStruct(DiscountStructEventInterface $event): void
58+
{
59+
/** @var AnniversaryConditionStep $stepData */
60+
$stepData = $event
61+
->getData()
62+
->getStepByIdentifier(AnniversaryConditionStep::IDENTIFIER)?->getStepData();
63+
64+
if ($stepData === null || !$stepData->enabled) {
65+
return;
66+
}
67+
68+
$discountStruct = $event->getStruct();
69+
$discountStruct->addCondition(new IsAccountAnniversary($stepData->tolerance));
70+
}
71+
}

code_samples/discounts/src/Discounts/Step/CustomStepFormListener.php renamed to code_samples/discounts/src/Discounts/Step/AnniversaryConditionStepFormListener.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
namespace App\Discounts\Step;
44

5-
use App\Form\Type\CustomStepType;
5+
use App\Form\Type\AnniversaryConditionStepType;
66
use Ibexa\Contracts\Discounts\Admin\Form\Data\DiscountStepData;
77
use Ibexa\Contracts\Discounts\Admin\Form\Listener\AbstractStepFormListener;
88
use JMS\TranslationBundle\Model\Message;
99
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
1010
use Symfony\Component\Form\Event\PreSetDataEvent;
1111
use Symfony\Component\Form\FormInterface;
1212

13-
final class CustomStepFormListener extends AbstractStepFormListener implements TranslationContainerInterface
13+
final class AnniversaryConditionStepFormListener extends AbstractStepFormListener implements TranslationContainerInterface
1414
{
1515
public function isDataSupported(DiscountStepData $data): bool
1616
{
17-
return $data->getStepData() instanceof CustomStep;
17+
return $data->getStepData() instanceof AnniversaryConditionStep;
1818
}
1919

2020
public function addFields(FormInterface $form, DiscountStepData $data, PreSetDataEvent $event): void
2121
{
2222
$form->add(
2323
'stepData',
24-
CustomStepType::class,
24+
AnniversaryConditionStepType::class,
2525
[
2626
'label' => false,
2727
]

code_samples/discounts/src/Discounts/Step/CustomStep.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

code_samples/discounts/src/Discounts/Step/CustomStepEventSubscriber.php

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Form\Type;
6+
7+
use App\Discounts\Step\AnniversaryConditionStep;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
10+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
11+
use Symfony\Component\Form\FormBuilderInterface;
12+
use Symfony\Component\OptionsResolver\OptionsResolver;
13+
14+
/**
15+
* @extends \Symfony\Component\Form\AbstractType<AnniversaryConditionStep>
16+
*/
17+
final class AnniversaryConditionStepType extends AbstractType
18+
{
19+
public function buildForm(FormBuilderInterface $builder, array $options): void
20+
{
21+
$builder->add(
22+
'enabled',
23+
CheckboxType::class,
24+
[
25+
'label' => 'Enable anniversary discount',
26+
'required' => false,
27+
]
28+
)->add(
29+
'tolerance',
30+
NumberType::class,
31+
[
32+
'label' => 'Tolerance in days',
33+
'required' => false,
34+
]
35+
);
36+
}
37+
38+
public function configureOptions(OptionsResolver $resolver): void
39+
{
40+
$resolver->setDefaults([
41+
'data_class' => AnniversaryConditionStep::class,
42+
]);
43+
}
44+
}

code_samples/discounts/src/Form/Type/CustomStepType.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

docs/api/event_reference/discounts_events.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ The event below allows you to inject your custom logic before the discount code
7575

7676
| Event | Dispatched by | Description |
7777
|---|---|---|
78-
|[`BeforeDiscountCodeApplyEvent`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-Event-BeforeDiscountCodeApplyEvent.html)|`Ibexa\Bundle\DiscountsCodes\Controller\REST\DiscountCodeController`| Dispatched before a discount code is applied in the cart |
78+
|<nobr>[`BeforeDiscountCodeApplyEvent`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-DiscountsCodes-Event-BeforeDiscountCodeApplyEvent.html)</nobr>|`Ibexa\Bundle\DiscountsCodes\Controller\REST\DiscountCodeController`| Dispatched before a discount code is applied in the cart |

docs/discounts/extend_discounts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ Two new expressions are now available for use in custom conditions and rules.
8686

8787
### Implement custom condition
8888

89-
The following example creates a new discount condition. It allows you to offer a special discount for customers on the date when their account was created.
89+
The following example creates a new discount condition. It allows you to offer a special discount for customers on the date when their account was created, making use of the expressions added above.
9090

91-
The `tolerance` option allows you to make the discount usable for a slighlty longer period of time (for example, a day before or after the registration date) to allow more time for the customers to use it.
91+
The `tolerance` option allows you to make the discount usable for a longer period of time (for example, a day before or after the registration date) to allow more time for the customers to use it.
9292

9393
Create the condition by creating a class implementing the [`DiscountConditionInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Value-DiscountConditionInterface.html):
9494

0 commit comments

Comments
 (0)