Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

64 linhas
1.8KB

  1. <?php
  2. namespace domain\User\User;
  3. use common\helpers\CSV;
  4. use common\helpers\Password;
  5. use domain\_\AbstractManager;
  6. use yii\base\ErrorException;
  7. class UserBulkImporter extends AbstractManager
  8. {
  9. const TYPE = 0;
  10. const LASTNAME = 1;
  11. const FIRSTNAME = 2;
  12. const NAME_LEGAL_PERSON = 3;
  13. const PHONE = 4;
  14. const EMAIL = 5;
  15. const NEWSLETTER = 6;
  16. const ADDRESS = 7;
  17. protected UserSolver $userSolver;
  18. protected UserBuilder $userBuilder;
  19. public function loadDependencies(): void
  20. {
  21. $this->userSolver = $this->loadService(UserSolver::class);
  22. $this->userBuilder = $this->loadService(UserBuilder::class);
  23. }
  24. /**
  25. * @throws ErrorException
  26. */
  27. public function import(string $fileName, bool $sendMailWelcome = true): void
  28. {
  29. $usersArray = $this->loadCsv($fileName);
  30. foreach($usersArray as $userData) {
  31. $this->userBuilder->createUser(
  32. $this->userSolver->getTypeIndex($userData[self::TYPE]),
  33. $userData[self::EMAIL],
  34. $userData[self::FIRSTNAME],
  35. $userData[self::LASTNAME],
  36. $userData[self::NAME_LEGAL_PERSON],
  37. $userData[self::PHONE],
  38. $userData[self::ADDRESS],
  39. $userData[self::NEWSLETTER],
  40. Password::generate(),
  41. $sendMailWelcome
  42. );
  43. }
  44. }
  45. public function loadCsv(string $fileName): array
  46. {
  47. $usersArray = CSV::csv2array($fileName);
  48. if(count($usersArray) && count($usersArray[0]) != 8) {
  49. throw new ErrorException("Le fichier n'a pas le bon nombre de colonnes.
  50. Veuillez vous baser sur le fichier d'exemple téléchargeable ci-dessous.");
  51. }
  52. return $usersArray;
  53. }
  54. }