Conditions | 14 |
Paths | 108 |
Total Lines | 88 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
95 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
96 | $uid = $input->getArgument('uid'); |
||
97 | if ($this->userManager->userExists($uid)) { |
||
98 | $output->writeln('<error>The user "' . $uid . '" already exists.</error>'); |
||
99 | return 1; |
||
100 | } |
||
101 | |||
102 | // Validate email before we create the user |
||
103 | if ($input->getOption('email')) { |
||
104 | // Validate first |
||
105 | if(!$this->mailer->validateMailAddress($input->getOption('email'))) { |
||
106 | // Invalid! Error |
||
107 | $output->writeln('<error>Invalid email address supplied</error>'); |
||
108 | return 1; |
||
109 | } else { |
||
110 | $email = $input->getOption('email'); |
||
111 | } |
||
112 | } else { |
||
113 | $email = null; |
||
114 | } |
||
115 | |||
116 | if ($input->getOption('password-from-env')) { |
||
117 | $password = getenv('OC_PASS'); |
||
118 | if (!$password) { |
||
119 | $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
||
120 | return 1; |
||
121 | } |
||
122 | } elseif ($input->isInteractive()) { |
||
123 | /** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */ |
||
124 | $dialog = $this->getHelperSet()->get('question'); |
||
125 | $q = new Question('<question>Enter password: </question>',false); |
||
126 | $q->setHidden(true); |
||
127 | $password = $dialog->ask($input, $output, $q); |
||
128 | $q = new Question('<question>Confirm password: </question>',false); |
||
129 | $q->setHidden(true); |
||
130 | $confirm = $dialog->ask($input, $output, $q); |
||
131 | |||
132 | if ($password !== $confirm) { |
||
133 | $output->writeln("<error>Passwords did not match!</error>"); |
||
134 | return 1; |
||
135 | } |
||
136 | } else { |
||
137 | $output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>"); |
||
138 | return 1; |
||
139 | } |
||
140 | |||
141 | $user = $this->userManager->createUser( |
||
142 | $input->getArgument('uid'), |
||
143 | $password |
||
144 | ); |
||
145 | |||
146 | if ($user instanceof IUser) { |
||
147 | $output->writeln('<info>The user "' . $user->getUID() . '" was created successfully</info>'); |
||
148 | } else { |
||
149 | $output->writeln('<error>An error occurred while creating the user</error>'); |
||
150 | return 1; |
||
151 | } |
||
152 | |||
153 | if ($input->getOption('display-name')) { |
||
154 | $user->setDisplayName($input->getOption('display-name')); |
||
155 | $output->writeln('Display name set to "' . $user->getDisplayName() . '"'); |
||
156 | } |
||
157 | |||
158 | // Set email if supplied & valid |
||
159 | if(!is_null($email)) { |
||
160 | $user->setEMailAddress($email); |
||
161 | $output->writeln('Email address set to "' . $user->getEMailAddress() . '"'); |
||
162 | } |
||
163 | |||
164 | $groups = $input->getOption('group'); |
||
165 | |||
166 | if (!empty($groups)) { |
||
167 | // Make sure we init the Filesystem for the user, in case we need to |
||
168 | // init some group shares. |
||
169 | Filesystem::init($user->getUID(), ''); |
||
170 | } |
||
171 | |||
172 | foreach ($groups as $groupName) { |
||
173 | $group = $this->groupManager->get($groupName); |
||
174 | if (!$group) { |
||
175 | $this->groupManager->createGroup($groupName); |
||
176 | $group = $this->groupManager->get($groupName); |
||
177 | $output->writeln('Created group "' . $group->getGID() . '"'); |
||
178 | } |
||
179 | $group->addUser($user); |
||
180 | $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"'); |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 |