migrations/Version20221221105531.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use App\Entity\Access;
  5. use App\Entity\Institute;
  6. use App\Entity\Municipal;
  7. use App\Entity\User;
  8. use App\Service\Doctrine\Migrations\AwareHasherInterface;
  9. use Doctrine\DBAL\Schema\Schema;
  10. use Doctrine\Migrations\AbstractMigration;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. /**
  16.  * Auto-generated Migration: Please modify to your needs!
  17.  */
  18. final class Version20221221105531 extends AbstractMigration implements ContainerAwareInterfaceAwareHasherInterface
  19. {
  20.     private UserPasswordHasherInterface $hasher;
  21.     private EntityManagerInterface $em;
  22.     private User $adminUser;
  23.     public function setContainer(ContainerInterface $container null)
  24.     {
  25.         $this->em $container->get('doctrine.orm.entity_manager');
  26.     }
  27.     public function setHasher(UserPasswordHasherInterface $hasher): void
  28.     {
  29.         $this->hasher $hasher;
  30.     }
  31.     public function getDescription(): string
  32.     {
  33.         return '';
  34.     }
  35.     public function up(Schema $schema): void
  36.     {
  37.         // this up() migration is auto-generated, please modify it to your needs
  38.         $this->addSql('CREATE TABLE access (id SERIAL NOT NULL, user_id INT NOT NULL, municipal_id INT DEFAULT NULL, institute_id INT DEFAULT NULL, PRIMARY KEY(id))');
  39.         $this->addSql('CREATE INDEX IDX_6692B54A76ED395 ON access (user_id)');
  40.         $this->addSql('CREATE INDEX IDX_6692B547A79FFCF ON access (municipal_id)');
  41.         $this->addSql('CREATE INDEX IDX_6692B54697B0F4C ON access (institute_id)');
  42.         $this->addSql('COMMENT ON TABLE access IS \'Внутренний портал. Какой пользователь к какой сущности привязан.\'');
  43.         $this->addSql('ALTER TABLE access ADD CONSTRAINT FK_6692B54A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
  44.         $this->addSql('ALTER TABLE access ADD CONSTRAINT FK_6692B547A79FFCF FOREIGN KEY (municipal_id) REFERENCES municipal (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
  45.         $this->addSql('ALTER TABLE access ADD CONSTRAINT FK_6692B54697B0F4C FOREIGN KEY (institute_id) REFERENCES institute (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
  46.         $republicInstitutes = [
  47.             'ГАУ РСШОР по фехтованию',
  48.             'ГАУ РСШОР по фехтованию',
  49.             'ГБУ РСШОР по фигурному катанию на коньках и шорт-треку',
  50.             'ГАУ РСШОР по шахматам, шашкам, го им. Р.Г.Нежметдинова',
  51.             'ГАУ РСШОР "Батыр"',
  52.             'ГБУ РСШ по конному спорту',
  53.             'ГАУ РСШ по борьбе',
  54.             'ГБУ РСШОР по стендовой и пулевой стрельбе',
  55.             'ГАУ РСШОР "Динамо"',
  56.             'ГБУ РСШОР по водным видам спорта "Акватика"',
  57.             'ГБУ РСШОР по водным видам спорта "Акватика"',
  58.             'ГБУ "РСАШ"',
  59.             'ГБУ РСШ по зимним видам спорта "Барс"',
  60.             'ГБУ РСШ по бадминтону им. Ф.Г.Валеева',
  61.             'ГБУ РСШ по бадминтону им. Ф.Г.Валеева',
  62.             'ГБУ РСШ по регби',
  63.         ];
  64.         foreach ($republicInstitutes as $republicInstitute) {
  65.             $sql sprintf("INSERT INTO institute (created_by_id, name) VALUES (1, '%s')"$republicInstitute);
  66.             $this->addSql($sql);
  67.         }
  68.     }
  69.     /**
  70.      * @throws \Exception
  71.      */
  72.     public function postUp(Schema $schema): void
  73.     {
  74.         $this->adminUser $this->em->getRepository(User::class)->findOneBy(['email' => $_ENV['SUPERUSER_EMAIL']]);
  75.         try {
  76.             $this->em->beginTransaction();
  77.             $this->makeMunicipalUsers();
  78.             $this->makeRepublicUsers();
  79.             $this->em->commit();
  80.         } catch (\Exception $exception) {
  81.             $this->em->rollback();
  82.             throw $exception;
  83.         }
  84.     }
  85.     private function makeMunicipalUsers()
  86.     {
  87.         $municipals $this->getMunicipals();
  88.         foreach ($municipals as $municipal) {
  89.             $user $this->makeUser('municipal_' $municipal->getId());
  90.             $access = new Access();
  91.             $access->setUser($user);
  92.             $access->setMunicipal($municipal);
  93.             $this->em->persist($access);
  94.         }
  95.         $this->em->flush();
  96.     }
  97.     private function makeRepublicUsers()
  98.     {
  99.         $institutes $this->getRepublicInstitutes();
  100.         foreach ($institutes as $institute) {
  101.             $user $this->makeUser('institute' $institute->getId());
  102.             $access = new Access();
  103.             $access->setUser($user);
  104.             $access->setInstitute($institute);
  105.             $this->em->persist($access);
  106.         }
  107.         $this->em->flush();
  108.     }
  109.     private function makeUser(string $userName): User
  110.     {
  111.         $user = new User();
  112.         $user->setEmail($userName '@etton.ru');
  113.         $user->setFirstName($userName);
  114.         $user->setSecondName($userName);
  115.         $user->setLastName($userName);
  116.         $user->addRole(User::ROLE_MUNICIPAL_OFFICER);
  117.         $user->setCreatedBy($this->adminUser);
  118.         $user->setCreatedAt(new \DateTime());
  119.         $password $this->hasher->hashPassword($user$userName);
  120.         $user->setPassword($password);
  121.         $this->em->persist($user);
  122.         return $user;
  123.     }
  124.     /**
  125.      * @return Municipal[]
  126.      */
  127.     private function getMunicipals(): array
  128.     {
  129.         return $this->em->createQueryBuilder()
  130.             ->select('m')
  131.             ->from(Municipal::class, 'm')
  132.             ->orderBy('m.id')
  133.             ->getQuery()
  134.             ->getResult();
  135.     }
  136.     /**
  137.      * @return Institute[]
  138.      */
  139.     private function getRepublicInstitutes(): array
  140.     {
  141.         return $this->em->createQueryBuilder()
  142.             ->select('i')
  143.             ->from(Institute::class, 'i')
  144.             ->where('i.municipal IS NULL')
  145.             ->orderBy('i.id')
  146.             ->getQuery()
  147.             ->getResult();
  148.     }
  149.     public function down(Schema $schema): void
  150.     {
  151.         // this down() migration is auto-generated, please modify it to your needs
  152.         $this->addSql('ALTER TABLE access DROP CONSTRAINT FK_6692B54A76ED395');
  153.         $this->addSql('ALTER TABLE access DROP CONSTRAINT FK_6692B547A79FFCF');
  154.         $this->addSql('ALTER TABLE access DROP CONSTRAINT FK_6692B54697B0F4C');
  155.         $this->addSql('DROP TABLE access');
  156.     }
  157. }