|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wabel\Zoho\CRM\Copy; |
|
4
|
|
|
|
|
5
|
|
|
use Mouf\Utils\Common\Lock; |
|
6
|
|
|
use Mouf\Utils\Common\LockException; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Wabel\Zoho\CRM\AbstractZohoDao; |
|
13
|
|
|
use Wabel\Zoho\CRM\Service\EntitiesGeneratorService; |
|
14
|
|
|
use Wabel\Zoho\CRM\ZohoClient; |
|
15
|
|
|
use Logger\Formatters\DateTimeFormatter; |
|
16
|
|
|
use Mouf\Utils\Log\Psr\MultiLogger; |
|
17
|
|
|
use Wabel\Zoho\CRM\Request\Response; |
|
18
|
|
|
use zcrmsdk\crm\utility\ZCRMConfigUtil; |
|
19
|
|
|
|
|
20
|
|
|
class ZohoSyncDatabaseCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* The list of Zoho DAOs to copy. |
|
24
|
|
|
* |
|
25
|
|
|
* @var AbstractZohoDao[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $zohoDaos; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ZohoDatabaseModelSync |
|
31
|
|
|
*/ |
|
32
|
|
|
private $zohoDatabaseModelSync; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ZohoDatabaseCopier |
|
36
|
|
|
*/ |
|
37
|
|
|
private $zohoDatabaseCopier; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ZohoDatabasePusher |
|
41
|
|
|
*/ |
|
42
|
|
|
private $zohoDatabaseSync; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* @var MultiLogger |
|
47
|
|
|
*/ |
|
48
|
|
|
private $logger; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var Lock |
|
52
|
|
|
*/ |
|
53
|
|
|
private $lock; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The Zoho Dao and Beans generator |
|
57
|
|
|
* |
|
58
|
|
|
* @var EntitiesGeneratorService |
|
59
|
|
|
*/ |
|
60
|
|
|
private $zohoEntitiesGenerator; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
* @var ZohoClient |
|
65
|
|
|
*/ |
|
66
|
|
|
private $zohoClient; |
|
67
|
|
|
|
|
68
|
|
|
private $pathZohoDaos; |
|
69
|
|
|
|
|
70
|
|
|
private $namespaceZohoDaos; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* |
|
74
|
|
|
* @var Response |
|
75
|
|
|
*/ |
|
76
|
|
|
private $usersResponse; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var string[] |
|
80
|
|
|
*/ |
|
81
|
|
|
private $excludedZohoDao; |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param ZohoDatabaseModelSync $zohoDatabaseModelSync |
|
86
|
|
|
* @param ZohoDatabaseCopier $zohoDatabaseCopier |
|
87
|
|
|
* @param ZohoDatabasePusher $zohoDatabaseSync |
|
88
|
|
|
* @param EntitiesGeneratorService $zohoEntitiesGenerator The Zoho Dao and Beans generator |
|
89
|
|
|
* @param ZohoClient $zohoClient |
|
90
|
|
|
* @param string $pathZohoDaos Tht path where we need to generate the Daos. |
|
91
|
|
|
* @param string $namespaceZohoDaos Daos namespace |
|
92
|
|
|
* @param MultiLogger $logger |
|
93
|
|
|
* @param Lock $lock A lock that can be used to avoid running the same command (copy) twice at the same time |
|
94
|
|
|
* @param string[] $excludedZohoDao To exclude Dao and or solve Dao which can create ZohoResponse Error |
|
95
|
|
|
*/ |
|
96
|
|
|
public function __construct(ZohoDatabaseModelSync $zohoDatabaseModelSync, ZohoDatabaseCopier $zohoDatabaseCopier, ZohoDatabasePusher $zohoDatabaseSync, |
|
97
|
|
|
EntitiesGeneratorService $zohoEntitiesGenerator, ZohoClient $zohoClient, |
|
98
|
|
|
$pathZohoDaos, $namespaceZohoDaos, MultiLogger $logger, Lock $lock = null, $excludedZohoDao = [] |
|
99
|
|
|
) { |
|
100
|
|
|
parent::__construct(); |
|
101
|
|
|
$this->zohoDatabaseModelSync = $zohoDatabaseModelSync; |
|
102
|
|
|
$this->zohoDatabaseCopier = $zohoDatabaseCopier; |
|
103
|
|
|
$this->zohoDatabaseSync = $zohoDatabaseSync; |
|
104
|
|
|
$this->zohoEntitiesGenerator = $zohoEntitiesGenerator; |
|
105
|
|
|
$this->zohoClient = $zohoClient; |
|
106
|
|
|
$this->pathZohoDaos = $pathZohoDaos; |
|
107
|
|
|
$this->namespaceZohoDaos = $namespaceZohoDaos; |
|
108
|
|
|
$this->logger = $logger; |
|
109
|
|
|
$this->lock = $lock; |
|
110
|
|
|
$this->excludedZohoDao = $excludedZohoDao; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function configure() |
|
114
|
|
|
{ |
|
115
|
|
|
$this |
|
116
|
|
|
->setName('zoho:sync') |
|
117
|
|
|
->setDescription('Synchronize the Zoho CRM data in a local database.') |
|
118
|
|
|
->addOption('reset', 'r', InputOption::VALUE_NONE, 'Get a fresh copy of Zoho (rather than doing incremental copy)') |
|
119
|
|
|
->addOption('skip-trigger', 's', InputOption::VALUE_NONE, 'Do not create or update the trigger') |
|
120
|
|
|
->addOption('fetch-only', 'f', InputOption::VALUE_NONE, 'Fetch only the Zoho data in local database') |
|
121
|
|
|
->addOption('push-only', 'p', InputOption::VALUE_NONE, 'Push only the local data to Zoho') |
|
122
|
|
|
->addOption('limit', 'l', InputOption::VALUE_NONE, 'use defined memory limit or unlimited memory limit') |
|
123
|
|
|
->addOption('log-path', null, InputOption::VALUE_NONE, 'Set the path of logs file') |
|
124
|
|
|
->addOption('clear-logs', null, InputOption::VALUE_NONE, 'Clear logs file at startup') |
|
125
|
|
|
->addOption('dump-logs', null, InputOption::VALUE_NONE, 'Dump logs into console when command finishes'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->logger->addLogger(new DateTimeFormatter(new ConsoleLogger($output))); |
|
131
|
|
|
try { |
|
132
|
|
|
if ($this->lock) { |
|
133
|
|
|
$this->lock->acquireLock(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// TODO: find a better way when zohocrm/php-sdk:src/crm/utility/Logger.php will allow to get the filename, delete, etc. |
|
137
|
|
|
if ($input->getOption('log-path') && $input->getOption('clear-logs')) { |
|
138
|
|
|
$output->writeln('Clearing logs...'); |
|
139
|
|
|
$path = $input->getOption('log-path'); |
|
140
|
|
|
$logFile = $path . '/ZCRMClientLibrary.log'; |
|
141
|
|
|
if (file_exists($logFile)) { |
|
142
|
|
|
if (is_writable($logFile)) { |
|
143
|
|
|
if (file_put_contents($logFile, '') === false) { |
|
144
|
|
|
$output->writeln(sprintf('<error>Error when clearing log file in %s</error>', $logFile)); |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
$output->writeln(sprintf('<warning>Cannot write into log file in %s</warning>', $logFile)); |
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
|
|
$output->writeln(sprintf('<warning>Cannot find log file in %s</warning>', $logFile)); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if(!$input->getOption('limit')) { |
|
155
|
|
|
ini_set('memory_limit', '-1'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if ($input->getOption('fetch-only') && $input->getOption('push-only')) { |
|
159
|
|
|
$output->writeln('<error>Options fetch-only and push-only are mutually exclusive.</error>'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->syncUserModel($output); |
|
163
|
|
|
|
|
164
|
|
|
$this->regenerateZohoDao($output); |
|
165
|
|
|
|
|
166
|
|
|
$this->syncModel($input, $output); |
|
167
|
|
|
|
|
168
|
|
|
if (!$input->getOption('push-only')) { |
|
169
|
|
|
$this->fetchUserDb($input, $output); |
|
170
|
|
|
$this->fetchDb($input, $output); |
|
171
|
|
|
} |
|
172
|
|
|
if (!$input->getOption('fetch-only')) { |
|
173
|
|
|
$this->pushDb($output); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if ($input->getOption('log-path') && $input->getOption('dump-logs')) { |
|
177
|
|
|
$output->writeln('Dumping logs...'); |
|
178
|
|
|
$path = $input->getOption('log-path'); |
|
179
|
|
|
$logFile = $path . '/ZCRMClientLibrary.log'; |
|
180
|
|
|
if (file_exists($logFile)) { |
|
181
|
|
|
if (is_readable($logFile)) { |
|
182
|
|
|
$output->writeln(file_get_contents($logFile)); |
|
183
|
|
|
} else { |
|
184
|
|
|
$output->writeln(sprintf('<warning>Cannot read into log file in %s</warning>', $logFile)); |
|
185
|
|
|
} |
|
186
|
|
|
} else { |
|
187
|
|
|
$output->writeln(sprintf('<warning>Cannot find log file in %s</warning>', $logFile)); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if ($this->lock) { |
|
192
|
|
|
$this->lock->releaseLock(); |
|
193
|
|
|
} |
|
194
|
|
|
} catch (LockException $e) { |
|
195
|
|
|
$output->writeln('<error>Could not start zoho:copy-db copy command. Another zoho:copy-db copy command is already running.</error>'); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Sychronizes the model of the database with Zoho records. |
|
201
|
|
|
* |
|
202
|
|
|
* @param OutputInterface $output |
|
203
|
|
|
*/ |
|
204
|
|
|
private function syncModel(InputInterface $input, OutputInterface $output) |
|
205
|
|
|
{ |
|
206
|
|
|
$twoWaysSync = !$input->getOption('fetch-only'); |
|
207
|
|
|
$skipCreateTrigger = $input->getOption('skip-trigger'); |
|
208
|
|
|
|
|
209
|
|
|
$output->writeln('Starting synchronize Zoho data into Zoho CRM.'); |
|
210
|
|
|
foreach ($this->zohoDaos as $zohoDao) { |
|
211
|
|
|
$this->zohoDatabaseModelSync->synchronizeDbModel($zohoDao, $twoWaysSync, $skipCreateTrigger); |
|
212
|
|
|
} |
|
213
|
|
|
$output->writeln('Zoho data successfully synchronized.'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Sychronizes the model of the database with Zoho Users records. |
|
218
|
|
|
* |
|
219
|
|
|
* @param OutputInterface $output |
|
220
|
|
|
*/ |
|
221
|
|
|
private function syncUserModel(OutputInterface $output) |
|
222
|
|
|
{ |
|
223
|
|
|
$output->writeln('Starting synchronize Zoho users model.'); |
|
224
|
|
|
$this->zohoDatabaseModelSync->synchronizeUserDbModel(); |
|
225
|
|
|
$output->writeln('Zoho users model successfully synchronized.'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Regerate Zoho Daos |
|
230
|
|
|
* |
|
231
|
|
|
* @param InputInterface $input |
|
|
|
|
|
|
232
|
|
|
* @param OutputInterface $output |
|
233
|
|
|
*/ |
|
234
|
|
|
private function regenerateZohoDao(OutputInterface $output) |
|
235
|
|
|
{ |
|
236
|
|
|
$output->writeln("Start to generate all the zoho daos."); |
|
237
|
|
|
$zohoModules = $this->zohoEntitiesGenerator->generateAll($this->pathZohoDaos, $this->namespaceZohoDaos); |
|
238
|
|
|
foreach ($zohoModules as $daoFullClassName) { |
|
239
|
|
|
/* @var $zohoDao AbstractZohoDao */ |
|
240
|
|
|
$zohoDao = new $daoFullClassName($this->zohoClient); |
|
241
|
|
|
//To have more module which is use time of modification (createdTime or lastActivityTime). |
|
242
|
|
|
//use an array of Excluded Dao by full namespace |
|
243
|
|
|
if (($this->excludedZohoDao && in_array(get_class($zohoDao), $this->excludedZohoDao))) { |
|
|
|
|
|
|
244
|
|
|
continue; |
|
245
|
|
|
} |
|
246
|
|
|
$this->zohoDaos [] = $zohoDao; |
|
247
|
|
|
$output->writeln(sprintf('<info>%s has created</info>', get_class($zohoDao))); |
|
248
|
|
|
} |
|
249
|
|
|
$output->writeln("Success to create all the zoho daos."); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Run the fetch User Db command. |
|
254
|
|
|
* |
|
255
|
|
|
* @param InputInterface $input |
|
256
|
|
|
* @param OutputInterface $output |
|
257
|
|
|
*/ |
|
258
|
|
|
private function fetchUserDb(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
259
|
|
|
{ |
|
260
|
|
|
$output->writeln('Starting copying Zoho users data into local database.'); |
|
261
|
|
|
$this->zohoDatabaseCopier->fetchUserFromZoho(); |
|
262
|
|
|
$output->writeln('Zoho users data successfully copied.'); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Run the fetch Db command. |
|
268
|
|
|
* |
|
269
|
|
|
* @param InputInterface $input |
|
270
|
|
|
* @param OutputInterface $output |
|
271
|
|
|
*/ |
|
272
|
|
|
private function fetchDb(InputInterface $input, OutputInterface $output) |
|
273
|
|
|
{ |
|
274
|
|
|
if ($input->getOption('reset')) { |
|
275
|
|
|
$incremental = false; |
|
276
|
|
|
} else { |
|
277
|
|
|
$incremental = true; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
$twoWaysSync = !$input->getOption('fetch-only'); |
|
281
|
|
|
|
|
282
|
|
|
$output->writeln('Starting copying Zoho data into local database.'); |
|
283
|
|
|
foreach ($this->zohoDaos as $zohoDao) { |
|
284
|
|
|
$output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao))); |
|
285
|
|
|
$this->zohoDatabaseCopier->fetchFromZoho($zohoDao, $incremental, $twoWaysSync); |
|
286
|
|
|
} |
|
287
|
|
|
$output->writeln('Zoho data successfully copied.'); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Run the push Db command. |
|
292
|
|
|
* |
|
293
|
|
|
* @param OutputInterface $output |
|
294
|
|
|
*/ |
|
295
|
|
|
private function pushDb(OutputInterface $output) |
|
296
|
|
|
{ |
|
297
|
|
|
$output->writeln('Starting synchronize Zoho data into Zoho CRM.'); |
|
298
|
|
|
foreach ($this->zohoDaos as $zohoDao) { |
|
299
|
|
|
if($zohoDao->getFieldFromFieldName('createdTime')) { |
|
300
|
|
|
$this->zohoDatabaseSync->pushToZoho($zohoDao); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
$output->writeln('Zoho data successfully synchronized.'); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.