Completed
Push — master ( c58853...11c7a9 )
by Lukas
14:51
created

OCI   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 79
Duplicated Lines 8.86 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 79
rs 10
wmc 16
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 15 2
B validate() 7 11 5
B setupDatabase() 0 27 5
A getLastError() 0 13 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Andreas Fischer <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Jörn Friedrich Dreyer <[email protected]>
9
 * @author Manish Bisht <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Thomas Pulzer <[email protected]>
14
 * @author Victor Dubiniuk <[email protected]>
15
 *
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
32
namespace OC\Setup;
33
34
class OCI extends AbstractDatabase {
35
	public $dbprettyname = 'Oracle';
36
37
	protected $dbtablespace;
38
39
	public function initialize($config) {
40
		parent::initialize($config);
41
		if (array_key_exists('dbtablespace', $config)) {
42
			$this->dbtablespace = $config['dbtablespace'];
43
		} else {
44
			$this->dbtablespace = 'USERS';
45
		}
46
		// allow empty hostname for oracle
47
		$this->dbHost = $config['dbhost'];
48
49
		$this->config->setValues([
50
			'dbhost' => $this->dbHost,
51
			'dbtablespace' => $this->dbtablespace,
52
		]);
53
	}
54
55
	public function validate($config) {
56
		$errors = array();
57 View Code Duplication
		if (empty($config['dbuser']) && empty($config['dbname'])) {
58
			$errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname));
59
		} else if (empty($config['dbuser'])) {
60
			$errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname));
61
		} else if (empty($config['dbname'])) {
62
			$errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname));
63
		}
64
		return $errors;
65
	}
66
67
	public function setupDatabase($username) {
68
		try {
69
			$this->connect();
70
		} catch (\Exception $e) {
71
			$errorMessage = $this->getLastError();
72
			if ($errorMessage) {
73
				throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'),
74
					$errorMessage . ' Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME')
75
					. ' ORACLE_SID=' . getenv('ORACLE_SID')
76
					. ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH')
77
					. ' NLS_LANG=' . getenv('NLS_LANG')
78
					. ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable');
79
			}
80
			throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'),
81
				'Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME')
82
				. ' ORACLE_SID=' . getenv('ORACLE_SID')
83
				. ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH')
84
				. ' NLS_LANG=' . getenv('NLS_LANG')
85
				. ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable');
86
		}
87
88
		$this->config->setValues([
89
			'dbuser' => $this->dbUser,
90
			'dbname' => $this->dbName,
91
			'dbpassword' => $this->dbPassword,
92
		]);
93
	}
94
95
	/**
96
	 * @param resource $connection
97
	 * @return string
98
	 */
99
	protected function getLastError($connection = null) {
100
		if ($connection) {
101
			$error = oci_error($connection);
102
		} else {
103
			$error = oci_error();
104
		}
105
		foreach (array('message', 'code') as $key) {
106
			if (isset($error[$key])) {
107
				return $error[$key];
108
			}
109
		}
110
		return '';
111
	}
112
}
113