baseImporter = $baseImporter; $this->client = $client; $this->logger = $logger; } public function import(): void { $response = $this->client->request('GET', self::CSV); if (200 !== $response->getStatusCode()) { throw new RuntimeException('could not download CSV'); } $tmpfile = tmpfile(); if (false === $tmpfile) { throw new RuntimeException('could not create temporary file'); } foreach ($this->client->stream($response) as $chunk) { fwrite($tmpfile, $chunk->getContent()); } fseek($tmpfile, 0); $csv = Reader::createFromStream($tmpfile); $csv->setDelimiter(';'); $csv->setHeaderOffset(0); foreach ($csv as $offset => $record) { $this->handleRecord($record); } $this->baseImporter->finalize(); fclose($tmpfile); $this->logger->info(__CLASS__ . ' postal code fetched', ['offset' => $offset ?? 0]); } private function handleRecord(array $record): void { if ('' !== trim($record['coordonnees_geographiques'] ?? $record['coordonnees_gps'])) { [$lat, $lon] = array_map(static fn ($el) => (float) trim($el), explode(',', $record['coordonnees_geographiques'] ?? $record['coordonnees_gps'])); } else { $lat = $lon = 0.0; } $ref = trim($record['Code_commune_INSEE']); if ('987' === substr($ref, 0, 3)) { // some differences in French Polynesia $ref .= '.' . trim($record['Libellé_d_acheminement']); } $this->baseImporter->importCode( 'FR', trim($record['Libellé_d_acheminement']), trim($record['Code_postal']), $ref, 'INSEE', $lat, $lon, 4326 ); } }