Conditions | 12 |
Paths | 180 |
Total Lines | 91 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
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 |
||
74 | public function fetchFromZoho(AbstractZohoDao $dao, $incrementalSync = true, $twoWaysSync = true) |
||
75 | { |
||
76 | $tableName = ZohoDatabaseHelper::getTableName($dao, $this->prefix); |
||
77 | |||
78 | if ($incrementalSync) { |
||
79 | $this->logger->info("Copying incremental data for '$tableName'"); |
||
80 | // Let's get the last modification date: |
||
81 | $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(lastActivityTime) FROM '.$tableName); |
||
82 | if ($lastActivityTime !== null) { |
||
83 | $lastActivityTime = new \DateTime($lastActivityTime); |
||
84 | $this->logger->info('Last activity time: '.$lastActivityTime->format('c')); |
||
85 | // Let's add one second to the last activity time (otherwise, we are fetching again the last record in DB). |
||
86 | $lastActivityTime->add(new \DateInterval('PT1S')); |
||
87 | } |
||
88 | |||
89 | $records = $dao->getRecords(null, null, $lastActivityTime); |
||
90 | $deletedRecordIds = $dao->getDeletedRecordIds($lastActivityTime); |
||
91 | } else { |
||
92 | $this->logger->notice("Copying FULL data for '$tableName'"); |
||
93 | $records = $dao->getRecords(); |
||
94 | $deletedRecordIds = []; |
||
95 | } |
||
96 | $this->logger->info('Fetched '.count($records).' records'); |
||
97 | |||
98 | $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
||
99 | |||
100 | $flatFields = ZohoDatabaseHelper::getFlatFields($dao->getFields()); |
||
101 | $fieldsByName = []; |
||
102 | foreach ($flatFields as $field) { |
||
103 | $fieldsByName[$field['name']] = $field; |
||
104 | } |
||
105 | |||
106 | $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id'); |
||
107 | |||
108 | $this->connection->beginTransaction(); |
||
109 | |||
110 | foreach ($records as $record) { |
||
111 | $data = []; |
||
112 | $types = []; |
||
113 | foreach ($table->getColumns() as $column) { |
||
114 | if (in_array($column->getName(), ['id', 'uid'])) { |
||
115 | continue; |
||
116 | } else { |
||
117 | $field = $fieldsByName[$column->getName()]; |
||
118 | $getterName = $field['getter']; |
||
119 | $data[$column->getName()] = $record->$getterName(); |
||
120 | $types[$column->getName()] = $column->getType()->getName(); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | $select->execute(['id' => $record->getZohoId()]); |
||
125 | $result = $select->fetch(\PDO::FETCH_ASSOC); |
||
126 | if ($result === false) { |
||
127 | $this->logger->debug("Inserting record with ID '".$record->getZohoId()."'."); |
||
128 | |||
129 | $data['id'] = $record->getZohoId(); |
||
130 | $types['id'] = 'string'; |
||
131 | |||
132 | $this->connection->insert($tableName, $data, $types); |
||
133 | |||
134 | foreach ($this->listeners as $listener) { |
||
135 | $listener->onInsert($data, $dao); |
||
136 | } |
||
137 | } else { |
||
138 | $this->logger->debug("Updating record with ID '".$record->getZohoId()."'."); |
||
139 | $identifier = ['id' => $record->getZohoId()]; |
||
140 | $types['id'] = 'string'; |
||
141 | |||
142 | $this->connection->update($tableName, $data, $identifier, $types); |
||
143 | |||
144 | // Let's add the id for the update trigger |
||
145 | $data['id'] = $record->getZohoId(); |
||
146 | foreach ($this->listeners as $listener) { |
||
147 | $listener->onUpdate($data, $result, $dao); |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | $sqlStatementUid = 'select uid from '.$this->connection->quoteIdentifier($tableName).' where id = :id'; |
||
152 | foreach ($deletedRecordIds as $id) { |
||
153 | $uid = $this->connection->fetchColumn($sqlStatementUid, ['id' => $id]); |
||
154 | $this->connection->delete($tableName, ['id' => $id]); |
||
155 | if ($twoWaysSync) { |
||
156 | // TODO: we could detect if there are changes to be updated to the server and try to warn with a log message |
||
157 | // Also, let's remove the newly created field (because of the trigger) to avoid looping back to Zoho |
||
158 | $this->connection->delete('local_delete', ['table_name' => $tableName, 'id' => $id]); |
||
159 | $this->connection->delete('local_update', ['table_name' => $tableName, 'uid' => $uid]); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | $this->connection->commit(); |
||
164 | } |
||
165 | } |
||
166 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.