Conditions | 17 |
Paths | 26 |
Total Lines | 152 |
Lines | 47 |
Ratio | 30.92 % |
Changes | 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 |
||
80 | public function pushDataToZoho(AbstractZohoDao $zohoDao, $update = false) |
||
81 | { |
||
82 | $localTable = $update ? 'local_update' : 'local_insert'; |
||
83 | $tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix); |
||
84 | $countToPush = $this->countElementInTable($zohoDao, $update); |
||
85 | $this->logger->notice($countToPush . ' records to ' . ($update ? 'update' : 'insert') . ' into Zoho for module ' . $zohoDao->getPluralModuleName()); |
||
86 | if ($countToPush) { |
||
87 | do { |
||
88 | $rowsDeleted = []; |
||
89 | $zohoBeans = []; |
||
90 | //@see https://www.zoho.com/crm/help/api/v2/#ra-update-records |
||
91 | //To optimize your API usage, get maximum 200 records with each request and insert, update or delete maximum 100 records with each request. |
||
92 | |||
93 | if ($update) { |
||
94 | $recordsToUpdateQuery = $this->connection->createQueryBuilder(); |
||
95 | $recordsToUpdateQuery |
||
96 | ->select('DISTINCT table_name, uid') |
||
97 | ->from($localTable) |
||
98 | ->where('error IS NULL') |
||
99 | ->andWhere('table_name = :table_name') |
||
100 | ->setMaxResults($this->apiLimitInsertUpdateDelete) |
||
101 | ->setParameters([ |
||
102 | 'table_name' => $tableName |
||
103 | ]); |
||
104 | $recordsToUpdateResults = $recordsToUpdateQuery->execute()->fetchAll(); |
||
105 | $this->logger->info(sprintf('Processing %s records to update...', count($recordsToUpdateResults))); |
||
106 | foreach ($recordsToUpdateResults as $result) { |
||
107 | $recordQuery = $this->connection->createQueryBuilder(); |
||
108 | $recordQuery |
||
109 | ->select('*') |
||
110 | ->from($tableName) |
||
111 | ->where('uid = :uid') |
||
112 | ->setParameters([ |
||
113 | 'uid' => $result['uid'] |
||
114 | ]); |
||
115 | $record = $recordQuery->execute()->fetch(); |
||
116 | |||
117 | View Code Duplication | if (!$record) { |
|
|
|||
118 | $errorMessage = sprintf('Impossible to find row with uid %s in the table %s', $result['uid'], $tableName); |
||
119 | $this->logger->warning($errorMessage); |
||
120 | $this->connection->update($localTable, [ |
||
121 | 'error' => $errorMessage, |
||
122 | 'errorTime' => date('Y-m-d H:i:s'), |
||
123 | ], [ |
||
124 | 'uid' => $result['uid'], |
||
125 | 'table_name' => $tableName |
||
126 | ]); |
||
127 | continue; |
||
128 | } |
||
129 | |||
130 | View Code Duplication | if (isset($zohoBeans[$record['uid']])) { |
|
131 | $zohoBean = $zohoBeans[$record['uid']]; |
||
132 | } else { |
||
133 | $zohoBean = $zohoDao->create(); |
||
134 | } |
||
135 | |||
136 | if ($record['id'] && !$zohoBean->getZohoId()) { |
||
137 | $zohoBean->setZohoId($record['id']); |
||
138 | } |
||
139 | |||
140 | $fieldsUpdatedQuery = $this->connection->createQueryBuilder(); |
||
141 | $fieldsUpdatedQuery |
||
142 | ->select('field_name') |
||
143 | ->from($localTable) |
||
144 | ->where('uid = :uid') |
||
145 | ->andWhere('table_name = :table_name') |
||
146 | ->andWhere('error IS NULL') |
||
147 | ->setParameters([ |
||
148 | 'uid' => $result['uid'], |
||
149 | 'table_name' => $tableName, |
||
150 | ]); |
||
151 | $fieldsUpdatedResults = $fieldsUpdatedQuery->execute()->fetchAll(); |
||
152 | |||
153 | foreach ($fieldsUpdatedResults as $fieldResults) { |
||
154 | $columnName = $fieldResults['field_name']; |
||
155 | if (array_key_exists($columnName, $record)) { |
||
156 | $this->updateDataZohoBean($zohoDao, $zohoBean, $columnName, $record[$columnName]); |
||
157 | View Code Duplication | } else { |
|
158 | $errorMessage = sprintf('Impossible to find the column %s for row with uid %s in the table %s', $columnName, $result['uid'], $tableName); |
||
159 | $this->logger->warning($errorMessage); |
||
160 | $this->connection->update($localTable, [ |
||
161 | 'error' => $errorMessage, |
||
162 | 'errorTime' => date('Y-m-d H:i:s'), |
||
163 | ], [ |
||
164 | 'uid' => $result['uid'], |
||
165 | 'table_name' => $tableName, |
||
166 | 'field_name' => $columnName |
||
167 | ]); |
||
168 | continue; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | $this->logger->debug(sprintf('Updated row %s (id: \'%s\') from table %s added in queue to be pushed.', $record['uid'], $record['id'], $tableName)); |
||
173 | $zohoBeans[$record['uid']] = $zohoBean; |
||
174 | $rowsDeleted[] = $record['uid']; |
||
175 | } |
||
176 | } else { |
||
177 | $recordsToInsertQuery = $this->connection->createQueryBuilder(); |
||
178 | $recordsToInsertQuery |
||
179 | ->select('DISTINCT table_name, uid') |
||
180 | ->from($localTable) |
||
181 | ->where('error IS NULL') |
||
182 | ->andWhere('table_name = :table_name') |
||
183 | ->setMaxResults($this->apiLimitInsertUpdateDelete) |
||
184 | ->setParameters([ |
||
185 | 'table_name' => $tableName |
||
186 | ]); |
||
187 | $recordsToInsertResults = $recordsToInsertQuery->execute()->fetchAll(); |
||
188 | $this->logger->info(sprintf('Processing %s records to insert...', count($recordsToInsertResults))); |
||
189 | foreach ($recordsToInsertResults as $result) { |
||
190 | $recordQuery = $this->connection->createQueryBuilder(); |
||
191 | $recordQuery |
||
192 | ->select('*') |
||
193 | ->from($tableName) |
||
194 | ->where('uid = :uid') |
||
195 | ->setParameters([ |
||
196 | 'uid' => $result['uid'] |
||
197 | ]); |
||
198 | $record = $recordQuery->execute()->fetch(); |
||
199 | |||
200 | View Code Duplication | if (!$record) { |
|
201 | $errorMessage = sprintf('Impossible to find row with uid %s in the table %s', $result['uid'], $tableName); |
||
202 | $this->logger->warning($errorMessage); |
||
203 | $this->connection->update($localTable, [ |
||
204 | 'error' => $errorMessage, |
||
205 | 'errorTime' => date('Y-m-d H:i:s'), |
||
206 | ], [ |
||
207 | 'uid' => $result['uid'], |
||
208 | 'table_name' => $tableName |
||
209 | ]); |
||
210 | continue; |
||
211 | } |
||
212 | |||
213 | View Code Duplication | if (isset($zohoBeans[$record['uid']])) { |
|
214 | $zohoBean = $zohoBeans[$record['uid']]; |
||
215 | } else { |
||
216 | $zohoBean = $zohoDao->create(); |
||
217 | } |
||
218 | |||
219 | $this->logger->debug(sprintf('New row with uid %s from table %s added in queue to be pushed.', $record['uid'], $tableName)); |
||
220 | $this->insertDataZohoBean($zohoDao, $zohoBean, $record); |
||
221 | $zohoBeans[$record['uid']] = $zohoBean; |
||
222 | $rowsDeleted[] = $record['uid']; |
||
223 | } |
||
224 | } |
||
225 | if (count($zohoBeans)) { |
||
226 | $this->sendDataToZohoCleanLocal($zohoDao, $zohoBeans, $rowsDeleted, $update); |
||
227 | } |
||
228 | $countToPush = $this->countElementInTable($zohoDao, $update); |
||
229 | } while ($countToPush > 0); |
||
230 | } |
||
231 | } |
||
232 | |||
442 |
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.