mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
fix: SA: Fix "might not be defined" rule.
SA stands for Static Analysis.
This commit is contained in:
@@ -1,27 +1,13 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Command;
|
||||
|
||||
use Chill\MainBundle\Doctrine\Model\Point;
|
||||
use Chill\MainBundle\Entity\Country;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -31,38 +17,19 @@ use Symfony\Component\Filesystem\Filesystem;
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* Class LoadPostalCodesCommand
|
||||
*
|
||||
* @package Chill\MainBundle\Command
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class LoadPostalCodesCommand extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $entityManager;
|
||||
|
||||
/**
|
||||
* @var ValidatorInterface
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* LoadPostalCodesCommand constructor.
|
||||
*
|
||||
* @param EntityManager $entityManager
|
||||
* @param ValidatorInterface $validator
|
||||
*/
|
||||
public function __construct(EntityManager $entityManager, ValidatorInterface $validator)
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private ValidatorInterface $validator;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, ValidatorInterface $validator)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->validator = $validator;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('chill:main:postal-code:populate')
|
||||
@@ -78,10 +45,10 @@ class LoadPostalCodesCommand extends Command
|
||||
->addArgument('csv_file', InputArgument::REQUIRED, "the path to "
|
||||
. "the csv file. See the help for specifications.")
|
||||
->addOption(
|
||||
'delimiter',
|
||||
'd',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
"The delimiter character of the csv file",
|
||||
'delimiter',
|
||||
'd',
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
"The delimiter character of the csv file",
|
||||
",")
|
||||
->addOption(
|
||||
'enclosure',
|
||||
@@ -99,31 +66,26 @@ class LoadPostalCodesCommand extends Command
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
try {
|
||||
$csv = $this->getCSVResource($input);
|
||||
} catch (\RuntimeException $e) {
|
||||
$output->writeln('<error>Error during opening the csv file : '.
|
||||
$e->getMessage().'</error>');
|
||||
}
|
||||
|
||||
$csv = $this->getCSVResource($input);
|
||||
|
||||
if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERY_VERBOSE) {
|
||||
$output->writeln('The content of the file is ...');
|
||||
$output->write(file_get_contents($input->getArgument('csv_file')));
|
||||
}
|
||||
|
||||
|
||||
$num = 0;
|
||||
$line = 0;
|
||||
|
||||
|
||||
while (($row = fgetcsv(
|
||||
$csv,
|
||||
0,
|
||||
$input->getOption('delimiter'),
|
||||
$input->getOption('enclosure'),
|
||||
$csv,
|
||||
0,
|
||||
$input->getOption('delimiter'),
|
||||
$input->getOption('enclosure'),
|
||||
$input->getOption('escape'))) !== false) {
|
||||
|
||||
|
||||
try{
|
||||
$this->addPostalCode($row, $output);
|
||||
$num++;
|
||||
@@ -136,31 +98,31 @@ class LoadPostalCodesCommand extends Command
|
||||
}
|
||||
$line ++;
|
||||
}
|
||||
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
|
||||
$output->writeln('<info>'.$num.' were added !</info>');
|
||||
}
|
||||
|
||||
|
||||
private function getCSVResource(InputInterface $input)
|
||||
{
|
||||
$fs = new Filesystem();
|
||||
$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.");
|
||||
}
|
||||
|
||||
|
||||
$resource = fopen($filename, 'r');
|
||||
|
||||
|
||||
if ($resource == FALSE) {
|
||||
throw new \RuntimeException("The file '$filename' could not be opened.");
|
||||
}
|
||||
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
|
||||
private function addPostalCode($row, OutputInterface $output)
|
||||
{
|
||||
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
||||
@@ -170,28 +132,28 @@ class LoadPostalCodesCommand extends Command
|
||||
$country = $em
|
||||
->getRepository(Country::class)
|
||||
->findOneBy(array('countryCode' => $row[2]));
|
||||
|
||||
|
||||
if ($country === NULL) {
|
||||
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
|
||||
$existingPC = $em
|
||||
->getRepository(PostalCode::class)
|
||||
->findBy(array('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",
|
||||
throw new ExistingPostalCodeException(sprintf("A postal code with code : %s and name : %s already exists, skipping",
|
||||
$row[0], $row[1]));
|
||||
}
|
||||
|
||||
|
||||
$postalCode = (new PostalCode())
|
||||
->setCode($row[0])
|
||||
->setName($row[1])
|
||||
->setCountry($country)
|
||||
;
|
||||
|
||||
|
||||
if (NULL != $row[3]){
|
||||
$postalCode->setRefPostalCodeId($row[3]);
|
||||
}
|
||||
@@ -205,7 +167,7 @@ class LoadPostalCodesCommand extends Command
|
||||
}
|
||||
|
||||
$errors = $this->validator->validate($postalCode);
|
||||
|
||||
|
||||
if ($errors->count() == 0) {
|
||||
$em->persist($postalCode);
|
||||
} else {
|
||||
@@ -213,12 +175,12 @@ class LoadPostalCodesCommand extends Command
|
||||
foreach ($errors as $error) {
|
||||
$msg .= " ".$error->getMessage();
|
||||
}
|
||||
|
||||
|
||||
throw new PostalCodeNotValidException($msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
||||
$output->writeln(sprintf('Creating postal code with code: %s, name: %s, countryCode: %s',
|
||||
$postalCode->getCode(), $postalCode->getName(), $postalCode->getCountry()->getCountryCode()));
|
||||
@@ -229,15 +191,15 @@ class LoadPostalCodesCommand extends Command
|
||||
|
||||
class ExistingPostalCodeException extends \Exception
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
class CountryCodeNotFoundException extends \Exception
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
class PostalCodeNotValidException extends \Exception
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user