Total Complexity | 45 |
Total Lines | 179 |
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 = []) { |
||
62 | //Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys |
||
63 | if (is_array($field)) $field = $field[0]; |
||
64 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
||
65 | |||
66 | try { |
||
67 | $this->addIndex(array_keys($query['args'])); |
||
68 | $this->addIndex(explode(',', $group)); |
||
69 | $result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
||
70 | |||
71 | if (isset($result[0]) && $group == null) return $result[0]->val; |
||
72 | else if ($group != null) { |
||
73 | $ret = []; |
||
74 | foreach ($result as $res) $ret[$res->$field] = $res->val; |
||
75 | return $ret; |
||
76 | } |
||
77 | else return 0; |
||
78 | } |
||
79 | catch (\Exception $e) { |
||
80 | $this->errors[] = $e; |
||
81 | return $group ? [] : 0; |
||
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) { |
||
112 | if ($mode == null) $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
||
113 | if (isset($options['limit']) != null) $limit = ' LIMIT ' . $options['limit']; |
||
114 | else $limit = ''; |
||
115 | |||
116 | $query = $this->selectBuilder->createSql($fields, $mode); |
||
117 | $query['sql'] = array_filter($query['sql']); |
||
118 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit)); |
||
119 | $this->addIndex(array_keys($query['args'])); |
||
120 | |||
121 | //Clear the cache |
||
122 | $this->cache = []; |
||
123 | $this->resultCache = []; |
||
124 | } |
||
125 | |||
126 | public function save($data, $tryagain = true) { |
||
166 | } |
||
167 | |||
168 | private function insert($table, array $primaryKey, $data) { |
||
169 | $error = 0; |
||
182 | } |
||
183 | } |
||
184 |