Completed
Pull Request — 1.1 (#3)
by Raphaël
02:40
created

ZohoSyncDatabaseCommand::pushDb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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