Completed
Push — 3.3.0 ( a70a1b )
by
unknown
06:18
created

ZohoDatabasePusher::array_clone()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 1
nop 1
1
<?php
2
3
namespace Wabel\Zoho\CRM\Copy;
4
5
use Doctrine\DBAL\Connection;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
use Wabel\Zoho\CRM\AbstractZohoDao;
9
use Wabel\Zoho\CRM\Helpers\BeanHelper;
10
use Wabel\Zoho\CRM\Service\EntitiesGeneratorService;
11
use Wabel\Zoho\CRM\ZohoBeanInterface;
12
use zcrmsdk\crm\api\response\EntityResponse;
13
use zcrmsdk\crm\crud\ZCRMRecord;
14
use zcrmsdk\crm\exception\ZCRMException;
15
16
/**
17
 * Description of ZohoDatabasePusher.
18
 *
19
 * @author rbergina
20
 */
21
class ZohoDatabasePusher
22
{
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var string
35
     */
36
    private $prefix;
37
38
    /**
39
     * @var ZohoChangeListener[]
40
     */
41
    private $listeners;
42
43
    /**
44
     * @param Connection $connection
45
     * @param int $apiLimitInsertUpdateDelete
46
     * @param string $prefix
47
     * @param LoggerInterface $logger
48
     * @param ZohoChangeListener[] $listeners
49
     */
50
    public function __construct(Connection $connection, $apiLimitInsertUpdateDelete = 100, $prefix = 'zoho_', LoggerInterface $logger = null, array $listeners = [])
51
    {
52
        $this->connection = $connection;
53
        $this->prefix = $prefix;
54
        if ($logger === null) {
55
            $this->logger = new NullLogger();
56
        } else {
57
            $this->logger = $logger;
58
        }
59
        $this->apiLimitInsertUpdateDelete = $apiLimitInsertUpdateDelete;
60
        if ($apiLimitInsertUpdateDelete === null) {
61
            $this->apiLimitInsertUpdateDelete = 100;
62
        }
63
        $this->listeners = $listeners;
64
    }
65
66
    /**
67
     * @var int
68
     */
69
    private $apiLimitInsertUpdateDelete;
70
71
    /**
72
     * @param AbstractZohoDao $zohoDao
73
     * @param bool $update
74
     * @return int
75
     */
76
    private function countElementInTable(AbstractZohoDao $zohoDao, $update = false)
77
    {
78
        $tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix);
79
        if ($update) {
80
            return (int)$this->connection->executeQuery('SELECT COUNT(DISTINCT uid) AS nb FROM `local_update` WHERE table_name LIKE :tableName AND error IS NULL', ['tableName' => $tableName])->fetchColumn();
81
        }
82
        return (int)$this->connection->executeQuery('SELECT COUNT(uid) AS nb FROM `local_insert` WHERE table_name LIKE :tableName AND error IS NULL', ['tableName' => $tableName])->fetchColumn();
83
    }
84
85
    /**
86
     * Insert or Update rows.
87
     *
88
     * @param AbstractZohoDao $zohoDao
89
     */
90
    public function pushDataToZoho(AbstractZohoDao $zohoDao, $update = false)
91
    {
92
        $localTable = $update ? 'local_update' : 'local_insert';
93
        $tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix);
94
        $countToPush = $this->countElementInTable($zohoDao, $update);
95
        $this->logger->notice($countToPush . ' records to ' . ($update ? 'update' : 'insert') . ' into Zoho for module ' . $zohoDao->getPluralModuleName());
