cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,24 +1,32 @@
<?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.
*/
namespace Chill\PersonBundle\Command;
use Chill\PersonBundle\Actions\Remove\PersonMove;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Chill\PersonBundle\Actions\Remove\PersonMove;
use Doctrine\ORM\EntityManagerInterface;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Console\Exception\RuntimeException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use function ctype_digit;
final class ChillPersonMoveCommand extends Command
{
private PersonMove $mover;
private LoggerInterface $chillLogger;
private EntityManagerInterface $em;
private LoggerInterface $chillLogger;
private PersonMove $mover;
public function __construct(
PersonMove $mover,
@@ -32,64 +40,64 @@ final class ChillPersonMoveCommand extends Command
$this->chillLogger = $chillLogger;
}
protected function buildLoggingContext(Person $from, Person $to, $deleteEntities, $sqls)
{
$ctxt = [
'from' => $from->getId(),
'to' => $to->getId(),
];
foreach ($deleteEntities as $key => $de) {
$ctxt['delete_entity_' . $key] = $de;
}
foreach ($sqls as $key => $sql) {
$ctxt['sql_' . $key] = $sql;
}
return $ctxt;
}
protected function configure()
{
$this
->setName('chill:person:move')
->setDescription('Move all the associated entities on a "from" person to a "to" person and remove the old person')
->addOption('from', 'f', InputOption::VALUE_REQUIRED, "The person id to delete, all associated data will be moved before")
->addOption('to', 't', InputOption::VALUE_REQUIRED, "The person id which will received data")
->addOption('dump-sql', null, InputOption::VALUE_NONE, "dump sql to stdout")
->addOption('force', null, InputOption::VALUE_NONE, "execute sql instead of dumping it")
->addOption('delete-entity', null, InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, "entity to delete", [])
;
}
protected function interact(InputInterface $input, OutputInterface $output)
{
if (FALSE === $input->hasOption('dump-sql') && FALSE === $input->hasOption('force')) {
$msg = "You must use \"--dump-sql\" or \"--force\"";
throw new RuntimeException($msg);
}
foreach (["from", "to"] as $name) {
if (empty($input->getOption($name))) {
throw new RuntimeException("You must set a \"$name\" option");
}
$id = $input->getOption($name);
if (\ctype_digit($id) === FALSE) {
throw new RuntimeException("The id in \"$name\" field does not contains "
. "only digits: $id");
}
}
->addOption('from', 'f', InputOption::VALUE_REQUIRED, 'The person id to delete, all associated data will be moved before')
->addOption('to', 't', InputOption::VALUE_REQUIRED, 'The person id which will received data')
->addOption('dump-sql', null, InputOption::VALUE_NONE, 'dump sql to stdout')
->addOption('force', null, InputOption::VALUE_NONE, 'execute sql instead of dumping it')
->addOption('delete-entity', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'entity to delete', []);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$repository = $this->em->getRepository(Person::class);
$from = $repository->find($input->getOption('from'));
$to = $repository->find($input->getOption('to'));
$to = $repository->find($input->getOption('to'));
$deleteEntities = $input->getOption('delete-entity');
if ($from === NULL) {
throw new RuntimeException(sprintf("Person \"from\" with id %d not found", $input->getOption('from')));
if (null === $from) {
throw new RuntimeException(sprintf('Person "from" with id %d not found', $input->getOption('from')));
}
if ($to === NULL) {
throw new RuntimeException(sprintf("Person \"to\" with id %d not found", $input->getOption('to')));
if (null === $to) {
throw new RuntimeException(sprintf('Person "to" with id %d not found', $input->getOption('to')));
}
$sqls = $this->mover->getSQL($from, $to, $deleteEntities);
if ($input->getOption('dump-sql')) {
foreach($sqls as $sql) {
foreach ($sqls as $sql) {
$output->writeln($sql);
}
} else {
$ctxt = $this->buildLoggingContext($from, $to, $deleteEntities, $sqls);
$this->chillLogger->notice("Trying to move a person from command line", $ctxt);
$this->chillLogger->notice('Trying to move a person from command line', $ctxt);
$connection = $this->em->getConnection();
$connection->beginTransaction();
foreach($sqls as $sql) {
foreach ($sqls as $sql) {
if ($output->isVerbose()) {
$output->writeln($sql);
}
@@ -97,25 +105,28 @@ final class ChillPersonMoveCommand extends Command
}
$connection->commit();
$this->chillLogger->notice("Move a person from command line succeeded", $ctxt);
$this->chillLogger->notice('Move a person from command line succeeded', $ctxt);
}
}
protected function buildLoggingContext(Person $from, Person $to, $deleteEntities, $sqls)
protected function interact(InputInterface $input, OutputInterface $output)
{
$ctxt = [
'from' => $from->getId(),
'to' => $to->getId()
];
if (false === $input->hasOption('dump-sql') && false === $input->hasOption('force')) {
$msg = 'You must use "--dump-sql" or "--force"';
foreach ($deleteEntities as $key => $de) {
$ctxt['delete_entity_'.$key] = $de;
}
foreach ($sqls as $key => $sql) {
$ctxt['sql_'.$key] = $sql;
throw new RuntimeException($msg);
}
return $ctxt;
foreach (['from', 'to'] as $name) {
if (empty($input->getOption($name))) {
throw new RuntimeException("You must set a \"{$name}\" option");
}
$id = $input->getOption($name);
if (ctype_digit($id) === false) {
throw new RuntimeException("The id in \"{$name}\" field does not contains "
. "only digits: {$id}");
}
}
}
}

View File

@@ -1,26 +1,30 @@
<?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 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 Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
final class ImportSocialWorkMetadata extends Command
{
/**
* @var EntityManagerInterface
*/
protected EntityManagerInterface $em;
/**
@@ -51,13 +55,13 @@ final class ImportSocialWorkMetadata extends Command
try {
$csv = Reader::createFromPath($filepath);
} catch (Throwable $e) {
throw new Exception('Error while loading CSV.',0, $e);
throw new Exception('Error while loading CSV.', 0, $e);
}
$csv->setDelimiter(';');
return true === $this->importer->import($csv) ?
0:
0 :
1;
}
}