| Conditions | 15 |
| Paths | 220 |
| Total Lines | 117 |
| Lines | 20 |
| Ratio | 17.09 % |
| 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 |
||
| 64 | public function setupDatabase($username) { |
||
| 65 | $e_host = \addslashes($this->dbHost); |
||
| 66 | $e_dbname = \addslashes($this->dbName); |
||
| 67 | //check if the database user has admin right |
||
| 68 | View Code Duplication | if ($e_host == '') { |
|
| 69 | $easy_connect_string = $e_dbname; // use dbname as easy connect name |
||
| 70 | } else { |
||
| 71 | $easy_connect_string = '//'.$e_host.'/'.$e_dbname; |
||
| 72 | } |
||
| 73 | $this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']); |
||
| 74 | $connection = @\oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
||
| 75 | if (!$connection) { |
||
| 76 | $errorMessage = $this->getLastError(); |
||
| 77 | if ($errorMessage) { |
||
| 78 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'), |
||
| 79 | $errorMessage.' Check environment: ORACLE_HOME='.\getenv('ORACLE_HOME') |
||
| 80 | .' ORACLE_SID='.\getenv('ORACLE_SID') |
||
| 81 | .' LD_LIBRARY_PATH='.\getenv('LD_LIBRARY_PATH') |
||
| 82 | .' NLS_LANG='.\getenv('NLS_LANG') |
||
| 83 | .' tnsnames.ora is '.(\is_readable(\getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
||
| 84 | } |
||
| 85 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
||
| 86 | 'Check environment: ORACLE_HOME='.\getenv('ORACLE_HOME') |
||
| 87 | .' ORACLE_SID='.\getenv('ORACLE_SID') |
||
| 88 | .' LD_LIBRARY_PATH='.\getenv('LD_LIBRARY_PATH') |
||
| 89 | .' NLS_LANG='.\getenv('NLS_LANG') |
||
| 90 | .' tnsnames.ora is '.(\is_readable(\getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
||
| 91 | } |
||
| 92 | //check for roles creation rights in oracle |
||
| 93 | |||
| 94 | $query="SELECT CASE WHEN (count(*) = 6) THEN 1 ELSE 0 END AS all_privileges |
||
| 95 | FROM user_role_privs a1 |
||
| 96 | INNER JOIN role_sys_privs a2 ON a1.granted_role = a2.role |
||
| 97 | WHERE ( a2.privilege IN ('CREATE USER', 'ALTER USER') ) |
||
| 98 | OR ( a2.privilege IN ( |
||
| 99 | 'CREATE SESSION', |
||
| 100 | 'CREATE TABLE', |
||
| 101 | 'CREATE SEQUENCE', |
||
| 102 | 'CREATE TRIGGER' |
||
| 103 | ) AND a2.admin_option = 'YES')"; |
||
| 104 | $stmt = \oci_parse($connection, $query); |
||
| 105 | View Code Duplication | if (!$stmt) { |
|
| 106 | $entry = $this->trans->t('DB Error: "%s"', [$this->getLastError($connection)]) . '<br />'; |
||
| 107 | $entry .= $this->trans->t('Offending command was: "%s"', [$query]) . '<br />'; |
||
| 108 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
||
| 109 | } |
||
| 110 | $result = \oci_execute($stmt); |
||
| 111 | if ($result) { |
||
| 112 | $row = \oci_fetch_row($stmt); |
||
| 113 | |||
| 114 | if ($row[0] > 0) { |
||
| 115 | //use the admin login data for the new database user |
||
| 116 | |||
| 117 | //add prefix to the oracle user name to prevent collisions |
||
| 118 | $this->dbUser='oc_'.$username; |
||
| 119 | //create a new password so we don't need to store the admin config in the config file |
||
| 120 | $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
||
| 121 | |||
| 122 | //oracle passwords are treated as identifiers: |
||
| 123 | // must start with alphanumeric char |
||
| 124 | // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. |
||
| 125 | $this->dbPassword=\substr($this->dbPassword, 0, 30); |
||
| 126 | |||
| 127 | $this->createDBUser($connection); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->config->setSystemValues([ |
||
| 132 | 'dbuser' => $this->dbUser, |
||
| 133 | 'dbname' => $this->dbName, |
||
| 134 | 'dbpassword' => $this->dbPassword, |
||
| 135 | ]); |
||
| 136 | |||
| 137 | //create the database not necessary, oracle implies user = schema |
||
| 138 | //$this->createDatabase($this->dbname, $this->dbuser, $connection); |
||
| 139 | |||
| 140 | //FIXME check tablespace exists: select * from user_tablespaces |
||
| 141 | |||
| 142 | // the connection to dbname=oracle is not needed anymore |
||
| 143 | \oci_close($connection); |
||
| 144 | |||
| 145 | // connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled |
||
| 146 | $this->dbUser = $this->config->getSystemValue('dbuser'); |
||
| 147 | //$this->dbname = \OC_Config::getValue('dbname'); |
||
| 148 | $this->dbPassword = $this->config->getSystemValue('dbpassword'); |
||
| 149 | |||
| 150 | $e_host = \addslashes($this->dbHost); |
||
| 151 | $e_dbname = \addslashes($this->dbName); |
||
| 152 | |||
| 153 | View Code Duplication | if ($e_host == '') { |
|
| 154 | $easy_connect_string = $e_dbname; // use dbname as easy connect name |
||
| 155 | } else { |
||
| 156 | $easy_connect_string = '//'.$e_host.'/'.$e_dbname; |
||
| 157 | } |
||
| 158 | $connection = @\oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
||
| 159 | if (!$connection) { |
||
| 160 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
||
| 161 | $this->trans->t('You need to enter either an existing account or the administrator.')); |
||
| 162 | } |
||
| 163 | $query = "SELECT count(*) FROM user_tables WHERE table_name = :un"; |
||
| 164 | $stmt = \oci_parse($connection, $query); |
||
| 165 | $un = $this->tablePrefix.'users'; |
||
| 166 | \oci_bind_by_name($stmt, ':un', $un); |
||
| 167 | View Code Duplication | if (!$stmt) { |
|
| 168 | $entry = $this->trans->t('DB Error: "%s"', [$this->getLastError($connection)]) . '<br />'; |
||
| 169 | $entry .= $this->trans->t('Offending command was: "%s"', [$query]) . '<br />'; |
||
| 170 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
||
| 171 | } |
||
| 172 | $result = \oci_execute($stmt); |
||
| 173 | |||
| 174 | if ($result) { |
||
| 175 | $row = \oci_fetch_row($stmt); |
||
| 176 | } |
||
| 177 | if (!$result or $row[0]==0) { |
||
| 178 | \OC_DB::createDbFromStructure($this->dbDefinitionFile); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 273 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.