add fake data generator
All checks were successful
/ ls (pull_request) Successful in 31s
/ ls (push) Successful in 30s

This commit is contained in:
Jonas 2024-06-14 19:08:55 +02:00
parent b18ee3cbd5
commit 63bf367ab0
Signed by: lubiana
SSH key fingerprint: SHA256:gkqM8DUX4Blf6P52fycW8ISTd+4eAHH+Uzu9iyc8hAM
2 changed files with 86 additions and 49 deletions

View file

@ -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;
}
}

81
src/Service/FakeData.php Normal file
View 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;
}
}