From 8a45c69cf4b3f71556e1e6411bddbbd48fda6a67 Mon Sep 17 00:00:00 2001 From: Kentaro Ohkouchi Date: Mon, 16 Aug 2021 17:57:42 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=8A=E3=81=99=E3=81=99=E3=82=81=E5=95=86?= =?UTF-8?q?=E5=93=81=E3=81=AE=E3=83=80=E3=83=9F=E3=83=BC=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E7=94=9F=E6=88=90=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Command/GenerateDummyDataCommand.php | 116 +++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Command/GenerateDummyDataCommand.php diff --git a/Command/GenerateDummyDataCommand.php b/Command/GenerateDummyDataCommand.php new file mode 100644 index 0000000..bbebe17 --- /dev/null +++ b/Command/GenerateDummyDataCommand.php @@ -0,0 +1,116 @@ +entityManager = $entityManager; + $this->recommendService = $recommendService; + } + + protected function configure() + { + $this + ->setDescription('Dummy data generator') + ->addOption('with-locale', null, InputOption::VALUE_REQUIRED, 'Set to the locale.', 'ja_JP') + ->addOption('recommendproducts', null, InputOption::VALUE_REQUIRED, 'Number of Recommend Products.', 10) + ->setHelp(<<%command.name% command generate of dummy data. + + php %command.full_name% +; +EOF + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $locale = $input->getOption('with-locale'); + $numberOfProducts = $input->getOption('recommendproducts'); + + $faker = Faker::create($locale); + /** @var Product[] $Products */ + $Products = $this->createQueryBuilder($numberOfProducts)->getQuery()->getResult(); + + foreach ($Products as $Product) { + // @see https://github.com/fzaninotto/Faker/issues/1125#issuecomment-268676186 + gc_collect_cycles(); + switch ($output->getVerbosity()) { + case OutputInterface::VERBOSITY_QUIET: + break; + case OutputInterface::VERBOSITY_NORMAL: + $output->write('R'); + break; + case OutputInterface::VERBOSITY_VERBOSE: + case OutputInterface::VERBOSITY_VERY_VERBOSE: + case OutputInterface::VERBOSITY_DEBUG: + $output->writeln('Recommend Product: id='.$Product->getId().' '.$Product->getName().' '); + break; + } + + $this->recommendService->createRecommend([ + 'Product' => $Product, + 'comment' => $faker->paragraph() + ]); + } + } + + /** + * @param int|null $limit + * + * @return QueryBuilder + */ + private function createQueryBuilder($limit = null) + { + /** @var QueryBuilder $qb */ + $qb = $this->entityManager->getRepository(Product::class) + ->createQueryBuilder('p'); + + $qb->where('p.Status in (:Status)') + ->setParameter('Status', [ProductStatus::DISPLAY_SHOW, ProductStatus::DISPLAY_HIDE]) + ->setMaxResults($limit); + + return $qb; + } +}