mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
105 lines
3.1 KiB
PHP
105 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* 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\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;
|
|
|
|
class AddressReferenceBEFromBestAddress
|
|
{
|
|
private const RELEASE = 'https://gitea.champs-libres.be/api/v1/repos/Chill-project/belgian-bestaddresses-transform/releases/tags/v1.0.0';
|
|
|
|
private AddressReferenceBaseImporter $baseImporter;
|
|
|
|
private HttpClientInterface $client;
|
|
|
|
public function __construct(HttpClientInterface $client, AddressReferenceBaseImporter $baseImporter)
|
|
{
|
|
$this->client = $client;
|
|
$this->baseImporter = $baseImporter;
|
|
}
|
|
|
|
public function import(string $lang, array $lists): void
|
|
{
|
|
foreach ($lists as $list) {
|
|
$this->importList($lang, $list);
|
|
}
|
|
}
|
|
|
|
private function getDownloadUrl(string $lang, string $list): string
|
|
{
|
|
try {
|
|
$release = $this->client->request('GET', self::RELEASE)
|
|
->toArray();
|
|
} catch (TransportExceptionInterface $e) {
|
|
throw new RuntimeException('could not get the release definition', 0, $e);
|
|
}
|
|
|
|
$asset = array_filter($release['assets'], static function (array $item) use ($lang, $list) {
|
|
return 'addresses-' . $list . '.' . $lang . '.csv.gz' === $item['name'];
|
|
});
|
|
|
|
return array_values($asset)[0]['browser_download_url'];
|
|
}
|
|
|
|
private function importList(string $lang, string $list): void
|
|
{
|
|
$downloadUrl = $this->getDownloadUrl($lang, $list);
|
|
|
|
$response = $this->client->request('GET', $downloadUrl);
|
|
|
|
if (200 !== $response->getStatusCode()) {
|
|
throw new Exception('Could not download CSV: ' . $response->getStatusCode());
|
|
}
|
|
|
|
$tmpname = tempnam(sys_get_temp_dir(), 'php-add-' . $list . $lang);
|
|
$file = fopen($tmpname, 'r+b');
|
|
|
|
foreach ($this->client->stream($response) as $chunk) {
|
|
fwrite($file, $chunk->getContent());
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
$uncompressedStream = gzopen($tmpname, 'r');
|
|
|
|
$csv = Reader::createFromStream($uncompressedStream);
|
|
$csv->setDelimiter(',');
|
|
$csv->setHeaderOffset(0);
|
|
|
|
$stmt = Statement::create()
|
|
->process($csv);
|
|
|
|
foreach ($stmt as $record) {
|
|
$this->baseImporter->importAddress(
|
|
$record['best_id'],
|
|
$record['municipality_objectid'],
|
|
$record['postal_info_objectid'],
|
|
$record['streetname'],
|
|
$record['housenumber'] . $record['boxnumber'],
|
|
'bestaddress.' . $list,
|
|
(float) $record['X'],
|
|
(float) $record['Y'],
|
|
3812
|
|
);
|
|
}
|
|
|
|
$this->baseImporter->finalize();
|
|
|
|
gzclose($uncompressedStream);
|
|
}
|
|
}
|