96
        if ($countToPush) {
97
            do {
98
                $rowsDeleted = [];
99
                $zohoBeans = [];
100
                $localRecords = [];
101
                $localPreviousRecordValues = [];
102
                //@see https://www.zoho.com/crm/help/api/v2/#ra-update-records
103
                //To optimize your API usage, get maximum 200 records with each request and insert, update or delete maximum 100 records with each request.
104
105
                if ($update) {
106
                    $recordsToUpdateQuery = $this->connection->createQueryBuilder();
107
                    $recordsToUpdateQuery
108
                        ->select('DISTINCT table_name, uid')
109
                        ->from($localTable)
110
                        ->where('error IS NULL')
111
                        ->andWhere('table_name = :table_name')
112
                        ->setMaxResults($this->apiLimitInsertUpdateDelete)
113
                        ->setParameters([
114
                            'table_name' => $tableName
115
                        ]);
116
                    $recordsToUpdateResults = $recordsToUpdateQuery->execute()->fetchAll();
117
                    $this->logger->info(sprintf('Processing %s records to update...', count($recordsToUpdateResults)));
118
                    foreach ($recordsToUpdateResults as $result) {
119
                        $recordQuery = $this->connection->createQueryBuilder();
120
                        $recordQuery
121
                            ->select('*')
122
                            ->from($tableName)
123
                            ->where('uid = :uid')
124
                            ->setParameters([
125
                                'uid' => $result['uid']
126
                            ]);
127
                        $record = $recordQuery->execute()->fetch();
128
129 View Code Duplication
                        if (!$record) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
130
                            $errorMessage = sprintf('Impossible to find row with uid %s in the table %s', $result['uid'], $tableName);
131
                            $this->logger->warning($errorMessage);
132
                            $this->connection->update($localTable, [
133
                                'error' => $errorMessage,
134
                                'errorTime' => date('Y-m-d H:i:s'),
135
                            ], [
136
                                'uid' => $result['uid'],
137
                                'table_name' => $tableName
138
                            ]);
139
                            continue;
140
                        }
141
142 View Code Duplication
                        if (isset($zohoBeans[$record['uid']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
143
                            $zohoBean = $zohoBeans[$record['uid']];
144
                        } else {
145
                            $zohoBean = $zohoDao->create();
146
                        }
147
148
                        if ($record['id'] && !$zohoBean->getZohoId()) {
149
                            $zohoBean->setZohoId($record['id']);
150
                        }
151
152
                        $fieldsUpdatedQuery = $this->connection->createQueryBuilder();
153
                        $fieldsUpdatedQuery
154
                            ->select('field_name', 'previous_value')
155
                            ->from($localTable)
156
                            ->where('uid = :uid')
157
                            ->andWhere('table_name = :table_name')
158
                            ->andWhere('error IS NULL')
159
                            ->setParameters([
160
                                'uid' => $result['uid'],
161
                                'table_name' => $tableName,
162
                            ]);
163
                        $fieldsUpdatedResults = $fieldsUpdatedQuery->execute()->fetchAll();
164
165
                        $previousValues = [];
166
                        foreach ($fieldsUpdatedResults as $fieldResults) {
167
                            $columnName = $fieldResults['field_name'];
168
                            if (array_key_exists($columnName, $record)) {
169
                                $this->updateDataZohoBean($zohoDao, $zohoBean, $columnName, $record[$columnName]);
170
                                $previousValues[$columnName] = $fieldResults['previous_value'];
171 View Code Duplication
                            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
172
                                $errorMessage = sprintf('Impossible to find the column %s for row with uid %s in the table %s', $columnName, $result['uid'], $tableName);
173
                                $this->logger->warning($errorMessage);
174
                                $this->connection->update($localTable, [
175
                                    'error' => $errorMessage,
176
                                    'errorTime' => date('Y-m-d H:i:s'),
177
                                ], [
178
                                    'uid' => $result['uid'],
179
                                    'table_name' => $tableName,
180
                                    'field_name' => $columnName
181
                                ]);
182
                                continue;
183
                            }
184
                        }
185
186
                        $this->logger->debug(sprintf('Updated row %s (id: \'%s\') from table %s added in queue to be pushed.', $record['uid'], $record['id'], $tableName));
187
                        $zohoBeans[$record['uid']] = $zohoBean;
188
                        $localRecords[$record['uid']] = $record;
189
                        $localPreviousRecordValues[$record['uid']] = $previousValues;
190
                        $rowsDeleted[] = $record['uid'];
191
                    }
192
                } else {
193
                    $recordsToInsertQuery = $this->connection->createQueryBuilder();
194
                    $recordsToInsertQuery
195
                        ->select('DISTINCT table_name, uid')
196
                        ->from($localTable)
197
                        ->where('error IS NULL')
198
                        ->andWhere('table_name = :table_name')
199
                        ->setMaxResults($this->apiLimitInsertUpdateDelete)
200
                        ->setParameters([
201
                            'table_name' => $tableName
202
                        ]);
203
                    $recordsToInsertResults = $recordsToInsertQuery->execute()->fetchAll();
204
                    $this->logger->info(sprintf('Processing %s records to insert...', count($recordsToInsertResults)));
205
                    foreach ($recordsToInsertResults as $result) {
206
                        $recordQuery = $this->connection->createQueryBuilder();
207
                        $recordQuery
208
                            ->select('*')
209
                            ->from($tableName)
210
                            ->where('uid = :uid')
211
                            ->setParameters([
212
                                'uid' => $result['uid']
213
                            ]);
214
                        $record = $recordQuery->execute()->fetch();
215
216 View Code Duplication
                        if (!$record) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
217
                            $errorMessage = sprintf('Impossible to find row with uid %s in the table %s', $result['uid'], $tableName);
218
                            $this->logger->warning($errorMessage);
219
                            $this->connection->update($localTable, [
220
                                'error' => $errorMessage,
221
                                'errorTime' => date('Y-m-d H:i:s'),
222
                            ], [
223
                                'uid' => $result['uid'],
224
                                'table_name' => $tableName
225
                            ]);
226
                            continue;
227
                        }
228
229 View Code Duplication
                        if (isset($zohoBeans[$record['uid']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
230
                            $zohoBean = $zohoBeans[$record['uid']];
231
                        } else {
232
                            $zohoBean = $zohoDao->create();
233
                        }
234
235
                        $this->logger->debug(sprintf('New row with uid %s from table %s added in queue to be pushed.', $record['uid'], $tableName));
236
                        $this->insertDataZohoBean($zohoDao, $zohoBean, $record);
237
238
                        $zohoBeans[$record['uid']] = $zohoBean;
239
                        $localRecords[$record['uid']] = $record;
240
                        $localPreviousRecordValues[$record['uid']] = [];
241
                        $rowsDeleted[] = $record['uid'];
242
                    }
243
                }
244
                if (count($zohoBeans)) {
245
                    $this->sendDataToZohoCleanLocal($zohoDao, $zohoBeans, $rowsDeleted, $update, $localRecords, $localPreviousRecordValues);
246
                }
247
                $countToPush = $this->countElementInTable($zohoDao, $update);
248
            } while ($countToPush > 0);
249
        }
250
    }
251
252
    /**
253
     * @param AbstractZohoDao $zohoDao
254
     * @param ZohoBeanInterface[] $zohoBeans
255
     * @param string[] $rowsDeleted
256
     * @param bool $update
257
     * @param mixed[] $localRecords
258
     */
259
    private function sendDataToZohoCleanLocal(AbstractZohoDao $zohoDao, array $zohoBeans, $rowsDeleted, $update = false, array $localRecords = [], array $localPreviousRecordValues = [])
0 ignored issues
show
Unused Code introduced by
The parameter $rowsDeleted is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
260
    {
261
        $initialZohoBeans = $this->array_clone($zohoBeans);
262
        $local_table = $update ? 'local_update' : 'local_insert';
263
        $tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix);
264
        $entityResponses = $zohoDao->save($zohoBeans);
265
        $responseKey = 0;
266
        foreach ($zohoBeans as $uid => $zohoBean) {
267
            $response = $entityResponses[$responseKey]->getResponseJSON();
268
            if (strtolower($response['code']) === 'success') {
269
                if ($update) {
270
                    $this->logger->debug(sprintf('Updated successfully the record with uid %s (id \'%s\') from the table %s', $uid, $zohoBean->getZohoId(), $tableName));
271
                    $this->connection->executeQuery(
272
                        'DELETE FROM local_update WHERE uid LIKE :uid AND table_name = :table_name AND error IS NULL',
273
                        [
274
                            'uid' => $uid,
275
                            'table_name' => $tableName
276
                        ]
277
                    );
278
                } else {
279
                    $countResult = (int)$this->connection->fetchColumn('select count(id) from ' . $tableName . ' where id = :id', ['id' => $zohoBean->getZohoId()]);
280
                    //If the sent data were duplicates Zoho can merged so we need to check if the Zoho ID already exist.
281
                    if ($countResult === 0) {
282
                        // ID not exist we can update the new row with the Zoho ID
283
                        $this->connection->beginTransaction();
284
                        $this->connection->update($tableName, ['id' => $zohoBean->getZohoId()], ['uid' => $uid]);
285
                        $this->connection->delete('local_insert', ['table_name' => $tableName, 'uid' => $uid]);
286
                        $this->connection->commit();
287
                        $this->logger->debug(sprintf('Inserted successfully the record with uid %s (id \'%s\') from the table %s', $uid, $zohoBean->getZohoId(), $tableName));
288
                    } else {
289
                        //ID already exist we need to delete the duplicate row.
290
                        $this->connection->beginTransaction();
291
                        $this->connection->delete($tableName, ['uid' => $uid]);
292
                        $this->connection->delete('local_insert', ['table_name' => $tableName, 'uid' => $uid]);
293
                        $this->connection->commit();
294
                        $this->logger->warning(sprintf('Duplicate record found when inserting record with uid %s from the table %s. ID updated: %s. UID deleted: %s', $uid, $tableName, $zohoBean->getZohoId(), $uid));
295
                    }
296
                    $record = $localRecords[$uid];
297
                    $record['id'] = $zohoBean->getZohoId();
298
299
                    foreach ($this->listeners as $listener) {
300
                        $listener->onInsert($record, $zohoDao);
301
                    }
302
                }
303
304
                if (method_exists($initialZohoBeans[$uid], 'setTag')) {
305
                    if ($initialZohoBeans[$uid]->isDirty('tag')) {
306
                        /** @var ZCRMRecord $zcrmRecord */
307
                        $zcrmRecord = $initialZohoBeans[$uid]->getZCRMRecord();
308
                        $zcrmRecord->setEntityId($zohoBean->getZohoId());
309
                        $zcrmRecord->setModuleApiName($zohoDao->getZCRMModule()->getAPIName());
310
                        try {
311
                            if ($initialZohoBeans[$uid]->getTag() === null) {
312
                                if (isset($localPreviousRecordValues[$uid]['tag']) && !empty($localPreviousRecordValues[$uid]['tag'])) {
313
                                    $zcrmRecord->removeTags(explode(';', $localPreviousRecordValues[$uid]['tag']));
0 ignored issues
show
Documentation introduced by
explode(';', $localPrevi...ordValues[$uid]['tag']) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
314
                                }
315
                            } else {
316
                                $zcrmRecord->addTags(explode(';', $initialZohoBeans[$uid]->getTag()), true);
0 ignored issues
show
Documentation introduced by
explode(';', $initialZohoBeans[$uid]->getTag()) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to ZCRMRecord::addTags() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
317
                            }
318
                        } catch (ZCRMException $ZCRMException) {
319
                            $errorMessage = sprintf('Cannot update tags for record uid %s from the table %s. ID updated: %s. Error: %s', $uid, $tableName, $zohoBean->getZohoId(), $ZCRMException->getMessage());
320
                            $this->connection->insert('local_update', [
321
                                'uid' => $uid,
322
                                'table_name' => $tableName,
323
                                'field_name' => 'tag',
324
                                'error' => $errorMessage,
325
                                'errorTime' => date('Y-m-d H:i:s')
326
                            ]);
327
                            $this->logger->error($errorMessage);
328
                        }
329
                    }
330
                }
331
332
            } else {
333
                $errorMessage = sprintf('An error occurred when %s record with uid %s from table %s into Zoho: %s', ($update ? 'updating' : 'inserting'), $uid, $tableName, json_encode($response));
334
                $this->logger->error($errorMessage);
335
                $this->connection->update($local_table, [
336
                    'error' => $errorMessage,
337
                    'errorTime' => date('Y-m-d H:i:s')
338
                ], [
339
                    'uid' => $uid,
340
                    'table_name' => $tableName
341
                ]);
342
            }
343
            $responseKey++;
344
        }
345
    }
