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

ZohoCopyDatabaseCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 11
Bugs 2 Features 1
Metric Value
wmc 17
c 11
b 2
f 1
lcom 1
cbo 8
dl 0
loc 140
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A configure() 0 9 1
A execute() 0 14 3
C copyDb() 0 33 7
B syncDb() 0 24 5
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 ZohoCopyDatabaseCommand extends Command
17
{
18
    /**
19
     * The list of Zoho DAOs to copy.
20
     *
21
     * @var AbstractZohoDao[]
22
     */
23
    private $zohoDaos;
24
25
    /**
26
     * @var ZohoDatabaseCopier
27
     */
28
    private $zohoDatabaseCopier;
29
30
    /**
31
     * @var ZohoDatabaseSyncZoho
32
     */
33
    private $zohoDatabaseSync;
34
35
    /**
36
     * @var Lock
37
     */
38
    private $lockCopy;
39
    
40
    /**
41
     * @var Lock
42
     */
43
    private $lockSync;
44
45
    /**
46
     * 
47
     * @param \Wabel\Zoho\CRM\Copy\ZohoDatabaseCopier $zohoDatabaseCopier
48
     * @param \Wabel\Zoho\CRM\Copy\ZohoDatabaseSyncZoho $zohoDatabaseSync
49
     * @param array $zohoDaos The list of Zoho DAOs to copy
50
     * @param Lock $lockCopy A lock that can be used to avoid running the same command (copy) twice at the same time
51
     * @param Lock $lockSync A lock that can be used to avoid running the same command (sync) twice at the same time
52
     */
53
    public function __construct(ZohoDatabaseCopier $zohoDatabaseCopier, ZohoDatabaseSyncZoho $zohoDatabaseSync, array $zohoDaos, Lock $lockCopy = null, Lock $lockSync = null)
54
    {
55
        parent::__construct();
56
        $this->zohoDatabaseCopier = $zohoDatabaseCopier;
57
        $this->zohoDatabaseSync = $zohoDatabaseSync;
58
        $this->zohoDaos = $zohoDaos;
59
        $this->lockCopy = $lockCopy;
60
        $this->lockSync = $lockSync;
61
    }
62
63
    protected function configure()
64
    {
65
        $this
66
            ->setName('zoho:copy-db')
67
            ->setDescription('Copies the Zoho database in local DB tables and synchronize Zoho CRM from the Zoho database.')
68
            ->addArgument("action",  InputArgument::REQUIRED, "Specify 'copy' or 'sync'")
69
            ->addOption("reset", "r", InputOption::VALUE_NONE, 'Get a fresh copy of Zoho (rather than doing incremental copy)')
70
            ->addOption("trigger", "t", InputOption::VALUE_NONE, 'Create or update the triggers');
71
    }
72
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $action = $input->getArgument('action');
76
        switch ($action) {
77
            case 'copy':
78
                $this->copyDb($input, $output);
79
                break;
80
            case 'sync':
81
                $this->syncDb($output);
82
                break;
83
            default:
84
                throw new InvalidArgumentException('Named argument not found.');
85
        }
86
    }
87
88
    /**
89
     * Run the copy Db command.
90
     * @param InputInterface $input
91
     * @param OutputInterface $output
92
     */
93
    private function copyDb(InputInterface $input, OutputInterface $output){
94
        try {
95
            if ($this->lockCopy) {
96
                $this->lockCopy->acquireLock();
97
            }
98
99
            if ($input->getOption('reset')) {
100
                $incremental = false;
101
            } else {
102
                $incremental = true;
103
            }
104
105
            $forceCreateTrigger = false;
106
107
            if($input->getOption('trigger')){
108
                $forceCreateTrigger = true;
109
            }
110
            $twoWaysSync = true;
111
            $this->zohoDatabaseCopier->setLogger(new ConsoleLogger($output));
112
113
            $output->writeln('Starting copying Zoho data into local database.');
114
            foreach ($this->zohoDaos as $zohoDao) {
115
                $output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao)));
116
                $this->zohoDatabaseCopier->copy($zohoDao, $incremental, $twoWaysSync, $forceCreateTrigger);
117
            }
118
            $output->writeln('Zoho data successfully copied.');
119
            if ($this->lockCopy) {
120
                $this->lockCopy->releaseLock();
121
            }
122
        } catch (LockException $e) {
123
            $output->writeln('<error>Could not start zoho:copy-db copy command. Another zoho:copy-db copy command is already running.</error>');
124
        }
125
    }
126
127
    /**
128
     * Run he sync Db command.
129
     * @param OutputInterface $output
130
     */
131
    private function syncDb(OutputInterface $output){
132
        try {
133
            if ($this->lockSync) {
134
                $this->lockSync->acquireLock();
135
            }
136
            $this->zohoDatabaseSync->setLogger(new ConsoleLogger($output));
137
138
            $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
139
            foreach ($this->zohoDaos as $zohoDao) {
140
                $output->writeln(sprintf(' > Insert new rows using <info>%s</info>', get_class($zohoDao)));
141
                $this->zohoDatabaseSync->pushInsertedRows($zohoDao);
142
                $output->writeln(sprintf(' > Update rows using <info>%s</info>', get_class($zohoDao)));
143
                $this->zohoDatabaseSync->pushUpdatedRows($zohoDao);
144
                $output->writeln(sprintf(' > Delete rows using <info>%s</info>', get_class($zohoDao)));
145
                $this->zohoDatabaseSync->pushDeletedRows($zohoDao);
146
            }
147
            $output->writeln('Zoho data successfully synchronized.');
148
            if ($this->lockSync) {
149
                $this->lockSync->releaseLock();
150
            }
151
        } catch (LockException $e) {
152
            $output->writeln('<error>Could not start zoho:sync-db sync command. Another zoho:sync-db sync command is already running.</error>');
153
        }
154
    }
155
}
156