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
|
|
|
|
13
|
|
|
use Wabel\Zoho\CRM\AbstractZohoDao; |
14
|
|
|
|
15
|
|
|
class ZohoCopyDatabaseCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The list of Zoho DAOs to copy. |
19
|
|
|
* |
20
|
|
|
* @var AbstractZohoDao[] |
21
|
|
|
*/ |
22
|
|
|
private $zohoDaos; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ZohoDatabaseCopier |
26
|
|
|
*/ |
27
|
|
|
private $zohoDatabaseCopier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Lock |
31
|
|
|
*/ |
32
|
|
|
private $lock; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ZohoDatabaseCopier $zohoDatabaseCopier |
36
|
|
|
* @param \Wabel\Zoho\CRM\AbstractZohoDao[] $zohoDaos The list of Zoho DAOs to copy |
37
|
|
|
* @param Lock $lock A lock that can be used to avoid running the same command twice at the same time |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ZohoDatabaseCopier $zohoDatabaseCopier, array $zohoDaos, Lock $lock = null) |
40
|
|
|
{ |
41
|
|
|
parent::__construct(); |
42
|
|
|
$this->zohoDatabaseCopier = $zohoDatabaseCopier; |
43
|
|
|
$this->zohoDaos = $zohoDaos; |
44
|
|
|
$this->lock = $lock; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function configure() |
48
|
|
|
{ |
49
|
|
|
$this |
50
|
|
|
->setName('zoho:copy-db') |
51
|
|
|
->setDescription('Copies the Zoho database in local DB tables') |
52
|
|
|
->addOption("reset", "r", InputOption::VALUE_NONE, 'Get a fresh copy of Zoho (rather than doing incremental copy)'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
|
|
if ($this->lock) { |
59
|
|
|
$this->lock->acquireLock(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($input->getOption('reset')) { |
63
|
|
|
$incremental = false; |
64
|
|
|
} else { |
65
|
|
|
$incremental = true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->zohoDatabaseCopier->setLogger(new ConsoleLogger($output)); |
69
|
|
|
|
70
|
|
|
$output->writeln('Starting copying Zoho data into local database.'); |
71
|
|
|
foreach ($this->zohoDaos as $zohoDao) { |
72
|
|
|
$output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao))); |
73
|
|
|
$this->zohoDatabaseCopier->copy($zohoDao, $incremental); |
74
|
|
|
} |
75
|
|
|
$output->writeln('Zoho data successfully copied.'); |
76
|
|
|
if ($this->lock) { |
77
|
|
|
$this->lock->releaseLock(); |
78
|
|
|
} |
79
|
|
|
} catch (LockException $e) { |
80
|
|
|
$output->writeln('<error>Could not start zoho:copy-db command. Another zoho:copy-db command is already running.</error>'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|