Total Complexity | 47 |
Total Lines | 187 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Database often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Database implements \Maphper\DataSource { |
||
4 | const EDIT_STRUCTURE = 1; |
||
5 | const EDIT_INDEX = 2; |
||
6 | const EDIT_OPTIMISE = 4; |
||
7 | |||
8 | private $table; |
||
9 | private $cache = []; |
||
10 | private $primaryKey; |
||
11 | private $fields = '*'; |
||
12 | private $defaultSort; |
||
13 | private $resultCache = []; |
||
14 | private $alterDb = false; |
||
15 | private $adapter; |
||
16 | private $crudBuilder; |
||
17 | |||
18 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
||
19 | $this->options = new DatabaseOptions($db, $options); |
||
|
|||
20 | $this->adapter = $this->options->getAdapter(); |
||
21 | |||
22 | $this->table = $table; |
||
23 | $this->primaryKey = is_array($primaryKey) ? $primaryKey : [$primaryKey]; |
||
24 | |||
25 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
||
26 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
||
27 | |||
28 | $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
||
29 | |||
30 | $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
||
31 | |||
32 | $this->alterDb = $this->options->getEditMode(); |
||
33 | |||
34 | if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
||
35 | } |
||
36 | |||
37 | public function getPrimaryKey() { |
||
38 | return $this->primaryKey; |
||
39 | } |
||
40 | |||
41 | public function deleteById($id) { |
||
44 | } |
||
45 | |||
46 | public function findById($id) { |
||
47 | if (!isset($this->cache[$id])) { |
||
48 | try { |
||
49 | $result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1])); |
||
50 | } |
||
51 | catch (\Exception $e) { |
||
52 | $this->errors[] = $e; |
||
53 | } |
||
54 | |||
55 | if (isset($result[0])) $this->cache[$id] = $result[0]; |
||
56 | else return null; |
||
57 | } |
||
58 | return $this->cache[$id]; |
||
59 | } |
||
60 | |||
61 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
||
82 | } |
||
83 | } |
||
84 | |||
85 | private function addIndex($args) { |
||
86 | if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
||
87 | } |
||
88 | |||
89 | public function findByField(array $fields, $options = []) { |
||
90 | $cacheId = md5(serialize(func_get_args())); |
||
91 | if (!isset($this->resultCache[$cacheId])) { |
||
92 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
||
93 | |||
94 | if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
||
95 | |||
96 | $query['sql'] = array_filter($query['sql']); |
||
97 | |||
98 | try { |
||
99 | $this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
||
100 | $this->addIndex(array_keys($query['args'])); |
||
101 | $this->addIndex(explode(',', $options['order'])); |
||
102 | } |
||
103 | catch (\Exception $e) { |
||
104 | $this->errors[] = $e; |
||
105 | $this->resultCache[$cacheId] = []; |
||
106 | } |
||
107 | } |
||
108 | return $this->resultCache[$cacheId]; |
||
109 | } |
||
110 | |||
111 | public function deleteByField(array $fields, array $options = [], $mode = null) { |
||
124 | } |
||
125 | |||
126 | private function getIfNew($data) { |
||
135 | } |
||
136 | |||
137 | public function save($data, $tryagain = true) { |
||
138 | $tryagain = $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
||
139 | $new = $this->getIfNew($data); |
||
140 | |||
141 | try { |
||
142 | $result = $this->insert($this->table, $this->primaryKey, $data); |
||
143 | |||
144 | //If there was an error but PDO is silent, trigger the catch block anyway |
||
145 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
||
146 | } |
||
147 | catch (\Exception $e) { |
||
148 | if ($tryagain) { |
||
149 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
||
150 | $this->save($data, false); |
||
151 | } |
||
152 | else throw $e; |
||
153 | } |
||
154 | |||
155 | if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
||
156 | //Something has changed, clear any cached results as they may now be incorrect |
||
157 | $this->resultCache = []; |
||
158 | $this->updateCache($data); |
||
159 | } |
||
160 | |||
161 | private function checkIfUpdateWorked($data) { |
||
162 | $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
||
163 | $matched = $this->findByField($updateWhere->getArgs()); |
||
164 | |||
165 | if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
||
166 | } |
||
167 | |||
168 | private function updateCache($data) { |
||
172 | } |
||
173 | |||
174 | private function insert($table, array $primaryKey, $data) { |
||
190 | } |
||
191 | } |
||
192 |