|
- <?php
-
- namespace domain\User\User;
-
- use common\helpers\CSV;
- use common\helpers\Password;
- use domain\_\AbstractManager;
- use yii\base\ErrorException;
-
- class UserBulkImporter extends AbstractManager
- {
- const TYPE = 0;
- const LASTNAME = 1;
- const FIRSTNAME = 2;
- const NAME_LEGAL_PERSON = 3;
- const PHONE = 4;
- const EMAIL = 5;
- const NEWSLETTER = 6;
- const ADDRESS = 7;
-
- protected UserSolver $userSolver;
- protected UserBuilder $userBuilder;
-
- public function loadDependencies(): void
- {
- $this->userSolver = $this->loadService(UserSolver::class);
- $this->userBuilder = $this->loadService(UserBuilder::class);
- }
-
- /**
- * @throws ErrorException
- */
- public function import(string $fileName, bool $sendMailWelcome = true): void
- {
- $usersArray = $this->loadCsv($fileName);
-
- foreach($usersArray as $userData) {
- $this->userBuilder->createUser(
- $this->userSolver->getTypeIndex($userData[self::TYPE]),
- $userData[self::EMAIL],
- $userData[self::FIRSTNAME],
- $userData[self::LASTNAME],
- $userData[self::NAME_LEGAL_PERSON],
- $userData[self::PHONE],
- $userData[self::ADDRESS],
- $userData[self::NEWSLETTER],
- Password::generate(),
- $sendMailWelcome
- );
- }
- }
-
- public function loadCsv(string $fileName): array
- {
- $usersArray = CSV::csv2array($fileName);
-
- if(count($usersArray) && count($usersArray[0]) != 8) {
- throw new ErrorException("Le fichier n'a pas le bon nombre de colonnes.
- Veuillez vous baser sur le fichier d'exemple téléchargeable ci-dessous.");
- }
-
- return $usersArray;
- }
- }
|