346
347
    private function array_clone($array) {
348
        return array_map(function($element) {
349
            return ((is_array($element))
350
                ? $this->array_clone($element)
351
                : ((is_object($element))
352
                    ? clone $element
353
                    : $element
354
                )
355
            );
356
        }, $array);
357
    }
358
359
    private function endsWith($haystack, $needle)
360
    {
361
        return substr_compare($haystack, $needle, -strlen($needle)) === 0;
362
    }
363
364
    /**
365
     * Insert data to bean in order to insert zoho records.
366
     *
367
     * @param AbstractZohoDao $dao
368
     * @param ZohoBeanInterface $zohoBean
369
     * @param array $row
370
     */
371
    private function insertDataZohoBean(AbstractZohoDao $dao, ZohoBeanInterface $zohoBean, array $row)
372
    {
373
        foreach ($row as $columnName => $columnValue) {
374
            $fieldMethod = $dao->getFieldFromFieldName($columnName);
375 View Code Duplication
            if (!in_array($columnName, EntitiesGeneratorService::$defaultDateFields) && $fieldMethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
376
                && (!in_array($columnName, ['id', 'uid'])) && !is_null($columnValue)
377
            ) {
378
                // Changing only Name doesn't work properly on Zoho
379
                if ($this->endsWith($columnName, '_OwnerName') || $this->endsWith($columnName, '_Name')) {
380
                    continue;
381
                }
382
                $type = $fieldMethod->getType();
383
                $value = $this->formatValueToBeans($type, $columnValue);
384
                $setterMethod = $fieldMethod->getSetter();
385
                $zohoBean->{$setterMethod}($value);
386
            }
387
        }
388
    }
