diff --git a/src/Command/FakeDataCommand.php b/src/Command/FakeDataCommand.php index 82e1fd6..fce35fc 100644 --- a/src/Command/FakeDataCommand.php +++ b/src/Command/FakeDataCommand.php @@ -2,9 +2,7 @@ namespace App\Command; -use App\Entity\FoodOrder; -use App\Entity\FoodVendor; -use Doctrine\ORM\EntityManagerInterface; +use App\Service\FakeData; use Override; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -12,17 +10,14 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -use function range; -use function sprintf; - #[AsCommand( name: 'app:fake-data', - description: 'Add a short description for your command', + description: 'add some fake data to database', )] final class FakeDataCommand extends Command { public function __construct( - private readonly EntityManagerInterface $entityManager, + private readonly FakeData $fakeData, ) { parent::__construct(); } @@ -31,47 +26,8 @@ final class FakeDataCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - $arg1 = $input->getArgument('arg1'); - - if ($arg1) { - $io->note(sprintf('You passed an argument: %s', $arg1)); - } - - if ($input->getOption('option1')) { - // ... - } - - $io->success('You have a new command! Now make it your own! Pass --help to see your options.'); - + $this->fakeData->generate(); + $io->success('Added some fake data to database'); return Command::SUCCESS; } - - /** - * @return FoodVendor[] - */ - public function generateVendors(int $amount = 10): array - { - $vendors = []; - foreach (range(1, $amount) as $i) { - $vendor = new FoodVendor; - $vendor->setName('Food Vendor ' . $i); - $this->entityManager->persist($vendor); - $vendors[] = $vendor; - } - return $vendors; - } - - public function generateOrdersForVendor(FoodVendor $vendor, int $amount = 10): array - { - $orders = []; - foreach (range(1, $amount) as $i) { - $order = new FoodOrder; - $order->setFoodVendor($vendor); - if ($i % 2 === 0) { - $order->close(); - } - $orders[] = $order; - } - return $orders; - } } diff --git a/src/Service/FakeData.php b/src/Service/FakeData.php new file mode 100644 index 0000000..3504976 --- /dev/null +++ b/src/Service/FakeData.php @@ -0,0 +1,81 @@ +generateVendors(); + foreach ($vendors as $vendor) { + $orders = $this->generateOrdersForVendor($vendor); + foreach ($orders as $order) { + $this->generateItemsForOrder($order); + } + } + $this->entityManager->flush(); + } + + /** + * @return FoodVendor[] + */ + public function generateVendors(int $amount = 10): array + { + $vendors = []; + foreach (range(1, $amount) as $i) { + $vendor = new FoodVendor; + $vendor->setName('Food Vendor ' . $i); + $this->entityManager->persist($vendor); + $vendors[] = $vendor; + } + return $vendors; + } + + /** + * @return FoodOrder[] + */ + public function generateOrdersForVendor(FoodVendor $vendor, int $amount = 10): array + { + $orders = []; + foreach (range(1, $amount) as $i) { + $order = new FoodOrder; + $order->setFoodVendor($vendor); + if ($i % 2 === 0) { + $order->close(); + } + $this->entityManager->persist($order); + $orders[] = $order; + } + return $orders; + } + + /** + * @return OrderItem[] + */ + public function generateItemsForOrder(FoodOrder $order, int $amount = 10): array + { + $items = []; + foreach (range(1, $amount) as $i) { + $item = new OrderItem; + $item->setName('Item ' . $i); + $item->setFoodOrder($order); + if ($i % 2 === 0) { + $item->setExtras('Extra ' . $i); + } + $this->entityManager->persist($item); + $items[] = $item; + } + return $items; + } +}