mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-07 15:25:00 +00:00
apply more cs rules for php-cs
This commit is contained in:
@@ -15,8 +15,6 @@ use Chill\MainBundle\Doctrine\Model\Point;
|
||||
use Chill\MainBundle\Entity\Country;
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@@ -25,9 +23,6 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
use function count;
|
||||
use function strlen;
|
||||
|
||||
class LoadPostalCodesCommand extends Command
|
||||
{
|
||||
public function __construct(private readonly EntityManagerInterface $entityManager, private readonly ValidatorInterface $validator)
|
||||
@@ -40,15 +35,15 @@ class LoadPostalCodesCommand extends Command
|
||||
$this->setName('chill:main:postal-code:populate')
|
||||
->setDescription('Add the postal code from a csv file.')
|
||||
->setHelp('This script will try to avoid existing postal code '
|
||||
. "using the postal code and name. \n"
|
||||
. 'The CSV file must have the following columns: '
|
||||
. 'postal code, label, country code.'
|
||||
. 'Optionally, the csv file can have the following '
|
||||
. 'columns after the country code: reference code, latitude, longitude, source. '
|
||||
. 'The latitude and longitude columns are supposed to be in WGS84 and expressed in decimal degrees. '
|
||||
. 'The CSV file should not have any header row.')
|
||||
."using the postal code and name. \n"
|
||||
.'The CSV file must have the following columns: '
|
||||
.'postal code, label, country code.'
|
||||
.'Optionally, the csv file can have the following '
|
||||
.'columns after the country code: reference code, latitude, longitude, source. '
|
||||
.'The latitude and longitude columns are supposed to be in WGS84 and expressed in decimal degrees. '
|
||||
.'The CSV file should not have any header row.')
|
||||
->addArgument('csv_file', InputArgument::REQUIRED, 'the path to '
|
||||
. 'the csv file. See the help for specifications.')
|
||||
.'the csv file. See the help for specifications.')
|
||||
->addOption(
|
||||
'delimiter',
|
||||
'd',
|
||||
@@ -76,7 +71,7 @@ class LoadPostalCodesCommand extends Command
|
||||
{
|
||||
$csv = $this->getCSVResource($input);
|
||||
|
||||
if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERY_VERBOSE) {
|
||||
if (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
|
||||
$output->writeln('The content of the file is ...');
|
||||
$output->write(file_get_contents($input->getArgument('csv_file')));
|
||||
}
|
||||
@@ -97,28 +92,29 @@ class LoadPostalCodesCommand extends Command
|
||||
$this->addPostalCode($row, $output);
|
||||
++$num;
|
||||
} catch (CountryCodeNotFoundException|ExistingPostalCodeException|PostalCodeNotValidException $ex) {
|
||||
$output->writeln('<warning> on line ' . $line . ' : ' . $ex->getMessage() . '</warning>');
|
||||
$output->writeln('<warning> on line '.$line.' : '.$ex->getMessage().'</warning>');
|
||||
}
|
||||
++$line;
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$output->writeln('<info>' . $num . ' were added !</info>');
|
||||
$output->writeln('<info>'.$num.' were added !</info>');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function addPostalCode($row, OutputInterface $output)
|
||||
{
|
||||
if ('FR' === $row[2] && strlen((string) $row[0]) === 4) {
|
||||
if ('FR' === $row[2] && 4 === \strlen((string) $row[0])) {
|
||||
// CP in FRANCE are on 5 digit
|
||||
// For CP starting with a zero, the starting zero can be remove if stored as number in a csv
|
||||
// add a zero if CP from FR and on 4 digit
|
||||
$row[0] = '0' . $row[0];
|
||||
$row[0] = '0'.$row[0];
|
||||
}
|
||||
|
||||
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
||||
$output->writeln('handling row: ' . $row[0] . ' | ' . $row[1] . ' | ' . $row[2]);
|
||||
$output->writeln('handling row: '.$row[0].' | '.$row[1].' | '.$row[2]);
|
||||
}
|
||||
$em = $this->entityManager;
|
||||
$country = $em
|
||||
@@ -126,12 +122,7 @@ class LoadPostalCodesCommand extends Command
|
||||
->findOneBy(['countryCode' => $row[2]]);
|
||||
|
||||
if (null === $country) {
|
||||
throw new CountryCodeNotFoundException(sprintf(
|
||||
'The country with code %s is not found. Aborting to insert postal code with %s - %s',
|
||||
$row[2],
|
||||
$row[0],
|
||||
$row[1]
|
||||
));
|
||||
throw new CountryCodeNotFoundException(sprintf('The country with code %s is not found. Aborting to insert postal code with %s - %s', $row[2], $row[0], $row[1]));
|
||||
}
|
||||
|
||||
// try to find an existing postal code
|
||||
@@ -139,12 +130,8 @@ class LoadPostalCodesCommand extends Command
|
||||
->getRepository(PostalCode::class)
|
||||
->findBy(['code' => $row[0], 'name' => $row[1]]);
|
||||
|
||||
if (count($existingPC) > 0) {
|
||||
throw new ExistingPostalCodeException(sprintf(
|
||||
'A postal code with code : %s and name : %s already exists, skipping',
|
||||
$row[0],
|
||||
$row[1]
|
||||
));
|
||||
if (\count($existingPC) > 0) {
|
||||
throw new ExistingPostalCodeException(sprintf('A postal code with code : %s and name : %s already exists, skipping', $row[0], $row[1]));
|
||||
}
|
||||
|
||||
$postalCode = (new PostalCode())
|
||||
@@ -166,13 +153,13 @@ class LoadPostalCodesCommand extends Command
|
||||
|
||||
$errors = $this->validator->validate($postalCode);
|
||||
|
||||
if ($errors->count() === 0) {
|
||||
if (0 === $errors->count()) {
|
||||
$em->persist($postalCode);
|
||||
} else {
|
||||
$msg = '';
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$msg .= ' ' . $error->getMessage();
|
||||
$msg .= ' '.$error->getMessage();
|
||||
}
|
||||
|
||||
throw new PostalCodeNotValidException($msg);
|
||||
@@ -194,22 +181,21 @@ class LoadPostalCodesCommand extends Command
|
||||
$filename = $input->getArgument('csv_file');
|
||||
|
||||
if (!$fs->exists($filename)) {
|
||||
throw new RuntimeException('The file does not exists or you do not '
|
||||
. 'have the right to read it.');
|
||||
throw new \RuntimeException('The file does not exists or you do not have the right to read it.');
|
||||
}
|
||||
|
||||
$resource = fopen($filename, 'rb');
|
||||
|
||||
if (false === $resource) {
|
||||
throw new RuntimeException("The file '{$filename}' could not be opened.");
|
||||
throw new \RuntimeException("The file '{$filename}' could not be opened.");
|
||||
}
|
||||
|
||||
return $resource;
|
||||
}
|
||||
}
|
||||
|
||||
class ExistingPostalCodeException extends Exception {}
|
||||
class ExistingPostalCodeException extends \Exception {}
|
||||
|
||||
class CountryCodeNotFoundException extends Exception {}
|
||||
class CountryCodeNotFoundException extends \Exception {}
|
||||
|
||||
class PostalCodeNotValidException extends Exception {}
|
||||
class PostalCodeNotValidException extends \Exception {}
|
||||
|
Reference in New Issue
Block a user