TableFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 5
c 7
b 0
f 1
lcom 0
cbo 5
dl 0
loc 59
ccs 22
cts 22
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 0 48 5
1
<?php
2
3
namespace Zortje\MySQLKeeper\Database;
4
5
use Zortje\MySQLKeeper\Database\Table\Column;
6
use Zortje\MySQLKeeper\Database\Table\ColumnCollection;
7
use Zortje\MySQLKeeper\Database\Table\Index;
8
use Zortje\MySQLKeeper\Database\Table\IndexCollection;
9
10
/**
11
 * Class TableFactory
12
 *
13
 * @package Zortje\MySQLKeeper\Database
14
 */
15
class TableFactory {
16
17
	/**
18
	 * Create table
19
	 *
20
	 * @param string $tableName Table name
21
	 * @param \PDO   $pdo       Database connection
22
	 *
23
	 * @return Table
24
	 */
25 3
	public static function create($tableName, \PDO $pdo) {
26
		/**
27
		 * Table
28
		 */
29 3
		$result = $pdo->query("SHOW TABLE STATUS LIKE '$tableName';");
30
31 3
		if ($result->rowCount() !== 1) {
32 1
			throw new \InvalidArgumentException(sprintf('Table %s was not found', $tableName));
33
		}
34
35 2
		$tableRow = $result->fetch();
36
37
		/**
38
		 * Columns
39
		 */
40 2
		$columns = new ColumnCollection();
41
42 2
		foreach ($pdo->query("SHOW FULL COLUMNS FROM `$tableName`;") as $row) {
43 2
			$column = new Column($row);
44
45 2
			$columns->add($column);
46 2
		}
47
48
		/**
49
		 * Indices
50
		 */
51 2
		$indexRows = [];
52
53 2
		foreach ($pdo->query("SHOW INDEX FROM `$tableName`;") as $row) {
54 2
			$indexRows[$row['Key_name']]['unique']        = $row['Non_unique'] === '0';
55 2
			$indexRows[$row['Key_name']]['columnNames'][] = $row['Column_name'];
56 2
		}
57
58 2
		$indices = new IndexCollection();
59
60 2
		foreach ($indexRows as $keyName => $indexArray) {
61 2
			$index = new Index($keyName, $indexArray['unique'], $indexArray['columnNames']);
62
63 2
			$indices->add($index);
64 2
		}
65
66
		/**
67
		 * Initialization
68
		 */
69 2
		$table = new Table($tableName, $tableRow['Collation'], $columns, $indices);
70
71 2
		return $table;
72
	}
73
}
74