Conditions | 12 |
Paths | 180 |
Total Lines | 90 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
207 | private function copyData(AbstractZohoDao $dao, $incrementalSync = true, $twoWaysSync = true) |
||
208 | { |
||
209 | $tableName = $this->getTableName($dao); |
||
210 | |||
211 | if ($incrementalSync) { |
||
212 | $this->logger->info("Copying incremental data for '$tableName'"); |
||
213 | // Let's get the last modification date: |
||
214 | $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(lastActivityTime) FROM '.$tableName); |
||
215 | if ($lastActivityTime !== null) { |
||
216 | $lastActivityTime = new \DateTime($lastActivityTime); |
||
217 | $this->logger->info("Last activity time: ".$lastActivityTime->format('c')); |
||
218 | // Let's add one second to the last activity time (otherwise, we are fetching again the last record in DB). |
||
219 | $lastActivityTime->add(new \DateInterval("PT1S")); |
||
220 | } |
||
221 | |||
222 | $records = $dao->getRecords(null, null, $lastActivityTime); |
||
223 | $deletedRecordIds = $dao->getDeletedRecordIds($lastActivityTime); |
||
224 | } else { |
||
225 | $this->logger->notice("Copying FULL data for '$tableName'"); |
||
226 | $records = $dao->getRecords(); |
||
227 | $deletedRecordIds = []; |
||
228 | } |
||
229 | $this->logger->info("Fetched ".count($records)." records"); |
||
230 | |||
231 | $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
||
232 | |||
233 | $flatFields = $this->getFlatFields($dao->getFields()); |
||
234 | $fieldsByName = []; |
||
235 | foreach ($flatFields as $field) { |
||
236 | $fieldsByName[$field['name']] = $field; |
||
237 | } |
||
238 | |||
239 | $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id'); |
||
240 | |||
241 | $this->connection->beginTransaction(); |
||
242 | |||
243 | foreach ($records as $record) { |
||
244 | $data = []; |
||
245 | $types = []; |
||
246 | foreach ($table->getColumns() as $column) { |
||
247 | if ($column->getName() === 'id') { |
||
248 | continue; |
||
249 | } else { |
||
250 | $field = $fieldsByName[$column->getName()]; |
||
251 | $getterName = $field['getter']; |
||
252 | $data[$column->getName()] = $record->$getterName(); |
||
253 | $types[$column->getName()] = $column->getType()->getName(); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | $select->execute(['id' => $record->getZohoId()]); |
||
258 | $result = $select->fetch(\PDO::FETCH_ASSOC); |
||
259 | if ($result === false) { |
||
260 | $this->logger->debug("Inserting record with ID '".$record->getZohoId()."'."); |
||
261 | |||
262 | $data['id'] = $record->getZohoId(); |
||
263 | $types['id'] = 'string'; |
||
264 | |||
265 | $this->connection->insert($tableName, $data, $types); |
||
266 | |||
267 | foreach ($this->listeners as $listener) { |
||
268 | $listener->onInsert($data, $dao); |
||
269 | } |
||
270 | } else { |
||
271 | $this->logger->debug("Updating record with ID '".$record->getZohoId()."'."); |
||
272 | $identifier = ['id' => $record->getZohoId()]; |
||
273 | $types['id'] = 'string'; |
||
274 | |||
275 | $this->connection->update($tableName, $data, $identifier, $types); |
||
276 | |||
277 | // Let's add the id for the update trigger |
||
278 | $data['id'] = $record->getZohoId(); |
||
279 | foreach ($this->listeners as $listener) { |
||
280 | $listener->onUpdate($data, $result, $dao); |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | foreach ($deletedRecordIds as $id) { |
||
286 | $this->connection->delete($tableName, [ 'id' => $id ]); |
||
287 | if ($twoWaysSync) { |
||
288 | // TODO: we could detect if there are changes to be updated to the server and try to warn with a log message |
||
289 | // Also, let's remove the newly created field (because of the trigger) to avoid looping back to Zoho |
||
290 | $this->connection->delete('local_delete', [ 'table_name' => $tableName, 'id' => $id ]); |
||
291 | $this->connection->delete('local_update', [ 'table_name' => $tableName, 'id' => $id ]); |
||
292 | } |
||
293 | } |
||
294 | |||
295 | $this->connection->commit(); |
||
296 | } |
||
297 | |||
323 |
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.