add fake data generator
This commit is contained in:
parent
b18ee3cbd5
commit
63bf367ab0
2 changed files with 86 additions and 49 deletions
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
namespace App\Command;
|
namespace App\Command;
|
||||||
|
|
||||||
use App\Entity\FoodOrder;
|
use App\Service\FakeData;
|
||||||
use App\Entity\FoodVendor;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use Override;
|
use Override;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
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\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
use function range;
|
|
||||||
use function sprintf;
|
|
||||||
|
|
||||||
#[AsCommand(
|
#[AsCommand(
|
||||||
name: 'app:fake-data',
|
name: 'app:fake-data',
|
||||||
description: 'Add a short description for your command',
|
description: 'add some fake data to database',
|
||||||
)]
|
)]
|
||||||
final class FakeDataCommand extends Command
|
final class FakeDataCommand extends Command
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly EntityManagerInterface $entityManager,
|
private readonly FakeData $fakeData,
|
||||||
) {
|
) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
@ -31,47 +26,8 @@ final class FakeDataCommand extends Command
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$io = new SymfonyStyle($input, $output);
|
||||||
$arg1 = $input->getArgument('arg1');
|
$this->fakeData->generate();
|
||||||
|
$io->success('Added some fake data to database');
|
||||||
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.');
|
|
||||||
|
|
||||||
return Command::SUCCESS;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
81
src/Service/FakeData.php
Normal file
81
src/Service/FakeData.php
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Entity\FoodOrder;
|
||||||
|
use App\Entity\FoodVendor;
|
||||||
|
use App\Entity\OrderItem;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
use function range;
|
||||||
|
|
||||||
|
final readonly class FakeData
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private EntityManagerInterface $entityManager
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function generate(): void
|
||||||
|
{
|
||||||
|
$vendors = $this->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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue