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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class is in charge of synchronizing one table of your database with Zoho records. |
12
|
|
|
*/ |
13
|
|
|
class ZohoDatabaseCopier |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Connection |
17
|
|
|
*/ |
18
|
|
|
private $connection; |
19
|
|
|
|
20
|
|
|
private $prefix; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ZohoChangeListener[] |
24
|
|
|
*/ |
25
|
|
|
private $listeners; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var LoggerInterface |
29
|
|
|
*/ |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LocalChangesTracker |
34
|
|
|
*/ |
35
|
|
|
private $localChangesTracker; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* ZohoDatabaseCopier constructor. |
39
|
|
|
* |
40
|
|
|
* @param Connection $connection |
41
|
|
|
* @param string $prefix Prefix for the table name in DB |
42
|
|
|
* @param ZohoChangeListener[] $listeners The list of listeners called when a record is inserted or updated. |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function __construct(Connection $connection, $prefix = 'zoho_', array $listeners = [], LoggerInterface $logger = null) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->connection = $connection; |
47
|
|
|
$this->prefix = $prefix; |
48
|
|
|
$this->listeners = $listeners; |
49
|
|
|
if ($logger === null) { |
50
|
|
|
$this->logger = new NullLogger(); |
51
|
|
|
} else { |
52
|
|
|
$this->logger = $logger; |
53
|
|
|
} |
54
|
|
|
$this->localChangesTracker = new LocalChangesTracker($connection, $this->logger); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param LoggerInterface $logger |
59
|
|
|
*/ |
60
|
|
|
public function setLogger(LoggerInterface $logger) |
61
|
|
|
{ |
62
|
|
|
$this->logger = $logger; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param AbstractZohoDao $dao |
67
|
|
|
* @param bool $incrementalSync Whether we synchronize only the modified files or everything. |
68
|
|
|
* @param bool $twoWaysSync |
69
|
|
|
* |
70
|
|
|
* @throws \Doctrine\DBAL\DBALException |
71
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
72
|
|
|
* @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException |
73
|
|
|
*/ |
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.