-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateIdentityCommand.php
More file actions
79 lines (67 loc) · 2.34 KB
/
Copy pathCreateIdentityCommand.php
File metadata and controls
79 lines (67 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace ADT\FancyAdmin\Console;
use ADT\FancyAdmin\Model\Entities\AclRole;
use ADT\FancyAdmin\Model\Entities\Identity;
use ADT\FancyAdmin\Model\Entities\Profile;
use ADT\FancyAdmin\Model\FancyAdmin;
use ADT\FancyAdmin\Model\Mailer\Mailer;
use ADT\FancyAdmin\Model\Queries\Factories\AclRoleQueryFactory;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(name: 'fancyadmin:create-identity', description: 'Create identity')]
class CreateIdentityCommand extends \ADT\FancyAdmin\Console\Command
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly AclRoleQueryFactory $aclRoleQueryFactory,
private readonly FancyAdmin $fancyAdmin,
private readonly Mailer $mailer
)
{
parent::__construct();
}
/**
* @throws NonUniqueResultException
* @throws Exception
*/
protected function executeCommand(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$identityEntity = $this->em->findEntityClassByInterface(Identity::class);
$this->validateInput($io, 'Křestní jméno', $firstname);
$this->validateInput($io, 'Příjmení', $lastname);
$this->validateInput($io, 'E-mail', $email);
$this->validateInput($io, 'Telefon', $phoneNumber);
/** @var AclRole $role */
$role = $this->aclRoleQueryFactory->create()->byIsAdmin(true)->fetchOne(false);
/** @var Identity $identity */
$identity = (new $identityEntity);
$identity
->setContext($this->fancyAdmin->getContext())
->setFirstName($firstname)
->setLastName($lastname)
->setEmail($email)
->setPhoneNumber($phoneNumber)
->addRole($role);
$this->em->persist($identity);
$this->em->flush();
$this->mailer->sendAccountCreationEmail($identity);
return Command::SUCCESS;
}
private function validateInput(SymfonyStyle $io, string $question, ?string &$variable): void
{
while (!$variable) {
$variable = trim((string)$io->ask($question));
if ($variable === '') {
$io->warning(sprintf('Položka \'%s\' musí být vyplněna', $question));
$variable = null;
}
}
}
}