Completed
Push — master ( 499e80...f97bb0 )
by Jean-Christophe
01:49
created

DbModelsCreator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 4.84 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 3
loc 62
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A connect() 0 13 2
A getTablesName() 0 5 1
A getFieldsInfos() 3 9 2
A getPrimaryKeys() 0 9 2
A getForeignKeys() 0 10 1

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
namespace Ubiquity\orm\creator\database;
3
4
use Ubiquity\orm\creator\ModelsCreator;
5
6
7
class DbModelsCreator extends ModelsCreator{
8
	private $pdoObject;
9
10
	protected function init($config){
11
		parent::init($config);
12
		$this->connect($config["database"]);
13
	}
14
	/**
15
	 * Réalise la connexion à la base de données
16
	 */
17
	private function connect($config) {
18
		try {
19
			$this->pdoObject = new \PDO(
20
					$config["type"].':host=' . $config["serverName"] . ';dbname='
21
					. $config["dbName"] . ';port:' . $config["port"],
22
					$config["user"], $config["password"]);
23
			$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
24
			$this->pdoObject->exec("SET CHARACTER SET utf8");
25
26
		} catch (\PDOException $e) {
27
			print "Error!: " . $e->getMessage() . "<br/>";
28
		}
29
	}
30
31
32
	protected function getTablesName(){
33
		$sql = 'SHOW TABLES';
34
			$query = $this->pdoObject->query($sql);
35
			return $query->fetchAll(\PDO::FETCH_COLUMN);
36
	}
37
38
	protected function getFieldsInfos($tableName) {
39
		$fieldsInfos=array();
40
		$recordset = $this->pdoObject->query("SHOW COLUMNS FROM `{$tableName}`");
41
		$fields = $recordset->fetchAll(\PDO::FETCH_ASSOC);
42 View Code Duplication
		foreach ($fields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
43
			$fieldsInfos[$field['Field']] = ["Type"=>$field['Type'],"Nullable"=>$field["Null"]];
44
		}
45
		return $fieldsInfos;
46
	}
47
48
	protected function getPrimaryKeys($tableName){
49
		$fieldkeys=array();
50
		$recordset = $this->pdoObject->query("SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'");
51
		$keys = $recordset->fetchAll(\PDO::FETCH_ASSOC);
52
		foreach ($keys as $key) {
53
			$fieldkeys[] = $key['Column_name'];
54
		}
55
		return $fieldkeys;
56
	}
57
58
	protected function getForeignKeys($tableName,$pkName){
59
		$recordset = $this->pdoObject->query("SELECT *
60
												FROM
61
												 information_schema.KEY_COLUMN_USAGE
62
												WHERE
63
												 REFERENCED_TABLE_NAME = '".$tableName."'
64
												 AND REFERENCED_COLUMN_NAME = '".$pkName."'
65
												 AND TABLE_SCHEMA = '".$this->config["dbName"]."';");
66
		return $recordset->fetchAll(\PDO::FETCH_ASSOC);
67
	}
68
}
69