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 |
||
24 | class CreateUserCommand extends ContainerAwareCommand |
||
|
|||
25 | { |
||
26 | /** @var EntityManagerInterface */ |
||
27 | private $em; |
||
28 | |||
29 | /** @var GroupManagerInterface */ |
||
30 | private $groupManager; |
||
31 | |||
32 | /** @var string */ |
||
33 | private $userClassname; |
||
34 | |||
35 | /** @var string */ |
||
36 | private $defaultLocale; |
||
37 | |||
38 | /** @var array */ |
||
39 | protected $groups = []; |
||
40 | |||
41 | public function __construct(/* EntityManagerInterface */ $em = null, GroupManagerInterface $groupManager = null, $userClassname = null, $defaultLocale = null) |
||
58 | |||
59 | protected function configure() |
||
98 | |||
99 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
103 | |||
104 | /** |
||
105 | * Executes the current command. |
||
106 | * |
||
107 | * @param InputInterface $input The input |
||
108 | * @param OutputInterface $output The output |
||
109 | * |
||
110 | * @return int |
||
111 | */ |
||
112 | protected function execute(InputInterface $input, OutputInterface $output) |
||
113 | { |
||
114 | if (null === $this->em) { |
||
115 | $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
116 | $this->groupManager = $this->getContainer()->get('fos_user.group_manager'); |
||
117 | $this->userClassname = $this->getContainer()->getParameter('fos_user.model.user.class'); |
||
118 | $this->defaultLocale = $this->getContainer()->getParameter('kunstmaan_admin.default_admin_locale'); |
||
119 | } |
||
120 | |||
121 | $username = $input->getArgument('username'); |
||
122 | $email = $input->getArgument('email'); |
||
123 | $password = $input->getArgument('password'); |
||
124 | $locale = $input->getArgument('locale'); |
||
125 | $superAdmin = $input->getOption('super-admin'); |
||
126 | $inactive = $input->getOption('inactive'); |
||
127 | $groupOption = $input->getOption('group'); |
||
128 | |||
129 | if (null === $locale) { |
||
130 | $locale = $this->defaultLocale; |
||
131 | } |
||
132 | $command = $this->getApplication()->find('fos:user:create'); |
||
133 | $arguments = array( |
||
134 | 'command' => 'fos:user:create', |
||
135 | 'username' => $username, |
||
136 | 'email' => $email, |
||
137 | 'password' => $password, |
||
138 | '--super-admin' => $superAdmin, |
||
139 | '--inactive' => $inactive, |
||
140 | ); |
||
141 | |||
142 | $input = new ArrayInput($arguments); |
||
143 | $command->run($input, $output); |
||
144 | |||
145 | // Fetch user that was just created |
||
146 | $user = $this->em->getRepository($this->userClassname)->findOneBy(array('username' => $username)); |
||
147 | |||
148 | // Attach groups |
||
149 | $groupOutput = []; |
||
150 | |||
151 | foreach (explode(',', $groupOption) as $groupId) { |
||
152 | if ((int) $groupId === 0) { |
||
153 | foreach ($this->groups as $value) { |
||
154 | if ($groupId === $value->getName()) { |
||
155 | $group = $value; |
||
156 | |||
157 | break; |
||
158 | } |
||
159 | } |
||
160 | } else { |
||
161 | $group = $this->groups[$groupId]; |
||
162 | } |
||
163 | |||
164 | if (isset($group) && $group instanceof Group) { |
||
165 | $groupOutput[] = $group->getName(); |
||
166 | $user->getGroups()->add($group); |
||
167 | } else { |
||
168 | throw new \RuntimeException('The selected group(s) can\'t be found.'); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | // Set admin interface locale and enable password changed |
||
173 | $user->setAdminLocale($locale); |
||
174 | $user->setPasswordChanged(true); |
||
175 | |||
176 | // Persist |
||
177 | $this->em->persist($user); |
||
178 | $this->em->flush(); |
||
179 | |||
180 | $output->writeln(sprintf('Added user <comment>%s</comment> to groups <comment>%s</comment>', $input->getArgument('username'), implode(',', $groupOutput))); |
||
181 | |||
182 | return 0; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Interacts with the user. |
||
187 | * |
||
188 | * @param InputInterface $input The input |
||
189 | * @param OutputInterface $output The output |
||
190 | * |
||
191 | * @throws \InvalidArgumentException |
||
192 | */ |
||
193 | protected function interact(InputInterface $input, OutputInterface $output) |
||
194 | { |
||
195 | View Code Duplication | if (!$input->getArgument('username')) { |
|
196 | $question = new Question('Please choose a username:'); |
||
197 | $question->setValidator(function ($username) { |
||
198 | if (null === $username) { |
||
199 | throw new \InvalidArgumentException('Username can not be empty'); |
||
200 | } |
||
201 | |||
202 | return $username; |
||
203 | }); |
||
204 | $username = $this->getHelper('question')->ask( |
||
205 | $input, |
||
206 | $output, |
||
207 | $question |
||
208 | ); |
||
209 | $input->setArgument('username', $username); |
||
210 | } |
||
211 | |||
212 | View Code Duplication | if (!$input->getArgument('email')) { |
|
213 | $question = new Question('Please choose an email:'); |
||
214 | $question->setValidator(function ($email) { |
||
215 | if (null === $email) { |
||
216 | throw new \InvalidArgumentException('Email can not be empty'); |
||
217 | } |
||
218 | |||
219 | return $email; |
||
220 | }); |
||
221 | $email = $this->getHelper('question')->ask( |
||
222 | $input, |
||
223 | $output, |
||
224 | $question |
||
225 | ); |
||
226 | $input->setArgument('email', $email); |
||
227 | } |
||
228 | |||
229 | if (!$input->getArgument('password')) { |
||
230 | $question = new Question('Please choose a password:'); |
||
231 | $question->setHidden(true); |
||
232 | $question->setHiddenFallback(false); |
||
233 | $question->setValidator(function ($password) { |
||
234 | if (null === $password) { |
||
235 | throw new \InvalidArgumentException('Password can not be empty'); |
||
236 | } |
||
237 | |||
238 | return $password; |
||
239 | }); |
||
240 | $password = $this->getHelper('question')->ask( |
||
241 | $input, |
||
242 | $output, |
||
243 | $question |
||
244 | ); |
||
245 | |||
246 | $input->setArgument('password', $password); |
||
247 | } |
||
248 | |||
249 | if (!$input->getArgument('locale')) { |
||
250 | $locale = $this->getHelper('question')->ask( |
||
251 | $input, |
||
252 | $output, |
||
253 | new Question('Please enter the locale (or leave empty for default admin locale):') |
||
254 | ); |
||
255 | $input->setArgument('locale', $locale); |
||
256 | } |
||
257 | |||
258 | if (!$input->getOption('group')) { |
||
259 | $question = new ChoiceQuestion( |
||
260 | 'Please enter the group(s) the user should be a member of (multiple possible, separated by comma):', |
||
261 | $this->groups, |
||
262 | '' |
||
263 | ); |
||
264 | $question->setMultiselect(true); |
||
265 | $question->setValidator(function ($groupsInput) { |
||
266 | if (!$this->groups) { |
||
267 | throw new \RuntimeException('No user group(s) could be found'); |
||
268 | } |
||
269 | |||
270 | // Validate that the chosen group options exist in the available groups |
||
271 | $groupNames = array_unique(explode(',', $groupsInput)); |
||
272 | if (\count(array_intersect_key(array_flip($groupNames), $this->groups)) !== \count($groupNames)) { |
||
273 | throw new InvalidArgumentException('You have chosen non existing group(s)'); |
||
274 | } |
||
275 | |||
276 | if ($groupsInput === '') { |
||
277 | throw new \RuntimeException('Group(s) must be of type integer and can not be empty'); |
||
278 | } |
||
279 | |||
280 | return $groupsInput; |
||
281 | }); |
||
282 | |||
283 | // Group has to be imploded because $input->setOption expects a string |
||
284 | $groups = $this->getHelper('question')->ask($input, $output, $question); |
||
285 | |||
286 | $input->setOption('group', $groups); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | private function getGroups() |
||
302 | } |
||
303 |
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.