custom/plugins/WbfkExtensions/src/Subscriber/AddTradeLicenseValidationForCustomer.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  5. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  8. use Symfony\Component\Validator\Constraints\File;
  9. class AddTradeLicenseValidationForCustomer implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             'kernel.controller_arguments' => 'addTradeLicenseToRequestDataBag',
  15.             'framework.validation.customer.create' => 'addTradeLicenseValidation',
  16.             'framework.validation.customer.update' => 'addTradeLicenseValidation',
  17.             'framework.validation.customer.profile.update' => 'addTradeLicenseValidation',
  18.         ];
  19.     }
  20.     public function addTradeLicenseToRequestDataBag(ControllerArgumentsEvent $event): void
  21.     {
  22.         // if current route is frontend.account.register.save
  23.         if (
  24.             !in_array(
  25.                 $event->getRequest()->get('_route'),
  26.                 [
  27.                     'frontend.account.register.save',
  28.                     'frontend.account.profile.save',
  29.                 ]
  30.             )) {
  31.             return;
  32.         }
  33.         $requestDataBag $this->findRequestDataBag($event);
  34.         if (!$requestDataBag) {
  35.             return;
  36.         }
  37.         // Add tradeLicense to request data bag
  38.         $requestDataBag->add(['tradeLicense' => $event->getRequest()->files->get('tradeLicense')]);
  39.     }
  40.     public function addTradeLicenseValidation(BuildValidationEvent $event): void
  41.     {
  42.         $definition $event->getDefinition();
  43.         $definition->add(
  44.             'tradeLicense',
  45.             new File([
  46.                 'maxSize' => '5M',
  47.                 'mimeTypes' => [
  48.                     'application/pdf',
  49.                     'image/png',
  50.                     'image/jpg',
  51.                     'image/jpeg',
  52.                 ],
  53.                 'mimeTypesMessage' => 'Please upload a valid PDF, PNG, JPG AND JPEG',
  54.             ])
  55.         );
  56.     }
  57.     private function findRequestDataBag(ControllerArgumentsEvent $event): ?RequestDataBag
  58.     {
  59.         $controllerArguments $event->getArguments();
  60.         foreach ($controllerArguments as $controllerArgument) {
  61.             if ($controllerArgument instanceof RequestDataBag) {
  62.                 return $controllerArgument;
  63.             }
  64.         }
  65.         return null;
  66.     }
  67. }