1
+ <?php
2
+
3
+ namespace Ggbb \SymfonyUserPermission \Command ;
4
+
5
+ use Doctrine \ORM \EntityManagerInterface ;
6
+ use Ggbb \SymfonyUserPermission \Entity \Interface \UserRoleInterface ;
7
+ use Ggbb \SymfonyUserPermission \GgbbUserPermissionBundle ;
8
+ use Symfony \Component \Console \Attribute \AsCommand ;
9
+ use Symfony \Component \Console \Command \Command ;
10
+ use Symfony \Component \Console \Input \InputInterface ;
11
+ use Symfony \Component \Console \Output \OutputInterface ;
12
+ use Symfony \Component \DependencyInjection \ParameterBag \ContainerBagInterface ;
13
+
14
+ #[AsCommand(
15
+ name: 'permission:create-default-user-role ' ,
16
+ description: '' ,
17
+ )]
18
+ class CreateDefaultUserRoleCommand extends Command
19
+ {
20
+ const USER_ROLES = [
21
+ 'ROLE_ADMIN ' ,
22
+ 'ROLE_USER ' ,
23
+ ];
24
+
25
+ public function __construct (
26
+ private EntityManagerInterface $ entityManager ,
27
+ private ContainerBagInterface $ container ,
28
+ string $ name = null )
29
+ {
30
+ parent ::__construct ($ name );
31
+ }
32
+
33
+ protected function execute (InputInterface $ input , OutputInterface $ output ): int
34
+ {
35
+ if (!$ this ->container ->has (GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY )) {
36
+ @trigger_error ('Config user_role_repository not found ' );
37
+ }
38
+
39
+ $ userRoleName = $ this ->container ->get (GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY );
40
+ foreach (self ::USER_ROLES as $ role ) {
41
+ /** @var UserRoleInterface $userRole */
42
+ $ userRole = new $ userRoleName ();
43
+ $ userRole ->setRole ($ role );
44
+ $ this ->entityManager ->persist ($ userRole );
45
+ }
46
+
47
+ try {
48
+ $ this ->entityManager ->flush ();
49
+ } catch (\Exception $ exception ) {
50
+ throw new \Exception ('Error: ' .$ exception ->getMessage ());
51
+
52
+ return Command::INVALID ;
53
+ }
54
+
55
+ return Command::SUCCESS ;
56
+ }
57
+ }
0 commit comments