Total Complexity | 58 |
Total Lines | 399 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TActiveRecordGateway 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 TActiveRecordGateway, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class TActiveRecordGateway extends \Prado\TComponent |
||
33 | { |
||
34 | private $_manager; |
||
35 | private $_tables = []; //table cache |
||
36 | private $_meta = []; //meta data cache. |
||
37 | private $_commandBuilders = []; |
||
38 | private $_currentRecord; |
||
39 | |||
40 | /** |
||
41 | * Constant name for specifying optional table name in TActiveRecord. |
||
42 | */ |
||
43 | const TABLE_CONST = 'TABLE'; |
||
44 | /** |
||
45 | * Method name for returning optional table name in in TActiveRecord |
||
46 | */ |
||
47 | const TABLE_METHOD = 'table'; |
||
48 | |||
49 | /** |
||
50 | * Record gateway constructor. |
||
51 | * @param TActiveRecordManager $manager |
||
52 | */ |
||
53 | public function __construct(TActiveRecordManager $manager) |
||
54 | { |
||
55 | $this->_manager = $manager; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return TActiveRecordManager record manager. |
||
60 | */ |
||
61 | protected function getManager() |
||
62 | { |
||
63 | return $this->_manager; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Gets the table name from the 'TABLE' constant of the active record |
||
|
|||
68 | * class if defined, otherwise use the class name as table name. |
||
69 | * @param TActiveRecord active record instance |
||
70 | * @return string table name for the given record class. |
||
71 | */ |
||
72 | protected function getRecordTableName(TActiveRecord $record) |
||
73 | { |
||
74 | $class = new ReflectionClass($record); |
||
75 | if($class->hasConstant(self::TABLE_CONST)) |
||
76 | { |
||
77 | $value = $class->getConstant(self::TABLE_CONST); |
||
78 | if(empty($value)) |
||
79 | throw new TActiveRecordException('ar_invalid_tablename_property', |
||
80 | get_class($record), self::TABLE_CONST); |
||
81 | return $value; |
||
82 | } |
||
83 | elseif ($class->hasMethod(self::TABLE_METHOD)) |
||
84 | { |
||
85 | $value = $record->{self::TABLE_METHOD}(); |
||
86 | if(empty($value)) |
||
87 | throw new TActiveRecordException('ar_invalid_tablename_method', |
||
88 | get_class($record), self::TABLE_METHOD); |
||
89 | return $value; |
||
90 | } |
||
91 | else |
||
92 | return strtolower(get_class($record)); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns table information, trys the application cache first. |
||
97 | * @param TActiveRecord $record |
||
98 | * @return TDbTableInfo table information. |
||
99 | */ |
||
100 | public function getRecordTableInfo(TActiveRecord $record) |
||
101 | { |
||
102 | $tableName = $this->getRecordTableName($record); |
||
103 | return $this->getTableInfo($record->getDbConnection(), $tableName); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns table information for table in the database connection. |
||
108 | * @param TDbConnection database connection |
||
109 | * @param string table name |
||
110 | * @return TDbTableInfo table details. |
||
111 | */ |
||
112 | public function getTableInfo(TDbConnection $connection, $tableName) |
||
113 | { |
||
114 | $connStr = $connection->getConnectionString(); |
||
115 | $key = $connStr . $tableName; |
||
116 | if(!isset($this->_tables[$key])) |
||
117 | { |
||
118 | //call this first to ensure that unserializing the cache |
||
119 | //will find the correct driver dependent classes. |
||
120 | if(!isset($this->_meta[$connStr])) |
||
121 | { |
||
122 | $this->_meta[$connStr] = TDbMetaData::getInstance($connection); |
||
123 | } |
||
124 | |||
125 | $tableInfo = null; |
||
126 | if(($cache = $this->getManager()->getCache()) !== null) |
||
127 | $tableInfo = $cache->get($key); |
||
128 | if(empty($tableInfo)) |
||
129 | { |
||
130 | $tableInfo = $this->_meta[$connStr]->getTableInfo($tableName); |
||
131 | if($cache !== null) |
||
132 | $cache->set($key, $tableInfo); |
||
133 | } |
||
134 | $this->_tables[$key] = $tableInfo; |
||
135 | } |
||
136 | return $this->_tables[$key]; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param TActiveRecord $record |
||
141 | * @return TDataGatewayCommand |
||
142 | */ |
||
143 | public function getCommand(TActiveRecord $record) |
||
144 | { |
||
145 | $conn = $record->getDbConnection(); |
||
146 | $connStr = $conn->getConnectionString(); |
||
147 | $tableInfo = $this->getRecordTableInfo($record); |
||
148 | if(!isset($this->_commandBuilders[$connStr])) |
||
149 | { |
||
150 | $builder = $tableInfo->createCommandBuilder($record->getDbConnection()); |
||
151 | $command = new TDataGatewayCommand($builder); |
||
152 | $command->OnCreateCommand[] = [$this, 'onCreateCommand']; |
||
153 | $command->OnExecuteCommand[] = [$this, 'onExecuteCommand']; |
||
154 | $this->_commandBuilders[$connStr] = $command; |
||
155 | |||
156 | } |
||
157 | $this->_commandBuilders[$connStr]->getBuilder()->setTableInfo($tableInfo); |
||
158 | $this->_currentRecord = $record; |
||
159 | return $this->_commandBuilders[$connStr]; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Raised when a command is prepared and parameter binding is completed. |
||
164 | * The parameter object is TDataGatewayEventParameter of which the |
||
165 | * {@link TDataGatewayEventParameter::getCommand Command} property can be |
||
166 | * inspected to obtain the sql query to be executed. |
||
167 | * This method also raises the OnCreateCommand event on the ActiveRecord |
||
168 | * object calling this gateway. |
||
169 | * @param TDataGatewayCommand originator $sender |
||
170 | * @param TDataGatewayEventParameter |
||
171 | */ |
||
172 | public function onCreateCommand($sender, $param) |
||
173 | { |
||
174 | $this->raiseEvent('OnCreateCommand', $this, $param); |
||
175 | if($this->_currentRecord !== null) |
||
176 | $this->_currentRecord->onCreateCommand($param); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Raised when a command is executed and the result from the database was returned. |
||
181 | * The parameter object is TDataGatewayResultEventParameter of which the |
||
182 | * {@link TDataGatewayEventParameter::getResult Result} property contains |
||
183 | * the data return from the database. The data returned can be changed |
||
184 | * by setting the {@link TDataGatewayEventParameter::setResult Result} property. |
||
185 | * This method also raises the OnCreateCommand event on the ActiveRecord |
||
186 | * object calling this gateway. |
||
187 | * @param TDataGatewayCommand originator $sender |
||
188 | * @param TDataGatewayResultEventParameter |
||
189 | */ |
||
190 | public function onExecuteCommand($sender, $param) |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Returns record data matching the given primary key(s). If the table uses |
||
199 | * composite key, specify the name value pairs as an array. |
||
200 | * @param TActiveRecord active record instance. |
||
201 | * @param array primary name value pairs |
||
202 | * @return array record data |
||
203 | */ |
||
204 | public function findRecordByPK(TActiveRecord $record, $keys) |
||
205 | { |
||
206 | $command = $this->getCommand($record); |
||
207 | return $command->findByPk($keys); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Returns records matching the list of given primary keys. |
||
212 | * @param TActiveRecord active record instance. |
||
213 | * @param array list of primary name value pairs |
||
214 | * @return array matching data. |
||
215 | */ |
||
216 | public function findRecordsByPks(TActiveRecord $record, $keys) |
||
217 | { |
||
218 | return $this->getCommand($record)->findAllByPk($keys); |
||
219 | } |
||
220 | |||
221 | |||
222 | /** |
||
223 | * Returns record data matching the given critera. If $iterator is true, it will |
||
224 | * return multiple rows as TDbDataReader otherwise it returns the <b>first</b> row data. |
||
225 | * @param TActiveRecord active record finder instance. |
||
226 | * @param TActiveRecordCriteria search criteria. |
||
227 | * @param boolean true to return multiple rows as iterator, false returns first row. |
||
228 | * @return mixed matching data. |
||
229 | */ |
||
230 | public function findRecordsByCriteria(TActiveRecord $record, $criteria, $iterator = false) |
||
231 | { |
||
232 | $command = $this->getCommand($record); |
||
233 | return $iterator ? $command->findAll($criteria) : $command->find($criteria); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Return record data from sql query. |
||
238 | * @param TActiveRecord active record finder instance. |
||
239 | * @param TActiveRecordCriteria sql query |
||
240 | * @return array result. |
||
241 | */ |
||
242 | public function findRecordBySql(TActiveRecord $record, $criteria) |
||
243 | { |
||
244 | return $this->getCommand($record)->findBySql($criteria); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Return record data from sql query. |
||
249 | * @param TActiveRecord active record finder instance. |
||
250 | * @param TActiveRecordCriteria sql query |
||
251 | * @return TDbDataReader result iterator. |
||
252 | */ |
||
253 | public function findRecordsBySql(TActiveRecord $record, $criteria) |
||
254 | { |
||
255 | return $this->getCommand($record)->findAllBySql($criteria); |
||
256 | } |
||
257 | |||
258 | public function findRecordsByIndex(TActiveRecord $record, $criteria, $fields, $values) |
||
259 | { |
||
260 | return $this->getCommand($record)->findAllByIndex($criteria, $fields, $values); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns the number of records that match the given criteria. |
||
265 | * @param TActiveRecord active record finder instance. |
||
266 | * @param TActiveRecordCriteria search criteria |
||
267 | * @return int number of records. |
||
268 | */ |
||
269 | public function countRecords(TActiveRecord $record, $criteria) |
||
270 | { |
||
271 | return $this->getCommand($record)->count($criteria); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Insert a new record. |
||
276 | * @param TActiveRecord new record. |
||
277 | * @return int number of rows affected. |
||
278 | */ |
||
279 | public function insert(TActiveRecord $record) |
||
280 | { |
||
281 | //$this->updateAssociatedRecords($record,true); |
||
282 | $result = $this->getCommand($record)->insert($this->getInsertValues($record)); |
||
283 | if($result) |
||
284 | $this->updatePostInsert($record); |
||
285 | //$this->updateAssociatedRecords($record); |
||
286 | return $result; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Sets the last insert ID to the corresponding property of the record if available. |
||
291 | * @param TActiveRecord record for insertion |
||
292 | */ |
||
293 | protected function updatePostInsert($record) |
||
294 | { |
||
295 | $command = $this->getCommand($record); |
||
296 | $tableInfo = $command->getTableInfo(); |
||
297 | foreach($tableInfo->getColumns() as $name => $column) |
||
298 | { |
||
299 | if($column->hasSequence()) |
||
300 | $record->setColumnValue($name, $command->getLastInsertID($column->getSequenceName())); |
||
301 | } |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param TActiveRecord record |
||
306 | * @return array insert values. |
||
307 | */ |
||
308 | protected function getInsertValues(TActiveRecord $record) |
||
309 | { |
||
310 | $values = []; |
||
311 | $tableInfo = $this->getCommand($record)->getTableInfo(); |
||
312 | foreach($tableInfo->getColumns() as $name => $column) |
||
313 | { |
||
314 | if($column->getIsExcluded()) |
||
315 | continue; |
||
316 | $value = $record->getColumnValue($name); |
||
317 | if(!$column->getAllowNull() && $value === null && !$column->hasSequence() && ($column->getDefaultValue() === TDbTableColumn::UNDEFINED_VALUE)) |
||
318 | { |
||
319 | throw new TActiveRecordException( |
||
320 | 'ar_value_must_not_be_null', get_class($record), |
||
321 | $tableInfo->getTableFullName(), $name); |
||
322 | } |
||
323 | if($value !== null) |
||
324 | $values[$name] = $value; |
||
325 | } |
||
326 | return $values; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Update the record. |
||
331 | * @param TActiveRecord dirty record. |
||
332 | * @return int number of rows affected. |
||
333 | */ |
||
334 | public function update(TActiveRecord $record) |
||
335 | { |
||
336 | //$this->updateAssociatedRecords($record,true); |
||
337 | list($data, $keys) = $this->getUpdateValues($record); |
||
338 | $result = $this->getCommand($record)->updateByPk($data, $keys); |
||
339 | //$this->updateAssociatedRecords($record); |
||
340 | return $result; |
||
341 | } |
||
342 | |||
343 | protected function getUpdateValues(TActiveRecord $record) |
||
344 | { |
||
345 | $values = []; |
||
346 | $tableInfo = $this->getCommand($record)->getTableInfo(); |
||
347 | $primary = []; |
||
348 | foreach($tableInfo->getColumns() as $name => $column) |
||
349 | { |
||
350 | if($column->getIsExcluded()) |
||
351 | continue; |
||
352 | $value = $record->getColumnValue($name); |
||
353 | if(!$column->getAllowNull() && $value === null && ($column->getDefaultValue() === TDbTableColumn::UNDEFINED_VALUE)) |
||
354 | { |
||
355 | throw new TActiveRecordException( |
||
356 | 'ar_value_must_not_be_null', get_class($record), |
||
357 | $tableInfo->getTableFullName(), $name); |
||
358 | } |
||
359 | if($column->getIsPrimaryKey()) |
||
360 | $primary[$name] = $value; |
||
361 | else |
||
362 | $values[$name] = $value; |
||
363 | } |
||
364 | return [$values,$primary]; |
||
365 | } |
||
366 | |||
367 | protected function updateAssociatedRecords(TActiveRecord $record, $updateBelongsTo = false) |
||
368 | { |
||
369 | $context = new TActiveRecordRelationContext($record); |
||
370 | return $context->updateAssociatedRecords($updateBelongsTo); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Delete the record. |
||
375 | * @param TActiveRecord record to be deleted. |
||
376 | * @return int number of rows affected. |
||
377 | */ |
||
378 | public function delete(TActiveRecord $record) |
||
381 | } |
||
382 | |||
383 | protected function getPrimaryKeyValues(TActiveRecord $record) |
||
384 | { |
||
385 | $tableInfo = $this->getCommand($record)->getTableInfo(); |
||
386 | $primary = []; |
||
387 | foreach($tableInfo->getColumns() as $name => $column) |
||
388 | { |
||
389 | if($column->getIsPrimaryKey()) |
||
390 | $primary[$name] = $record->getColumnValue($name); |
||
391 | } |
||
392 | return $primary; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Delete multiple records using primary keys. |
||
397 | * @param TActiveRecord finder instance. |
||
398 | * @return int number of rows deleted. |
||
399 | */ |
||
400 | public function deleteRecordsByPk(TActiveRecord $record, $keys) |
||
401 | { |
||
402 | return $this->getCommand($record)->deleteByPk($keys); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Delete multiple records by criteria. |
||
407 | * @param TActiveRecord active record finder instance. |
||
408 | * @param TActiveRecordCriteria search criteria |
||
409 | * @return int number of records. |
||
410 | */ |
||
411 | public function deleteRecordsByCriteria(TActiveRecord $record, $criteria) |
||
412 | { |
||
413 | return $this->getCommand($record)->delete($criteria); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Raise the corresponding command event, insert, update, delete or select. |
||
418 | * @param string command type |
||
419 | * @param TDbCommand sql command to be executed. |
||
420 | * @param TActiveRecord active record |
||
421 | * @param TActiveRecordCriteria data for the command. |
||
422 | */ |
||
423 | protected function raiseCommandEvent($event, $command, $record, $criteria) |
||
431 | } |
||
432 | } |
||
433 | |||
434 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths