1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Command; |
4
|
|
|
|
5
|
|
|
use FOS\UserBundle\Model\UserManager as FOSUserManager; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Kunstmaan\AdminBundle\Service\UserManager; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Question\Question; |
15
|
|
|
|
16
|
|
|
abstract class RoleCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** @var FOSUserManager|UserManager */ |
19
|
|
|
protected $userManager; |
20
|
|
|
|
21
|
|
View Code Duplication |
public function __construct(/* UserManager */ $userManager) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
parent::__construct(); |
24
|
|
|
|
25
|
|
|
if (!$userManager instanceof UserManager && !$userManager instanceof FOSUserManager) { |
26
|
|
|
throw new InvalidArgumentException(sprintf('The "$userManager" argument must be of type "%s" or type "%s"', UserManager::class, FOSUserManager::class)); |
27
|
|
|
} |
28
|
|
|
if ($userManager instanceof FOSUserManager) { |
29
|
|
|
// NEXT_MAJOR set the usermanaged typehint to the kunstmaan usermanager. |
30
|
|
|
@trigger_error(sprintf('Passing the usermanager from FOSUserBundle as the first argument of "%s" is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0. Use the new Kunstmaan Usermanager %s.', __METHOD__, UserManager::class), E_USER_DEPRECATED); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->userManager = $userManager; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setDefinition([ |
40
|
|
|
new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
41
|
|
|
new InputArgument('role', InputArgument::OPTIONAL, 'The role'), |
42
|
|
|
new InputOption('super', null, InputOption::VALUE_NONE, 'Instead specifying role, use this to quickly add the super administrator role'), |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
|
|
$username = $input->getArgument('username'); |
49
|
|
|
$role = $input->getArgument('role'); |
50
|
|
|
$super = (true === $input->getOption('super')); |
51
|
|
|
|
52
|
|
|
if (null !== $role && $super) { |
53
|
|
|
throw new InvalidArgumentException('You can pass either the role or the --super option (but not both simultaneously).'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (null === $role && !$super) { |
57
|
|
|
throw new RuntimeException('Not enough arguments.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->executeRoleCommand($output, $username, $super, $role); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
abstract protected function executeRoleCommand(OutputInterface $output, string $username, bool $super, string $role): int; |
64
|
|
|
|
65
|
|
View Code Duplication |
protected function interact(InputInterface $input, OutputInterface $output) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$questions = []; |
68
|
|
|
|
69
|
|
|
if (!$input->getArgument('username')) { |
70
|
|
|
$question = new Question('Please choose a username:'); |
71
|
|
|
$question->setValidator(function ($username) { |
72
|
|
|
if (empty($username)) { |
73
|
|
|
throw new InvalidArgumentException('Username can not be empty'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $username; |
77
|
|
|
}); |
78
|
|
|
$questions['username'] = $question; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ((true !== $input->getOption('super')) && !$input->getArgument('role')) { |
82
|
|
|
$question = new Question('Please choose a role:'); |
83
|
|
|
$question->setValidator(function ($role) { |
84
|
|
|
if (empty($role)) { |
85
|
|
|
throw new InvalidArgumentException('Role can not be empty'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $role; |
89
|
|
|
}); |
90
|
|
|
$questions['role'] = $question; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($questions as $name => $question) { |
94
|
|
|
$answer = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
95
|
|
|
$input->setArgument($name, $answer); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.