| Conditions | 6 |
| Paths | 13 |
| Total Lines | 53 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 116 | private function createSpecificUser($username, $connection) { |
||
| 117 | try { |
||
| 118 | //user already specified in config |
||
| 119 | $oldUser = $this->config->getValue('dbuser', false); |
||
| 120 | |||
| 121 | //we don't have a dbuser specified in config |
||
| 122 | if ($this->dbUser !== $oldUser) { |
||
| 123 | //add prefix to the admin username to prevent collisions |
||
| 124 | $adminUser = substr('oc_' . $username, 0, 16); |
||
| 125 | |||
| 126 | $i = 1; |
||
| 127 | while (true) { |
||
| 128 | //this should be enough to check for admin rights in mysql |
||
| 129 | $query = 'SELECT user FROM mysql.user WHERE user=?'; |
||
| 130 | $result = $connection->executeQuery($query, [$adminUser]); |
||
| 131 | |||
| 132 | //current dbuser has admin rights |
||
| 133 | if ($result) { |
||
| 134 | $data = $result->fetchAll(); |
||
| 135 | //new dbuser does not exist |
||
| 136 | if (count($data) === 0) { |
||
| 137 | //use the admin login data for the new database user |
||
| 138 | $this->dbUser = $adminUser; |
||
| 139 | |||
| 140 | //create a random password so we don't need to store the admin password in the config file |
||
| 141 | $this->dbPassword = $this->random->generate(30); |
||
| 142 | |||
| 143 | $this->createDBUser($connection); |
||
| 144 | |||
| 145 | break; |
||
| 146 | } else { |
||
| 147 | //repeat with different username |
||
| 148 | $length = strlen((string)$i); |
||
| 149 | $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
||
| 150 | $i++; |
||
| 151 | } |
||
| 152 | } else { |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | }; |
||
| 156 | } |
||
| 157 | } catch (\Exception $ex) { |
||
| 158 | $this->logger->info('Can not create a new MySQL user, will continue with the provided user: {error}', [ |
||
| 159 | 'app' => 'mysql.setup', |
||
| 160 | 'error' => $ex->getMessage() |
||
| 161 | ]); |
||
| 162 | } |
||
| 163 | |||
| 164 | $this->config->setValues([ |
||
| 165 | 'dbuser' => $this->dbUser, |
||
| 166 | 'dbpassword' => $this->dbPassword, |
||
| 167 | ]); |
||
| 168 | } |
||
| 169 | } |
||
| 170 |