Skip to content

Commit

Permalink
feat: set sponsor on registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadrien Lucas committed Jun 20, 2023
1 parent 06c19b9 commit 0872d40
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Form/CustomerSponsorshipRegistrationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@

namespace Acme\SyliusSponsorshipPlugin\Form;

use App\Entity\User\ShopUser;
use Sylius\Bundle\CoreBundle\Form\Type\Customer\CustomerRegistrationType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Component\Core\Model\Customer;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

final class CustomerSponsorshipRegistrationType extends AbstractResourceType
{
public function __construct(
protected string $dataClass,
protected array $validationGroups = [],
protected string $repositoryName,
private UserRepositoryInterface $shopUserRepository
)
{
parent::__construct($dataClass, $validationGroups);
}
public function buildForm(FormBuilderInterface $builder, array $options = []): void
{
parent::buildForm($builder, $options);
Expand All @@ -19,6 +33,20 @@ public function buildForm(FormBuilderInterface $builder, array $options = []): v
'label' => 'sylius.form.customer.first_name',
'mapped' => false,
])
->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
/** @var Customer $customer */
$customer = $event->getData();
/** @var ShopUser $user */
$user = $customer->getUser();

$sponsorCode = $form->get('sponsor')->get('code')->getData();

if ($sponsorCode !== null) {
$sponsor = $this->shopUserRepository->findOneBy(["sponsorshipCoupon" => $sponsorCode]);
$user->setSponsorshipSponsor($sponsor);
}
})
;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Form/SponsorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Acme\SyliusSponsorshipPlugin\Form;

use Acme\SyliusSponsorshipPlugin\Form\Constraints\SponsorshipCouponConstraint;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -19,6 +20,11 @@ public function buildForm(FormBuilderInterface $builder, array $options = []): v
'label' => 'sylius_sponsorship_plugin.ui.coupon',
'mapped' => false,
'required' => false,
'constraints' => [
new SponsorshipCouponConstraint(
['sylius'],
),
],
]);
}

Expand Down

0 comments on commit 0872d40

Please sign in to comment.