feat : first commit
This commit is contained in:
103
src/Command/SeedEmployeesCommand.php
Normal file
103
src/Command/SeedEmployeesCommand.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Employee;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:seed:employees',
|
||||
description: 'Seed employees (idempotent).'
|
||||
)]
|
||||
class SeedEmployeesCommand extends Command
|
||||
{
|
||||
private int $created = 0;
|
||||
private int $updated = 0;
|
||||
|
||||
public function __construct(private readonly EntityManagerInterface $entityManager)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$this->created = 0;
|
||||
$this->updated = 0;
|
||||
|
||||
$employees = [
|
||||
['lastName' => 'LIOT', 'firstName' => 'Geoffrey'],
|
||||
['lastName' => 'ARCHAMBAULT', 'firstName' => 'Emilie'],
|
||||
['lastName' => 'SARAZI', 'firstName' => 'Olivier'],
|
||||
['lastName' => 'SOURIAU', 'firstName' => 'Elodie'],
|
||||
['lastName' => 'BRUYERE', 'firstName' => 'Aurore'],
|
||||
['lastName' => 'PANGALLO', 'firstName' => 'Julie'],
|
||||
['lastName' => 'BACHELIER', 'firstName' => 'Delphine'],
|
||||
['lastName' => 'SAILLY', 'firstName' => 'Marine'],
|
||||
['lastName' => 'KUPCZYK', 'firstName' => 'Corinne'],
|
||||
['lastName' => 'ODUNCU', 'firstName' => 'Kemal'],
|
||||
['lastName' => 'PROUTEAU', 'firstName' => 'Damien'],
|
||||
['lastName' => 'COURLIVANT', 'firstName' => 'Franck'],
|
||||
['lastName' => 'GUILLOT', 'firstName' => 'Damien'],
|
||||
['lastName' => 'GELINET', 'firstName' => 'Alexandre'],
|
||||
['lastName' => 'ASSANI', 'firstName' => 'Chafainna'],
|
||||
['lastName' => 'BRIQUET', 'firstName' => 'Alain'],
|
||||
['lastName' => 'BOULOIZEAU', 'firstName' => 'Gwenael'],
|
||||
['lastName' => 'MENANTEAU', 'firstName' => 'Dimitri'],
|
||||
['lastName' => 'ROBERT', 'firstName' => 'Remi'],
|
||||
['lastName' => 'COUTEAU', 'firstName' => 'Johnny'],
|
||||
['lastName' => 'MAISSANT', 'firstName' => 'Guillaume'],
|
||||
['lastName' => 'GARRAUD', 'firstName' => 'Nadia'],
|
||||
['lastName' => 'CRUBILE', 'firstName' => 'Franck'],
|
||||
['lastName' => 'ALMAGRO', 'firstName' => 'Antonio'],
|
||||
['lastName' => 'MARCHAL SANCHEZ', 'firstName' => 'Jose'],
|
||||
['lastName' => 'LOISEAU', 'firstName' => 'Nicolas'],
|
||||
['lastName' => 'BRAAK', 'firstName' => 'Estelle'],
|
||||
['lastName' => 'RODRIGUES MENDES', 'firstName' => 'Hugo'],
|
||||
['lastName' => 'RICAUD', 'firstName' => 'Jason'],
|
||||
['lastName' => 'LECOINT', 'firstName' => 'Jerome'],
|
||||
['lastName' => 'DALEMBA', 'firstName' => 'Ewa'],
|
||||
];
|
||||
|
||||
foreach ($employees as $data) {
|
||||
$this->upsertEmployee($data['firstName'], $data['lastName']);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$io->success(sprintf('Seed completed: %d created, %d updated.', $this->created, $this->updated));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function upsertEmployee(string $firstName, string $lastName): void
|
||||
{
|
||||
$repo = $this->entityManager->getRepository(Employee::class);
|
||||
$employee = $repo->findOneBy([
|
||||
'firstName' => $firstName,
|
||||
'lastName' => $lastName,
|
||||
]);
|
||||
|
||||
if (!$employee) {
|
||||
$employee = new Employee();
|
||||
++$this->created;
|
||||
} else {
|
||||
++$this->updated;
|
||||
}
|
||||
|
||||
$employee
|
||||
->setFirstName($firstName)
|
||||
->setLastName($lastName)
|
||||
;
|
||||
|
||||
$this->entityManager->persist($employee);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user