src/EventSubscriber/Taxonomy/TaxonResourceSubscriber.php line 31

  1. <?php
  2. namespace App\EventSubscriber\Taxonomy;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. use Webmozart\Assert\Assert;
  7. use Sylius\Component\Customer\Model\CustomerInterface;
  8. use App\Repository\Customer\CustomerRepository;
  9. use App\Entity\Channel\ChannelEcodaAccountInfo
  10. use App\Repository\Channel\ChannelEcodaAccountInfoRepository;
  11. use App\Repository\Channel\ChannelEcodaAccountTypeRepository;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use App\Manager\EcodaAccountManager;
  16. use App\Entity\Taxon\TaxonVariant;
  17. use App\Entity\Taxonomy\Taxon;
  18. use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
  19. class TaxonResourceSubscriber  implements EventSubscriberInterface
  20.     private $taxonRepository;
  21.      
  22.     public function __construct(RequestStack $requestTaxonRepositoryInterface $taxonRepository)
  23.     { 
  24.         $this->taxonRepository $taxonRepository;
  25.     }
  26.     public function onPreCreate(GenericEvent $event)
  27.     {  
  28.         $entity $event->getSubject();  
  29.         Assert::isInstanceOf($entityTaxon::class);  
  30.         $code null;
  31.         $words explode(' ',$entity->getName());
  32.         foreach ($words as $word) {
  33.             $code $code.substr($word01);
  34.         }  
  35.         $i 1
  36.         while ($this->taxonRepository->findOneBy(['code'=> strtolower($code.'-'.$i)])) {
  37.             $i++; 
  38.         }
  39.         $code =  strtolower($code.'-'.$i);
  40.         $entity->setCode($code);
  41.     } 
  42.      
  43.  
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.            'sylius.taxon.pre_create' => 'onPreCreate',  
  48.         ];
  49.     }
  50. }