Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
25 | class CreateUserCommand extends ContainerAwareCommand |
||
|
|||
26 | { |
||
27 | /** @var EntityManagerInterface */ |
||
28 | private $em; |
||
29 | |||
30 | /** @var GroupManagerInterface */ |
||
31 | private $groupManager; |
||
32 | |||
33 | /** @var string */ |
||
34 | private $userClassname; |
||
35 | |||
36 | /** @var string */ |
||
37 | private $defaultLocale; |
||
38 | |||
39 | /** @var array */ |
||
40 | protected $groups = []; |
||
41 | |||
42 | public function __construct(/* EntityManagerInterface */ $em = null, GroupManagerInterface $groupManager = null, $userClassname = null, $defaultLocale = null) |
||
43 | { |
||
44 | parent::__construct(); |
||
45 | |||
46 | if (!$em instanceof EntityManagerInterface) { |
||
47 | @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED); |
||
48 | |||
49 | $this->setName(null === $em ? 'kuma:user:create' : $em); |
||
50 | |||
51 | return; |
||
52 | } |
||
53 | |||
54 | $this->em = $em; |
||
55 | $this->groupManager = $groupManager; |
||
56 | $this->userClassname = $userClassname; |
||
57 | $this->defaultLocale = $defaultLocale; |
||
58 | } |
||
59 | |||
60 | protected function configure() |
||
61 | { |
||
62 | parent::configure(); |
||
63 | |||
64 | $this->setName('kuma:user:create') |
||
65 | ->setDescription('Create a user.') |
||
66 | ->setDefinition(array( |
||
67 | new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
||
68 | new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
||
69 | new InputArgument('password', InputArgument::REQUIRED, 'The password'), |
||
70 | new InputArgument('locale', InputArgument::OPTIONAL, 'The locale (language)'), |
||
71 | new InputOption('group', null, InputOption::VALUE_REQUIRED, 'The group(s) the user should belong to'), |
||
72 | new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'), |
||
73 | new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'), |
||
74 | )) |
||
75 | ->setHelp(<<<'EOT' |
||
76 | The <info>kuma:user:create</info> command creates a user: |
||
77 | |||
78 | <info>php bin/console kuma:user:create matthieu --group=Users</info> |
||
79 | |||
80 | This interactive shell will ask you for an email and then a password. |
||
81 | |||
82 | You can alternatively specify the email, password and locale and group as extra arguments: |
||
83 | |||
84 | <info>php bin/console kuma:user:create matthieu [email protected] mypassword en --group=Users</info> |
||
85 | |||
86 | You can create a super admin via the super-admin flag: |
||
87 | |||
88 | <info>php bin/console kuma:user:create admin --super-admin --group=Administrators</info> |
||
89 | |||
90 | You can create an inactive user (will not be able to log in): |
||
91 | |||
92 | <info>php bin/console kuma:user:create thibault --inactive --group=Users</info> |
||
93 | |||
94 | <comment>Note:</comment> You have to specify at least one group. |
||
95 | |||
96 | EOT |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
104 | |||
105 | /** |
||
106 | * Executes the current command. |
||
107 | * |
||
108 | * @param InputInterface $input The input |
||
109 | * @param OutputInterface $output The output |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | protected function execute(InputInterface $input, OutputInterface $output) |
||
114 | { |
||
115 | if (null === $this->em) { |
||
116 | $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
117 | $this->groupManager = $this->getContainer()->get('fos_user.group_manager'); |
||
118 | $this->userClassname = $this->getContainer()->getParameter('fos_user.model.user.class'); |
||
119 | $this->defaultLocale = $this->getContainer()->getParameter('kunstmaan_admin.default_admin_locale'); |
||
120 | } |
||
121 | |||
122 | $username = $input->getArgument('username'); |
||
123 | $email = $input->getArgument('email'); |
||
124 | $password = $input->getArgument('password'); |
||
125 | $locale = $input->getArgument('locale'); |
||
126 | $superAdmin = $input->getOption('super-admin'); |
||
127 | $inactive = $input->getOption('inactive'); |
||
128 | $groupOption = $input->getOption('group'); |
||
129 | |||
130 | if (null === $locale) { |
||
131 | $locale = $this->defaultLocale; |
||
132 | } |
||
133 | $command = $this->getApplication()->find('fos:user:create'); |
||
134 | $arguments = array( |
||
135 | 'command' => 'fos:user:create', |
||
136 | 'username' => $username, |
||
137 | 'email' => $email, |
||
138 | 'password' => $password, |
||
139 | '--super-admin' => $superAdmin, |
||
140 | '--inactive' => $inactive, |
||
141 | ); |
||
142 | |||
143 | $input = new ArrayInput($arguments); |
||
144 | $command->run($input, $output); |
||
145 | |||
146 | // Fetch user that was just created |
||
147 | /** @var BaseUser $user */ |
||
148 | $user = $this->em->getRepository($this->userClassname)->findOneBy(array('username' => $username)); |
||
149 | |||
150 | $user->setCreatedBy('kuma:user:create command'); |
||
151 | $this->em->flush(); |
||
152 | |||
153 | // Attach groups |
||
154 | $groupOutput = []; |
||
155 | |||
156 | foreach (explode(',', $groupOption) as $groupId) { |
||
157 | if ((int) $groupId === 0) { |
||
158 | foreach ($this->groups as $value) { |
||
159 | if ($groupId === $value->getName()) { |
||
160 | $group = $value; |
||
161 | |||
162 | break; |
||
163 | } |
||
164 | } |
||
165 | } else { |
||
166 | $group = $this->groups[$groupId]; |
||
167 | } |
||
168 | |||
169 | if (isset($group) && $group instanceof Group) { |
||
170 | $groupOutput[] = $group->getName(); |
||
171 | $user->getGroups()->add($group); |
||
172 | } else { |
||
173 | throw new \RuntimeException('The selected group(s) can\'t be found.'); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | // Set admin interface locale and enable password changed |
||
178 | $user->setAdminLocale($locale); |
||
179 | $user->setPasswordChanged(true); |
||
180 | |||
181 | // Persist |
||
182 | $this->em->persist($user); |
||
183 | $this->em->flush(); |
||
184 | |||
185 | $output->writeln(sprintf('Added user <comment>%s</comment> to groups <comment>%s</comment>', $input->getArgument('username'), implode(',', $groupOutput))); |
||
186 | |||
187 | return 0; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Interacts with the user. |
||
192 | * |
||
193 | * @param InputInterface $input The input |
||
194 | * @param OutputInterface $output The output |
||
195 | * |
||
196 | * @throws \InvalidArgumentException |
||
197 | */ |
||
198 | protected function interact(InputInterface $input, OutputInterface $output) |
||
294 | |||
295 | private function getGroups() |
||
307 | } |
||
308 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.