src/Command/VariantsMakeGiftableCommand.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Command;
  3. use App\Entity\Gos\ProductVariant;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Style\SymfonyStyle;
  9. class VariantsMakeGiftableCommand extends Command
  10. {
  11.     private $em;
  12.     private $countriesRequired = ['chile''peru''mexico''paraguay''argentina''uruguay''panama''ecuador''spain''colombia'];
  13.     public function __construct(EntityManagerInterface $em)
  14.     {
  15.         $this->em $em;
  16.         parent::__construct();
  17.     }
  18.     protected function configure(): void
  19.     {
  20.         $this
  21.             ->setName('variants:make-giftable')
  22.             ->setDescription('Makes active UniqSkills product variants giftable');
  23.     }
  24.     protected function execute(InputInterface $inputOutputInterface $output): int
  25.     {
  26.         $io = new SymfonyStyle($input$output);
  27.         $io->comment('Making product variants giftable...');
  28.         $result $this->makeVariantsGiftable();
  29.         $io->success("Success! ${result} product variants made giftable");
  30.         return Command::SUCCESS;
  31.     }
  32.     private function makeVariantsGiftable(): int
  33.     {
  34.         $productVariants $this->em->getRepository(ProductVariant::class)
  35.             ->findAllEligibleToMakeGiftable($this->countriesRequired);
  36.         foreach ($productVariants as $productVariant)
  37.         {
  38.             if ($productVariant->isActive())
  39.             {
  40.                 $productVariant->setIsGiftable(true);
  41.             }
  42.         }
  43.         $this->em->flush();
  44.         return count($productVariants);
  45.     }
  46. }