389
390
    /**
391
     * Insert data to bean in order to update zoho records.
392
     *
393
     * @param ZohoBeanInterface $zohoBean
394
     * @param array $fieldsMatching
0 ignored issues
show
Bug introduced by
There is no parameter named $fieldsMatching. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
395
     * @param type $columnName
396
     * @param type $valueDb
397
     */
398
    private function updateDataZohoBean(AbstractZohoDao $dao, ZohoBeanInterface $zohoBean, $columnName, $valueDb)
399
    {
400
        $fieldMethod = $dao->getFieldFromFieldName($columnName);
401 View Code Duplication
        if (!in_array($columnName, EntitiesGeneratorService::$defaultDateFields) && $fieldMethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
402
            && !in_array($columnName, ['id', 'uid'])
403
        ) {
404
            // Changing only Name doesn't work properly on Zoho
405
            if ($this->endsWith($columnName, '_OwnerName') || $this->endsWith($columnName, '_Name')) {
406
                return;
407
            }
408
            $type = $fieldMethod->getType();
409
            $value = is_null($valueDb) ? $valueDb : $this->formatValueToBeans($type, $valueDb);
410
            $setterMethod = $fieldMethod->getSetter();
411
            $zohoBean->{$setterMethod}($value);
412
        }
413
    }
