Cancelled
Push — 2.0 ( c33a68...ef9d4b )
by David
346:33 queued 346:33
created

ZohoSyncDatabaseCommand::execute()   C

Complexity

Conditions 8
Paths 81

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 3
Metric Value
c 4
b 2
f 3
dl 0
loc 25
rs 5.3846
cc 8
eloc 15
nc 81
nop 2
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
14
class ZohoSyncDatabaseCommand extends Command
15
{
16
    /**
17
     * The list of Zoho DAOs to copy.
18
     *
19
     * @var AbstractZohoDao[]
20
     */
21
    private $zohoDaos;
22
23
    /**
24
     * @var ZohoDatabaseModelSync
25
     */
26
    private $zohoDatabaseModelSync;
27
28
    /**
29
     * @var ZohoDatabaseCopier
30
     */
31
    private $zohoDatabaseCopier;
32
33
    /**
34
     * @var ZohoDatabasePusher
35
     */
36
    private $zohoDatabaseSync;
37
38
    /**
39
     * @var Lock
40
     */
41
    private $lock;
42
43
    /**
44
     * @param ZohoDatabaseModelSync $zohoDatabaseModelSync
45
     * @param ZohoDatabaseCopier    $zohoDatabaseCopier
46
     * @param ZohoDatabasePusher    $zohoDatabaseSync
47
     * @param AbstractZohoDao[]                 $zohoDaos              The list of Zoho DAOs to copy
48
     * @param Lock                  $lock                  A lock that can be used to avoid running the same command (copy) twice at the same time
49
     */
50
    public function __construct(ZohoDatabaseModelSync $zohoDatabaseModelSync, ZohoDatabaseCopier $zohoDatabaseCopier, ZohoDatabasePusher $zohoDatabaseSync, array $zohoDaos, Lock $lock = null)
51
    {
52
        parent::__construct();
53
        $this->zohoDatabaseModelSync = $zohoDatabaseModelSync;
54
        $this->zohoDatabaseCopier = $zohoDatabaseCopier;
55
        $this->zohoDatabaseSync = $zohoDatabaseSync;
56
        $this->zohoDaos = $zohoDaos;
57
        $this->lock = $lock;
58
    }
59
60
    protected function configure()
61
    {
62
        $this
63
            ->setName('zoho:sync')
64
            ->setDescription('Synchronize the Zoho CRM data in a local database.')
65
            ->addOption('reset', 'r', InputOption::VALUE_NONE, 'Get a fresh copy of Zoho (rather than doing incremental copy)')
66
            ->addOption('skip-trigger', 's', InputOption::VALUE_NONE, 'Do not create or update the trigger')
67
            ->addOption('fetch-only', 'f', InputOption::VALUE_NONE, 'Fetch only the Zoho data in local database')
68
            ->addOption('push-only', 'p', InputOption::VALUE_NONE, 'Push only the local data to Zoho');
69
    }
70
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        try {
74
            if ($this->lock) {
75
                $this->lock->acquireLock();
76
            }
77
            if ($input->getOption('fetch-only') && $input->getOption('push-only')) {
78
                $output->writeln('<error>Options fetch-only and push-only are mutually exclusive.</error>');
79
            }
80
81
            $this->syncModel($input, $output);
82
83
            if (!$input->getOption('push-only')) {
84
                $this->fetchDb($input, $output);
85
            }
86
            if (!$input->getOption('fetch-only')) {
87
                $this->pushDb($output);
88
            }
89
            if ($this->lock) {
90
                $this->lock->releaseLock();
91
            }
92
        } catch (LockException $e) {
93
            $output->writeln('<error>Could not start zoho:copy-db copy command. Another zoho:copy-db copy command is already running.</error>');
94
        }
95
    }
96
97
    /**
98
     * Sychronizes the model of the database with Zoho records.
99
     *
100
     * @param OutputInterface $output
101
     */
102
    private function syncModel(InputInterface $input, OutputInterface $output)
103
    {
104
        $this->zohoDatabaseModelSync->setLogger(new ConsoleLogger($output));
105
106
        $twoWaysSync = !$input->getOption('fetch-only');
107
        $skipCreateTrigger = $input->getOption('skip-trigger');
108
109
        $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
110
        foreach ($this->zohoDaos as $zohoDao) {
111
            $this->zohoDatabaseModelSync->synchronizeDbModel($zohoDao, $twoWaysSync, $skipCreateTrigger);
112
        }
113
        $output->writeln('Zoho data successfully synchronized.');
114
    }
115
116
    /**
117
     * Run the fetch Db command.
118
     *
119
     * @param InputInterface  $input
120
     * @param OutputInterface $output
121
     */
122
    private function fetchDb(InputInterface $input, OutputInterface $output)
123
    {
124
        if ($input->getOption('reset')) {
125
            $incremental = false;
126
        } else {
127
            $incremental = true;
128
        }
129
130
        $twoWaysSync = !$input->getOption('fetch-only');
131
        $this->zohoDatabaseCopier->setLogger(new ConsoleLogger($output));
132
133
        $output->writeln('Starting copying Zoho data into local database.');
134
        foreach ($this->zohoDaos as $zohoDao) {
135
            $output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao)));
136
            $this->zohoDatabaseCopier->fetchFromZoho($zohoDao, $incremental, $twoWaysSync);
137
        }
138
        $output->writeln('Zoho data successfully copied.');
139
    }
140
141
    /**
142
     * Run the push Db command.
143
     *
144
     * @param OutputInterface $output
145
     */
146
    private function pushDb(OutputInterface $output)
147
    {
148
        $this->zohoDatabaseSync->setLogger(new ConsoleLogger($output));
149
150
        $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
151
        foreach ($this->zohoDaos as $zohoDao) {
152
            $this->zohoDatabaseSync->pushToZoho($zohoDao);
153
        }
154
        $output->writeln('Zoho data successfully synchronized.');
155
    }
156
}
157