<?php
/**
* This file is part of the Pimcore X Installation by
* ercas GmbH & CO. KG <https://www.ercasdieagentur.de>
*
* @license GPLv3
*/
namespace App\mvk\Auth\EventListener;
use App\Services\ContainerHelperService;
use App\Services\MailService;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
class PostAddListener
{
protected const USER_CLASSES = ['Agent'];
private $containerHelperService;
private MailService $mailService;
public function __construct(ContainerHelperService $containerHelperService, MailService $mailService)
{
$this->containerHelperService = $containerHelperService;
$this->mailService = $mailService;
}
public function onPostAdd(ElementEventInterface $e): void
{
if ($e instanceof DataObjectEvent) {
$user = $e->getObject();
$password= $this->generatePassword();
if (method_exists($user,'getClassName') && $this->supports($user->getClassName())) {
$user->setPassword($password);
$user->setActive(false);
$user->save();
$this->sendMailAction($user, $password);
}
}
}
private function supports($class)
{
if (in_array($class, self::USER_CLASSES)) {
return true;
}
return false;
}
private function sendMailAction($customer, $password)
{
$emailDocumentId = $this->containerHelperService->get('auth', 'FIRST_LOGIN_EMAIL_DOCUMENT_ID');
$username= $customer->getUserName() !== NULL ? $customer->getUserName() : ' ';
$mailParams = [
'Vermittler Nr' => $username,
'Passwort' => $password,
'html'=>true,
'import'=>true
];
$mailType='company';
$this->mailService->sendMail($mailType, $emailDocumentId, $mailParams);
}
private function generatePassword()
{
$data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
$dataSpecial= '$#&?!';
$dataFinale=substr(str_shuffle($data), 0, 6).substr(str_shuffle($dataSpecial), 0, 2);
return $dataFinale;
}
}