diff --git a/src/Bundle/ChillMainBundle/Command/LoadAddressReferenceCommand.php b/src/Bundle/ChillMainBundle/Command/LoadAddressReferenceCommand.php new file mode 100644 index 000000000..2f2f56800 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Command/LoadAddressReferenceCommand.php @@ -0,0 +1,126 @@ +entityManager=$entityManager; + parent::__construct(); + } + + /* + * (non-PHPdoc) + * @see \Symfony\Component\Console\Command\Command::configure() + */ + protected function configure(): void + { + $this + ->setName('chill:main:address-reference:populate') + ->setDescription('Load or update countries in db. This command does not delete or update existing address reference,'. + 'but will add new address reference entities based on their timestamp'); + } + + /* + * (non-PHPdoc) + * @see \Symfony\Component\Console\Command\Command::execute() + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $addressReferenceArray = static::fetchVendeeBAL(); + $em = $this->entityManager; + + foreach($addressReferenceArray as $a) { + if (static::isAddressReferenceNew($a)) { + $em->persist($a); + } + } + + $em->flush(); + + return 0; + } + + public static function isAddressReferenceNew(AddressReference $a): bool + { + $cond = true; + // TODO: write this function + + return $cond; + } + + + /* + * //TODO move this function in App and make the command generic by accepting a fetcher as an argument + * @see \Symfony\Component\Console\Command\Command::execute() + */ + public static function fetchVendeeBAL(): array + { + + $dataSource = 'Vendée Base d\'adresses locale'; + + $addressReferenceArray = array(); + + $wfsUrl = 'https://carto.vendee.fr/arcgis/services/Applications/CHILL/MapServer/WFSServer?SERVICE=WFS&REQUEST=GetFeature'; + + $wfsContent = file_get_contents($wfsUrl.'&version=2.0.0&TYPENAME=CHILL:Adresses&MAXFEATURES=3&OUTPUTFORMAT=geojson'); + //TODO loop for pagination + + $jsonContent = json_decode($wfsContent, true); + + dump($jsonContent); + foreach ($jsonContent['features'] as $key => $value) { + dump($key); + dump($value); + + $a = new AddressReference(); + + $x = $value['geometry']['coordinates'][0]; + $y = $value['geometry']['coordinates'][1]; //TODO get the coordinates in EPSG:4326 + + $lon = -1.4 + 0.01 * rand(-5, 5); + $lat = 46.5 + 0.01 * rand(-5, 5); //TODO temp + + $point = Point::fromLonLat($lon, $lat); + + $a->setRefId($value['properties']['OBJECTID']); + $a->setStreet($value['properties']['NOM_AFNOR']); + $a->setStreetNumber($value['properties']['NUMERO']); + $a->setPoint($point); + //$a->setPostcode('1000'); //TODO find postal code from the postal code in $value['properties']['CODE_POST'] //TODO store postcode if not found??? + $a->setMunicipalityCode($value['properties']['CODE_INSEE']); + $a->setSource($dataSource); + + $addressReferenceArray[] = $a; + } + + dump($addressReferenceArray); + + return $addressReferenceArray; + } + +} diff --git a/src/Bundle/ChillMainBundle/config/services/command.yaml b/src/Bundle/ChillMainBundle/config/services/command.yaml index 223449bfa..fc35f1e06 100644 --- a/src/Bundle/ChillMainBundle/config/services/command.yaml +++ b/src/Bundle/ChillMainBundle/config/services/command.yaml @@ -7,7 +7,7 @@ services: $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface' tags: - { name: console.command } - + Chill\MainBundle\Command\ChillUserSendRenewPasswordCodeCommand: arguments: $logger: '@Psr\Log\LoggerInterface' @@ -44,3 +44,9 @@ services: $entityManager: '@doctrine.orm.entity_manager' tags: - { name: console.command } + + Chill\MainBundle\Command\LoadAddressReferenceCommand: + arguments: + $entityManager: '@doctrine.orm.entity_manager' + tags: + - { name: console.command }