414
415
    /**
416
     * Change the value to the good format.
417
     *
418
     * @param string $type
419
     * @param mixed $value
420
     *
421
     * @return mixed
422
     */
423
    private function formatValueToBeans($type, $value)
424
    {
425
        switch ($type) {
426
            case 'date':
427
                $value = \DateTime::createFromFormat('Y-m-d', $value) ?: null;
428
                break;
429
            case 'datetime':
430
                $value = \DateTime::createFromFormat('Y-m-d H:i:s', $value) ?: null;
431
                break;
432
            case 'boolean' :
433
                $value = (bool)$value;
434
                break;
435
            case 'percent' :
436
                $value = (int)$value;
437
                break;
438
            case 'double' :
439
                $value = (float)$value;
440
                break;
441
            case 'multiselectlookup':
442
            case 'multiuserlookup':
443
            case 'multiselectpicklist':
444
                $value = explode(';', $value);
445
                break;
446
        }
447
448
        return $value;
449
    }
450
451
    /**
452
     * Run deleted rows to Zoho : local_delete.
453
     *
454
     * @param AbstractZohoDao $zohoDao
455
     */
456
    public function pushDeletedRows(AbstractZohoDao $zohoDao)
457
    {
458
        $localTable = 'local_delete';
459
        $tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix);
460
        $statement = $this->connection->createQueryBuilder();
461
        $statement->select('l.id')
462
            ->from($localTable, 'l')
463
            ->where('l.table_name=:table_name')
464
            ->setParameters(
465
                [
466
                    'table_name' => $tableName,
467
                ]
468
            );
469
        $results = $statement->execute();
470
        $this->logger->notice($results->rowCount() . ' records to delete into Zoho for module ' . $zohoDao->getPluralModuleName());
471
        while ($row = $results->fetch()) {
472
            $zohoDao->delete($row['id']);
473
            $this->connection->delete($localTable, ['table_name' => $tableName, 'id' => $row['id']]);
474
        }
475
    }
476
477
    /**
478
     * Run inserted rows to Zoho : local_insert.
479
     *
480
     * @param AbstractZohoDao $zohoDao
481
     */
482
    public function pushInsertedRows(AbstractZohoDao $zohoDao)
483
    {
484
        $this->pushDataToZoho($zohoDao);
485
    }
486
487
    /**
488
     * Run updated rows to Zoho : local_update.
489
     *
490
     * @param AbstractZohoDao $zohoDao
491
     */
492
    public function pushUpdatedRows(AbstractZohoDao $zohoDao)
493
    {
494
        $this->pushDataToZoho($zohoDao, true);
495
    }
496
497
    /**
498
     * Push data from db to Zoho.
499
     *
500
     * @param AbstractZohoDao $zohoDao
501
     */
502
    public function pushToZoho(AbstractZohoDao $zohoDao)
503
    {
504
        $this->logger->info(sprintf('Pushing inserted rows for module %s into Zoho...', $zohoDao->getPluralModuleName()));
505
        $this->pushInsertedRows($zohoDao);
506
        $this->logger->info(sprintf('Pushing updated rows for module %s into Zoho...', $zohoDao->getPluralModuleName()));
507
        $this->pushUpdatedRows($zohoDao);
508
        $this->logger->info(sprintf('Pushing deleted rows for module %s into Zoho...', $zohoDao->getPluralModuleName()));
509
        $this->pushDeletedRows($zohoDao);
510
    }
511
}
512