custom/plugins/ProcWegReg/src/Subscriber/CustomerGroupApprovalSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Proc\ProcWegReg\Subscriber;
  3. use Proc\ProcWegReg\Migration\Migration1718877324CustomFieldApproval;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerGroupRegistrationAccepted;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerGroupRegistrationDeclined;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\User\UserEntity;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. class CustomerGroupApprovalSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private readonly EntityRepository $customerRepository,
  16.         private readonly EntityRepository $userRepository,
  17.         private readonly RequestStack $requestStack
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             CustomerGroupRegistrationAccepted::class => ["onRegistrationAccepted"1000],
  24.             CustomerGroupRegistrationDeclined::class => ["onRegistrationDeclined"1000]
  25.         ];
  26.     }
  27.     public function onRegistrationAccepted(CustomerGroupRegistrationAccepted $event): void
  28.     {
  29.         $this->updateCustomerField($event->getCustomer()->getId(), true$event->getContext());
  30.     }
  31.     public function onRegistrationDeclined(CustomerGroupRegistrationDeclined $event): void
  32.     {
  33.         $this->updateCustomerField($event->getCustomer()->getId(), false$event->getContext());
  34.     }
  35.     private function updateCustomerField(string $idbool $acceptedContext $context): void
  36.     {
  37.         $user_id $this->requestStack->getCurrentRequest()?->get("oauth_user_id""");
  38.         $status $accepted "Accepted" "Declined";
  39.         if ($username $this->getUsername($user_id$context)) {
  40.             $this->customerRepository->upsert([
  41.                 [
  42.                     "id" => $id,
  43.                     "customFields" => [Migration1718877324CustomFieldApproval::CUSTOM_FIELD_NAME => "$username - $status"]
  44.                 ]
  45.             ], $context);
  46.         }
  47.     }
  48.     private function getUsername(string $idContext $context): string|null
  49.     {
  50.         /** @var UserEntity $user */
  51.         $user $this->userRepository->search(new Criteria([$id]), $context)->first();
  52.         return $user?->getUsername();
  53.     }
  54. }