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:
@@ -11,10 +11,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Service\Import;
|
||||
|
||||
use Exception;
|
||||
use League\Csv\Reader;
|
||||
use League\Csv\Statement;
|
||||
use RuntimeException;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
@@ -37,10 +35,10 @@ class AddressReferenceBEFromBestAddress
|
||||
$release = $this->client->request('GET', self::RELEASE)
|
||||
->toArray();
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
throw new RuntimeException('could not get the release definition', 0, $e);
|
||||
throw new \RuntimeException('could not get the release definition', 0, $e);
|
||||
}
|
||||
|
||||
$asset = array_filter($release['assets'], static fn (array $item) => 'addresses-' . $list . '.' . $lang . '.csv.gz' === $item['name']);
|
||||
$asset = array_filter($release['assets'], static fn (array $item) => 'addresses-'.$list.'.'.$lang.'.csv.gz' === $item['name']);
|
||||
|
||||
return array_values($asset)[0]['browser_download_url'];
|
||||
}
|
||||
@@ -52,10 +50,10 @@ class AddressReferenceBEFromBestAddress
|
||||
$response = $this->client->request('GET', $downloadUrl);
|
||||
|
||||
if (200 !== $response->getStatusCode()) {
|
||||
throw new Exception('Could not download CSV: ' . $response->getStatusCode());
|
||||
throw new \Exception('Could not download CSV: '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$tmpname = tempnam(sys_get_temp_dir(), 'php-add-' . $list . $lang);
|
||||
$tmpname = tempnam(sys_get_temp_dir(), 'php-add-'.$list.$lang);
|
||||
$file = fopen($tmpname, 'r+b');
|
||||
|
||||
foreach ($this->client->stream($response) as $chunk) {
|
||||
@@ -79,8 +77,8 @@ class AddressReferenceBEFromBestAddress
|
||||
$record['municipality_objectid'],
|
||||
$record['postal_info_objectid'],
|
||||
$record['streetname'],
|
||||
$record['housenumber'] .($record['boxnumber'] !== '' ? ' bte '. $record['boxnumber'] : ''),
|
||||
'bestaddress.' . $list,
|
||||
$record['housenumber'].('' !== $record['boxnumber'] ? ' bte '.$record['boxnumber'] : ''),
|
||||
'bestaddress.'.$list,
|
||||
(float) $record['Y'],
|
||||
(float) $record['X'],
|
||||
3812
|
||||
|
@@ -13,12 +13,7 @@ namespace Chill\MainBundle\Service\Import;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Statement;
|
||||
use Exception;
|
||||
use LogicException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
|
||||
final class AddressReferenceBaseImporter
|
||||
{
|
||||
@@ -71,16 +66,16 @@ final class AddressReferenceBaseImporter
|
||||
string $street,
|
||||
string $streetNumber,
|
||||
string $source,
|
||||
?float $lat = null,
|
||||
?float $lon = null,
|
||||
?int $srid = null
|
||||
float $lat = null,
|
||||
float $lon = null,
|
||||
int $srid = null
|
||||
): void {
|
||||
if (!$this->isInitialized) {
|
||||
$this->initialize($source);
|
||||
}
|
||||
|
||||
if ($this->currentSource !== $source) {
|
||||
throw new LogicException('Cannot store addresses from different sources during same import. Execute finalize to commit inserts before changing the source');
|
||||
throw new \LogicException('Cannot store addresses from different sources during same import. Execute finalize to commit inserts before changing the source');
|
||||
}
|
||||
|
||||
$this->waitingForInsert[] = [
|
||||
@@ -95,7 +90,7 @@ final class AddressReferenceBaseImporter
|
||||
$srid,
|
||||
];
|
||||
|
||||
if (100 <= count($this->waitingForInsert)) {
|
||||
if (100 <= \count($this->waitingForInsert)) {
|
||||
$this->doInsertPending();
|
||||
}
|
||||
}
|
||||
@@ -122,7 +117,7 @@ final class AddressReferenceBaseImporter
|
||||
|
||||
private function doInsertPending(): void
|
||||
{
|
||||
if (!array_key_exists($forNumber = count($this->waitingForInsert), $this->cachingStatements)) {
|
||||
if (!\array_key_exists($forNumber = \count($this->waitingForInsert), $this->cachingStatements)) {
|
||||
$sql = strtr(self::INSERT, [
|
||||
'{{ values }}' => implode(
|
||||
', ',
|
||||
@@ -130,7 +125,7 @@ final class AddressReferenceBaseImporter
|
||||
),
|
||||
]);
|
||||
|
||||
$this->logger->debug(self::LOG_PREFIX . ' generated sql for insert', [
|
||||
$this->logger->debug(self::LOG_PREFIX.' generated sql for insert', [
|
||||
'sql' => $sql,
|
||||
'forNumber' => $forNumber,
|
||||
]);
|
||||
@@ -142,7 +137,7 @@ final class AddressReferenceBaseImporter
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logger->debug(self::LOG_PREFIX . ' inserting pending addresses', [
|
||||
$this->logger->debug(self::LOG_PREFIX.' inserting pending addresses', [
|
||||
'number' => $forNumber,
|
||||
'first' => $this->waitingForInsert[0] ?? null,
|
||||
]);
|
||||
@@ -153,11 +148,11 @@ final class AddressReferenceBaseImporter
|
||||
$affected = $statement->executeStatement(array_merge(...$this->waitingForInsert));
|
||||
|
||||
if (0 === $affected) {
|
||||
throw new RuntimeException('no row affected');
|
||||
throw new \RuntimeException('no row affected');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
// in some case, we can add debug code here
|
||||
//dump($this->waitingForInsert);
|
||||
// dump($this->waitingForInsert);
|
||||
throw $e;
|
||||
} finally {
|
||||
$this->waitingForInsert = [];
|
||||
@@ -178,8 +173,8 @@ final class AddressReferenceBaseImporter
|
||||
'CREATE INDEX idx_ref_add_temp ON reference_address_temp (refid)'
|
||||
);
|
||||
|
||||
//1) Add new addresses
|
||||
$this->logger->info(self::LOG_PREFIX . 'upsert new addresses');
|
||||
// 1) Add new addresses
|
||||
$this->logger->info(self::LOG_PREFIX.'upsert new addresses');
|
||||
$affected = $this->defaultConnection->executeStatement("INSERT INTO chill_main_address_reference
|
||||
(id, postcode_id, refid, street, streetnumber, municipalitycode, source, point, createdat, deletedat, updatedat)
|
||||
SELECT
|
||||
@@ -198,16 +193,16 @@ final class AddressReferenceBaseImporter
|
||||
ON CONFLICT (refid, source) DO UPDATE
|
||||
SET postcode_id = excluded.postcode_id, refid = excluded.refid, street = excluded.street, streetnumber = excluded.streetnumber, municipalitycode = excluded.municipalitycode, source = excluded.source, point = excluded.point, updatedat = NOW(), deletedAt = NULL
|
||||
");
|
||||
$this->logger->info(self::LOG_PREFIX . 'addresses upserted', ['upserted' => $affected]);
|
||||
$this->logger->info(self::LOG_PREFIX.'addresses upserted', ['upserted' => $affected]);
|
||||
|
||||
//3) Delete addresses
|
||||
$this->logger->info(self::LOG_PREFIX . 'soft delete adresses');
|
||||
// 3) Delete addresses
|
||||
$this->logger->info(self::LOG_PREFIX.'soft delete adresses');
|
||||
$affected = $this->defaultConnection->executeStatement('UPDATE chill_main_address_reference
|
||||
SET deletedat = NOW()
|
||||
WHERE
|
||||
chill_main_address_reference.refid NOT IN (SELECT refid FROM reference_address_temp WHERE source LIKE ?)
|
||||
AND chill_main_address_reference.source LIKE ?
|
||||
', [$this->currentSource, $this->currentSource]);
|
||||
$this->logger->info(self::LOG_PREFIX . 'addresses deleted', ['deleted' => $affected]);
|
||||
$this->logger->info(self::LOG_PREFIX.'addresses deleted', ['deleted' => $affected]);
|
||||
}
|
||||
}
|
||||
|
@@ -11,12 +11,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Service\Import;
|
||||
|
||||
use Exception;
|
||||
use League\Csv\Reader;
|
||||
use League\Csv\Statement;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use UnexpectedValueException;
|
||||
use function is_int;
|
||||
|
||||
class AddressReferenceFromBano
|
||||
{
|
||||
@@ -24,8 +21,8 @@ class AddressReferenceFromBano
|
||||
|
||||
public function import(string $departementNo): void
|
||||
{
|
||||
if (!is_numeric($departementNo) || !is_int((int) $departementNo)) {
|
||||
throw new UnexpectedValueException('Could not parse this department number');
|
||||
if (!is_numeric($departementNo) || !\is_int((int) $departementNo)) {
|
||||
throw new \UnexpectedValueException('Could not parse this department number');
|
||||
}
|
||||
|
||||
$url = "https://bano.openstreetmap.fr/data/bano-{$departementNo}.csv";
|
||||
@@ -33,7 +30,7 @@ class AddressReferenceFromBano
|
||||
$response = $this->client->request('GET', $url);
|
||||
|
||||
if (200 !== $response->getStatusCode()) {
|
||||
throw new Exception('Could not download CSV: ' . $response->getStatusCode());
|
||||
throw new \Exception('Could not download CSV: '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$file = tmpfile();
|
||||
@@ -65,7 +62,7 @@ class AddressReferenceFromBano
|
||||
$record['postcode'],
|
||||
$record['street'],
|
||||
$record['streetNumber'],
|
||||
'BANO.' . $departementNo,
|
||||
'BANO.'.$departementNo,
|
||||
(float) $record['lat'],
|
||||
(float) $record['lon'],
|
||||
4326
|
||||
|
@@ -17,7 +17,7 @@ use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Mark existing addresses as to be reviewed regarding the
|
||||
* address reference
|
||||
* address reference.
|
||||
*/
|
||||
final readonly class AddressToReferenceMatcher
|
||||
{
|
||||
@@ -61,7 +61,7 @@ final readonly class AddressToReferenceMatcher
|
||||
private const SUBSTITUTES = [
|
||||
'{{ to_review }}' => Address::ADDR_REFERENCE_STATUS_TO_REVIEW,
|
||||
'{{ matching }}' => Address::ADDR_REFERENCE_STATUS_MATCH,
|
||||
'{{ reviewed }}' => Address::ADDR_REFERENCE_STATUS_REVIEWED
|
||||
'{{ reviewed }}' => Address::ADDR_REFERENCE_STATUS_REVIEWED,
|
||||
];
|
||||
|
||||
public function __construct(private Connection $connection, private LoggerInterface $logger) {}
|
||||
|
@@ -14,11 +14,7 @@ namespace Chill\MainBundle\Service\Import;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Statement;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
|
||||
final class GeographicalUnitBaseImporter
|
||||
{
|
||||
@@ -68,7 +64,7 @@ final class GeographicalUnitBaseImporter
|
||||
string $unitName,
|
||||
string $unitKey,
|
||||
string $geomAsWKT,
|
||||
?int $srid = null
|
||||
int $srid = null
|
||||
): void {
|
||||
$this->initialize();
|
||||
|
||||
@@ -81,7 +77,7 @@ final class GeographicalUnitBaseImporter
|
||||
'srid' => $srid,
|
||||
];
|
||||
|
||||
if (100 <= count($this->waitingForInsert)) {
|
||||
if (100 <= \count($this->waitingForInsert)) {
|
||||
$this->doInsertPending();
|
||||
}
|
||||
}
|
||||
@@ -106,13 +102,13 @@ final class GeographicalUnitBaseImporter
|
||||
|
||||
private function doInsertPending(): void
|
||||
{
|
||||
$forNumber = count($this->waitingForInsert);
|
||||
$forNumber = \count($this->waitingForInsert);
|
||||
|
||||
if (0 === $forNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!array_key_exists($forNumber, $this->cachingStatements)) {
|
||||
if (!\array_key_exists($forNumber, $this->cachingStatements)) {
|
||||
$sql = strtr(self::INSERT, [
|
||||
'{{ values }}' => implode(
|
||||
', ',
|
||||
@@ -120,7 +116,7 @@ final class GeographicalUnitBaseImporter
|
||||
),
|
||||
]);
|
||||
|
||||
$this->logger->debug(self::LOG_PREFIX . ' generated sql for insert', [
|
||||
$this->logger->debug(self::LOG_PREFIX.' generated sql for insert', [
|
||||
'sql' => $sql,
|
||||
'forNumber' => $forNumber,
|
||||
]);
|
||||
@@ -145,9 +141,9 @@ final class GeographicalUnitBaseImporter
|
||||
$affected = $statement->executeStatement();
|
||||
|
||||
if (0 === $affected) {
|
||||
throw new RuntimeException('no row affected');
|
||||
throw new \RuntimeException('no row affected');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
} finally {
|
||||
$this->waitingForInsert = [];
|
||||
@@ -193,8 +189,8 @@ final class GeographicalUnitBaseImporter
|
||||
"
|
||||
);
|
||||
|
||||
//1) Add new units
|
||||
$this->logger->info(self::LOG_PREFIX . 'upsert new units');
|
||||
// 1) Add new units
|
||||
$this->logger->info(self::LOG_PREFIX.'upsert new units');
|
||||
$affected = $this->defaultConnection->executeStatement("INSERT INTO chill_main_geographical_unit
|
||||
(id, geom, unitname, layer_id, unitrefid)
|
||||
SELECT
|
||||
@@ -208,10 +204,10 @@ final class GeographicalUnitBaseImporter
|
||||
DO UPDATE
|
||||
SET geom = EXCLUDED.geom, unitname = EXCLUDED.unitname
|
||||
");
|
||||
$this->logger->info(self::LOG_PREFIX . 'units upserted', ['upserted' => $affected]);
|
||||
$this->logger->info(self::LOG_PREFIX.'units upserted', ['upserted' => $affected]);
|
||||
|
||||
//3) Delete units
|
||||
$this->logger->info(self::LOG_PREFIX . 'soft delete adresses');
|
||||
// 3) Delete units
|
||||
$this->logger->info(self::LOG_PREFIX.'soft delete adresses');
|
||||
$affected = $this->defaultConnection->executeStatement('WITH to_delete AS (
|
||||
SELECT cmgu.id
|
||||
FROM chill_main_geographical_unit AS cmgu
|
||||
@@ -221,7 +217,7 @@ final class GeographicalUnitBaseImporter
|
||||
DELETE FROM chill_main_geographical_unit
|
||||
WHERE id NOT IN (SELECT id FROM to_delete)
|
||||
');
|
||||
$this->logger->info(self::LOG_PREFIX . 'addresses deleted', ['deleted' => $affected]);
|
||||
$this->logger->info(self::LOG_PREFIX.'addresses deleted', ['deleted' => $affected]);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@@ -13,7 +13,6 @@ namespace Chill\MainBundle\Service\Import;
|
||||
|
||||
use League\Csv\Reader;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
@@ -33,7 +32,7 @@ class PostalCodeBEFromBestAddress
|
||||
$tmpfile = fopen($tmpname, 'r+b');
|
||||
|
||||
if (false === $tmpfile) {
|
||||
throw new RuntimeException('could not create temporary file');
|
||||
throw new \RuntimeException('could not create temporary file');
|
||||
}
|
||||
|
||||
foreach ($this->client->stream($response) as $chunk) {
|
||||
@@ -55,11 +54,11 @@ class PostalCodeBEFromBestAddress
|
||||
gzclose($uncompressedStream);
|
||||
unlink($tmpname);
|
||||
|
||||
$this->logger->info(self::class . ' list of postal code downloaded');
|
||||
$this->logger->info(self::class.' list of postal code downloaded');
|
||||
|
||||
$this->baseImporter->finalize();
|
||||
|
||||
$this->logger->info(self::class . ' postal code fetched', ['offset' => $offset ?? 0]);
|
||||
$this->logger->info(self::class.' postal code fetched', ['offset' => $offset ?? 0]);
|
||||
}
|
||||
|
||||
private function getFileDownloadUrl(string $lang): string
|
||||
@@ -68,10 +67,10 @@ class PostalCodeBEFromBestAddress
|
||||
$release = $this->client->request('GET', self::RELEASE)
|
||||
->toArray();
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
throw new RuntimeException('could not get the release definition', 0, $e);
|
||||
throw new \RuntimeException('could not get the release definition', 0, $e);
|
||||
}
|
||||
|
||||
$postals = array_filter($release['assets'], static fn (array $item) => 'postals.' . $lang . '.csv.gz' === $item['name']);
|
||||
$postals = array_filter($release['assets'], static fn (array $item) => 'postals.'.$lang.'.csv.gz' === $item['name']);
|
||||
|
||||
return array_values($postals)[0]['browser_download_url'];
|
||||
}
|
||||
|
@@ -13,9 +13,6 @@ namespace Chill\MainBundle\Service\Import;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Statement;
|
||||
use Exception;
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* Optimized way to load postal code into database.
|
||||
@@ -85,14 +82,14 @@ class PostalCodeBaseImporter
|
||||
$centerSRID,
|
||||
];
|
||||
|
||||
if (100 <= count($this->waitingForInsert)) {
|
||||
if (100 <= \count($this->waitingForInsert)) {
|
||||
$this->doInsertPending();
|
||||
}
|
||||
}
|
||||
|
||||
private function doInsertPending(): void
|
||||
{
|
||||
if (!array_key_exists($forNumber = count($this->waitingForInsert), $this->cachingStatements)) {
|
||||
if (!\array_key_exists($forNumber = \count($this->waitingForInsert), $this->cachingStatements)) {
|
||||
$sql = strtr(self::QUERY, [
|
||||
'{{ values }}' => implode(
|
||||
', ',
|
||||
@@ -107,9 +104,9 @@ class PostalCodeBaseImporter
|
||||
|
||||
try {
|
||||
$statement->executeStatement(array_merge(...$this->waitingForInsert));
|
||||
} catch (Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
// in some case, we can add debug code here
|
||||
//dump($this->waitingForInsert);
|
||||
// dump($this->waitingForInsert);
|
||||
throw $e;
|
||||
} finally {
|
||||
$this->waitingForInsert = [];
|
||||
|
@@ -13,7 +13,6 @@ namespace Chill\MainBundle\Service\Import;
|
||||
|
||||
use League\Csv\Reader;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
@@ -33,13 +32,13 @@ class PostalCodeFRFromOpenData
|
||||
$response = $this->client->request('GET', self::CSV);
|
||||
|
||||
if (200 !== $response->getStatusCode()) {
|
||||
throw new RuntimeException('could not download CSV');
|
||||
throw new \RuntimeException('could not download CSV');
|
||||
}
|
||||
|
||||
$tmpfile = tmpfile();
|
||||
|
||||
if (false === $tmpfile) {
|
||||
throw new RuntimeException('could not create temporary file');
|
||||
throw new \RuntimeException('could not create temporary file');
|
||||
}
|
||||
|
||||
foreach ($this->client->stream($response) as $chunk) {
|
||||
@@ -59,7 +58,7 @@ class PostalCodeFRFromOpenData
|
||||
$this->baseImporter->finalize();
|
||||
fclose($tmpfile);
|
||||
|
||||
$this->logger->info(self::class . ' postal code fetched', ['offset' => $offset ?? 0]);
|
||||
$this->logger->info(self::class.' postal code fetched', ['offset' => $offset ?? 0]);
|
||||
}
|
||||
|
||||
private function handleRecord(array $record): void
|
||||
@@ -74,7 +73,7 @@ class PostalCodeFRFromOpenData
|
||||
|
||||
if (str_starts_with($ref, '987')) {
|
||||
// some differences in French Polynesia
|
||||
$ref .= '.' . trim((string) $record['Libellé_d_acheminement']);
|
||||
$ref .= '.'.trim((string) $record['Libellé_d_acheminement']);
|
||||
}
|
||||
|
||||
$this->baseImporter->importCode(
|
||||
|
Reference in New Issue
Block a user