migrations/Version20241030075504.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use App\Entity\InsuranceCompany;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  8. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  9. final class Version20241030075504 extends AbstractMigration implements ContainerAwareInterface
  10. {
  11.     use ContainerAwareTrait;
  12.     public function getDescription(): string
  13.     {
  14.         return 'Import all insurances from a CSV file';
  15.     }
  16.     /**
  17.      * @throws \Exception
  18.      */
  19.     public function up(Schema $schema): void
  20.     {
  21.         $csvFile __DIR__ '/../var/insurances/list.csv';
  22.         if (!file_exists($csvFile) || !is_readable($csvFile)) {
  23.             throw new \Exception('File not found or not readable');
  24.         }
  25.         $em $this->container->get('doctrine.orm.entity_manager');
  26.         if (($handle fopen($csvFile'r')) !== false) {
  27.             $headers fgetcsv($handle1000',');
  28.             $line 1;
  29.             while (($data fgetcsv($handle1000';')) !== false) {
  30.                 $insurance $em->getRepository(InsuranceCompany::class)->find($line);
  31.                 $streetNumber $this->extractStreetNumber($data[2]);
  32.                 if ($insurance) {
  33.                     $this->addSql('UPDATE insurance_company SET gmaps_address = ?, city = ?, zipCode = ?, streetNumber = ?, address = ?, name = ?, site = ?, bp = ?, phone = ?, email = ? WHERE id = ?', [
  34.                         $data[7],
  35.                         strtoupper($data[5]),
  36.                         $data[4],
  37.                         $streetNumber,
  38.                         $data[2],
  39.                         $data[0],
  40.                         'https://',
  41.                         $data[3],
  42.                         '0102030405',
  43.                         null,
  44.                         $line,
  45.                     ]);
  46.                 } else {
  47.                     $datetime = new \DateTime();
  48.                     $this->addSql('INSERT INTO insurance_company (id, is_deleted, createdAt, gmaps_address, city, zipCode, streetNumber, address, name, site, bp, phone, email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
  49.                         $line,
  50.                         0,
  51.                         $datetime->format('Y-m-d H:i:s'),
  52.                         $data[7],
  53.                         strtoupper($data[5]),
  54.                         $data[4],
  55.                         $streetNumber,
  56.                         $data[2],
  57.                         $data[0],
  58.                         'https://',
  59.                         $data[3],
  60.                         '0102030405',
  61.                         null
  62.                     ]);
  63.                 }
  64.                 $line++;
  65.             }
  66.             fclose($handle);
  67.         }
  68.     }
  69.     public function down(Schema $schema): void
  70.     {
  71.     }
  72.     /**
  73.      * @param string $address
  74.      * @return int|null
  75.      */
  76.     private function extractStreetNumber(string $address): ?int
  77.     {
  78.         $address ltrim($address);
  79.         $pattern '/^(\d+)/';
  80.         if (preg_match($pattern$address$matches)) {
  81.             return (int)$matches[1];
  82.         } else {
  83.             return null;
  84.         }
  85.     }
  86. }