Skip to content

Commit

Permalink
Ajoute la commande app:storage_area_generate
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Jan 23, 2025
1 parent 1d91c38 commit 9e911bb
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/Infrastructure/Symfony/Command/StorageAreaGenerateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Symfony\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'app:storage_area:generate',
description: 'Generate migration lines for data/aires_de_stockage.csv',
hidden: false,
)]
class StorageAreaGenerateCommand extends Command
{
public function __construct(
private readonly string $projectDir,
) {
parent::__construct();
}

private function parseCsvAssociative(string $csv): array
{
$lines = explode(PHP_EOL, trim($csv));
$fieldNames = str_getcsv($lines[0], ',');
$rows = [];

foreach ($lines as $line) {
$values = str_getcsv($line, ',');
$row = [];

foreach ($fieldNames as $index => $name) {
$row[$name] = $values[$index];
}

$rows[] = $row;
}

return $rows;
}

private function makePhp(array $rows): string
{
$lines = [];

$columns = [
'uuid',
'description',
'administrator',
'road_number',
'from_point_number',
'from_side',
'from_abscissa',
'to_point_number',
'to_side',
'to_abscissa',
'geometry',
];

$valuesList = [];

foreach ($rows as $row) {
$values = [
'uuid' => 'uuid_generate_v4()',
'description' => "''",
'administrator' => "''",
'road_number' => "''",
'from_point_number' => "'10'",
'from_side' => "'D'",
'from_abscissa' => '0',
'to_point_number' => "'11'",
'to_side' => "'D'",
'to_abscissa' => '0',
'geometry' => "ST_MakePoint('0 1')",
];

$valuesList[] = \sprintf(' (%s)', implode(', ', $values));
}

$statement = \sprintf(
'INSERT INTO storage_area (%s) VALUES%s%s',
implode(', ', $columns),
PHP_EOL,
implode(\sprintf(',%s', PHP_EOL), $valuesList),
);

$lines[] = \sprintf('$this->addSql(\'%s\');', $statement);

return implode(PHP_EOL, $lines);
}

public function execute(InputInterface $input, OutputInterface $output): int
{
$csv = file_get_contents(\sprintf('%s/data/aires_de_stockage.csv', $this->projectDir));
$rows = $this->parseCsvAssociative($csv);
$output->writeln($this->makePhp($rows));

return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Tests\Integration\Infrastructure\Symfony\Command;

use App\Infrastructure\Symfony\Command\StorageAreaGenerateCommand;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @group only
*/
final class StorageAreaGenerateCommandTest extends KernelTestCase
{
public function testExecute(): void
{
self::bootKernel();
$container = static::getContainer();

$command = $container->get(StorageAreaGenerateCommand::class);
$commandTester = new CommandTester($command);

$commandTester->execute([]);
$commandTester->assertCommandIsSuccessful();

$this->assertEmpty($commandTester->getDisplay());
}
}

0 comments on commit 9e911bb

Please sign in to comment.