feat: Add new command.

This commit is contained in:
Pol Dellaiera
2021-06-22 15:40:49 +02:00
parent 6e37e7feac
commit 141aabcddc
5 changed files with 377 additions and 1 deletions

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Command;
use Chill\PersonBundle\Service\Import\ChillImporter;
use Chill\PersonBundle\Service\Import\SocialWorkMetadataInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use League\Csv\Reader;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Throwable;
final class ImportSocialWorkMetadata extends Command
{
/**
* @var EntityManagerInterface
*/
protected EntityManagerInterface $em;
/**
* @var LoggerInterface
*/
protected ChillImporter $importer;
public function __construct(
SocialWorkMetadataInterface $socialWorkMetadata
) {
parent::__construct('chill:person:import-socialwork');
$this->importer = $socialWorkMetadata;
}
protected function configure()
{
$this
->setName('chill:person:import-socialwork')
->addOption('filepath', 'f', InputOption::VALUE_REQUIRED, 'The file to import.')
->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'The default language');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$filepath = $input->getOption('filepath');
try {
$csv = Reader::createFromPath($filepath);
} catch (Throwable $e) {
throw new Exception('Error while loading CSV.',0, $e);
}
$csv->setDelimiter(';');
return true === $this->importer->import($csv) ?
0:
1;
}
}