chill-bundles/src/Bundle/ChillPersonBundle/Command/ImportSocialWorkMetadata.php
2021-11-23 14:08:50 +01:00

68 lines
1.8 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Command;
use Chill\PersonBundle\Service\Import\ChillImporter;
use Chill\PersonBundle\Service\Import\SocialWorkMetadataInterface;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use League\Csv\Reader;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
final class ImportSocialWorkMetadata extends Command
{
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;
}
}