Complex classes like PdoHandlerDriver 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PdoHandlerDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class PdoHandlerDriver implements DataHandlerDriverInterface |
||
20 | { |
||
21 | use CondorcetVersion; |
||
22 | |||
23 | protected const SEGMENT = [300,100,50,10,1]; |
||
24 | |||
25 | protected $_handler; |
||
26 | protected $_transaction = false; |
||
27 | protected $_queryError = false; |
||
28 | |||
29 | // Database structure |
||
30 | protected $_struct; |
||
31 | // Prepare Query |
||
32 | protected $_prepare = []; |
||
33 | // Data CallBack function |
||
34 | public $_dataContextObject; |
||
35 | |||
36 | |||
37 | 5 | public function __construct (\PDO $bdd, bool $tryCreateTable = false, array $struct = ['tableName' => 'Entitys', 'primaryColumnName' => 'id', 'dataColumnName' => 'data']) |
|
38 | { |
||
39 | 5 | if (!$this->checkStructureTemplate($struct)) : |
|
40 | throw new CondorcetException; |
||
41 | endif; |
||
42 | |||
43 | 5 | $this->_struct = $struct; |
|
44 | |||
45 | 5 | $this->_handler = $bdd; |
|
46 | |||
47 | 5 | $this->_handler->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
48 | |||
49 | 5 | if ($tryCreateTable) : |
|
50 | 5 | $this->createTable(); |
|
51 | endif; |
||
52 | |||
53 | 5 | $this->initPrepareQuery(); |
|
54 | 5 | } |
|
55 | |||
56 | 1 | public function __destruct () |
|
57 | { |
||
58 | 1 | if ($this->_queryError) : |
|
59 | $this->_handler->rollback(); |
||
60 | $this->_transaction = false; |
||
61 | else : |
||
62 | 1 | $this->closeTransaction(); |
|
63 | endif; |
||
64 | 1 | } |
|
65 | |||
66 | |||
67 | // INTERNAL |
||
68 | |||
69 | 5 | protected function checkStructureTemplate (array &$struct) : bool |
|
70 | { |
||
71 | 5 | if ( !empty($struct['tableName']) && !empty($struct['primaryColumnName']) && !empty($struct['dataColumnName']) && |
|
72 | 5 | is_string($struct['tableName']) && is_string($struct['primaryColumnName']) && is_string($struct['dataColumnName']) |
|
73 | ) : |
||
74 | 5 | return true; |
|
75 | else : |
||
76 | return false; |
||
77 | endif; |
||
78 | } |
||
79 | |||
80 | 5 | public function createTable () : void |
|
81 | { |
||
82 | try { |
||
83 | 5 | $this->_handler->exec('CREATE TABLE IF NOT EXISTS '.$this->_struct['tableName'].' ('.$this->_struct['primaryColumnName'].' INTEGER PRIMARY KEY NOT NULL , '.$this->_struct['dataColumnName'].' BLOB NOT NULL )'); |
|
84 | } catch (\Exception $e) { |
||
85 | throw $e; |
||
86 | } |
||
87 | 5 | } |
|
88 | |||
89 | 5 | protected function initPrepareQuery () : void |
|
90 | { |
||
91 | 5 | $template = []; |
|
92 | |||
93 | // Base - Small query ends |
||
94 | 5 | $template['end_template'] = ';'; |
|
95 | 5 | $template['insert_template'] = 'INSERT INTO '.$this->_struct['tableName'].' ('.$this->_struct['primaryColumnName'].', '.$this->_struct['dataColumnName'].') VALUES '; |
|
96 | 5 | $template['delete_template'] = 'DELETE FROM '.$this->_struct['tableName'].' WHERE '.$this->_struct['primaryColumnName']; |
|
97 | 5 | $template['select_template'] = 'SELECT '.$this->_struct['primaryColumnName'].','.$this->_struct['dataColumnName'].' FROM '.$this->_struct['tableName'].' WHERE '.$this->_struct['primaryColumnName']; |
|
98 | |||
99 | // Select the max / min key value. Usefull if array cursor is lost on DataManager. |
||
100 | 5 | $this->_prepare['selectMaxKey'] = $this->_handler->prepare('SELECT max('.$this->_struct['primaryColumnName'].') FROM '.$this->_struct['tableName'] . $template['end_template']); |
|
101 | 5 | $this->_prepare['selectMinKey'] = $this->_handler->prepare('SELECT min('.$this->_struct['primaryColumnName'].') FROM '.$this->_struct['tableName'] . $template['end_template']); |
|
102 | |||
103 | // Insert many Entitys |
||
104 | 5 | $makeMany = function ($how) use (&$template) { |
|
105 | 5 | $query = $template['insert_template']; |
|
106 | |||
107 | 5 | for ($i=1; $i < $how; $i++) : |
|
108 | 5 | $query .= '(:key'.$i.', :data'.$i.'),'; |
|
109 | endfor; |
||
110 | |||
111 | 5 | $query .= '(:key'.$how.', :data'.$how.')' . $template['end_template']; |
|
112 | |||
113 | 5 | return $query; |
|
114 | 5 | }; |
|
115 | |||
116 | 5 | foreach (self::SEGMENT as $value) : |
|
117 | 5 | $this->_prepare['insert'.$value.'Entitys'] = $this->_handler->prepare($makeMany($value)); |
|
118 | endforeach; |
||
119 | |||
120 | // Delete one Entity |
||
121 | 5 | $this->_prepare['deleteOneEntity'] = $this->_handler->prepare($template['delete_template'] . ' = ?' . $template['end_template']); |
|
122 | |||
123 | // Get a Entity |
||
124 | 5 | $this->_prepare['selectOneEntity'] = $this->_handler->prepare($template['select_template'] . ' = ?' . $template['end_template']); |
|
125 | |||
126 | // Get a range of Entity |
||
127 | 5 | $this->_prepare['selectRangeEntitys'] = $this->_handler->prepare($template['select_template'] . ' >= :startKey order by '.$this->_struct['primaryColumnName'].' asc LIMIT :limit' . $template['end_template']); |
|
128 | |||
129 | // Count Entitys |
||
130 | 5 | $this->_prepare['countEntitys'] = $this->_handler->prepare('SELECT count('.$this->_struct['primaryColumnName'].') FROM '. $this->_struct['tableName'] . $template['end_template']); |
|
131 | 5 | } |
|
132 | |||
133 | 5 | protected function initTransaction () : void |
|
139 | |||
140 | 5 | public function closeTransaction () : void |
|
150 | |||
151 | |||
152 | // DATA MANAGER |
||
153 | 5 | public function insertEntitys (array $input) : void |
|
188 | |||
189 | 5 | protected function sliceInput (array &$input) : void |
|
207 | |||
208 | |||
209 | 2 | public function deleteOneEntity (int $key, bool $justTry) : ?int |
|
229 | |||
230 | 1 | public function selectMaxKey () : ?int |
|
246 | |||
247 | 4 | public function selectMinKey () : int |
|
259 | |||
260 | 5 | public function countEntitys () : int |
|
272 | |||
273 | // return false if Entity does not exist. |
||
274 | 4 | public function selectOneEntity (int $key) |
|
291 | |||
292 | 5 | public function selectRangeEntitys (int $key, int $limit) : array |
|
315 | |||
316 | } |