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 |
||
104 | private function createSpecificUser($username, $connection) { |
||
105 | try { |
||
106 | //user already specified in config |
||
107 | $oldUser = $this->config->getSystemValue('dbuser', false); |
||
108 | |||
109 | //we don't have a dbuser specified in config |
||
110 | if ($this->dbUser !== $oldUser) { |
||
111 | //add prefix to the admin username to prevent collisions |
||
112 | $adminUser = substr('oc_' . $username, 0, 16); |
||
113 | |||
114 | $i = 1; |
||
115 | while (true) { |
||
116 | //this should be enough to check for admin rights in mysql |
||
117 | $query = 'SELECT user FROM mysql.user WHERE user=?'; |
||
118 | $result = $connection->executeQuery($query, [$adminUser]); |
||
119 | |||
120 | //current dbuser has admin rights |
||
121 | if ($result) { |
||
122 | $data = $result->fetchAll(); |
||
123 | //new dbuser does not exist |
||
124 | if (count($data) === 0) { |
||
125 | //use the admin login data for the new database user |
||
126 | $this->dbUser = $adminUser; |
||
127 | |||
128 | //create a random password so we don't need to store the admin password in the config file |
||
129 | $this->dbPassword = $this->random->generate(30); |
||
130 | |||
131 | $this->createDBUser($connection); |
||
132 | |||
133 | break; |
||
134 | } else { |
||
135 | //repeat with different username |
||
136 | $length = strlen((string)$i); |
||
137 | $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
||
138 | $i++; |
||
139 | } |
||
140 | } else { |
||
141 | break; |
||
142 | } |
||
143 | }; |
||
144 | } |
||
145 | } catch (\Exception $ex) { |
||
146 | $this->logger->error('Specific user creation failed: {error}', [ |
||
147 | 'app' => 'mysql.setup', |
||
148 | 'error' => $ex->getMessage() |
||
149 | ]); |
||
150 | } |
||
151 | |||
152 | $this->config->setSystemValues([ |
||
153 | 'dbuser' => $this->dbUser, |
||
154 | 'dbpassword' => $this->dbPassword, |
||
155 | ]); |
||
156 | } |
||
157 | } |
||
158 |