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

ZohoCopyDatabaseCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 1
Metric Value
c 9
b 1
f 1
dl 0
loc 14
rs 9.4285
cc 3
eloc 11
nc 3
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 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 InputInterface $input
0 ignored issues
show
Bug introduced by
There is no parameter named $input. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
130
     * @param OutputInterface $output
131
     */
132
    private function syncDb(OutputInterface $output){
133
        try {
134
            if ($this->lockSync) {
135
                $this->lockSync->acquireLock();
136
            }
137
            $this->zohoDatabaseSync->setLogger(new ConsoleLogger($output));
138
139
            $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
140
            foreach ($this->zohoDaos as $zohoDao) {
141
                $output->writeln(sprintf(' > Insert new rows using <info>%s</info>', get_class($zohoDao)));
142
                $this->zohoDatabaseSync->pushInsertedRows($zohoDao);
143
                $output->writeln(sprintf(' > Update rows using <info>%s</info>', get_class($zohoDao)));
144
                $this->zohoDatabaseSync->pushUpdatedRows($zohoDao);
145
                $output->writeln(sprintf(' > Delete rows using <info>%s</info>', get_class($zohoDao)));
146
                $this->zohoDatabaseSync->pushDeletedRows($zohoDao);
147
            }
148
            $output->writeln('Zoho data successfully synchronized.');
149
            if ($this->lockSync) {
150
                $this->lockSync->releaseLock();
151
            }
152
        } catch (LockException $e) {
153
            $output->writeln('<error>Could not start zoho:sync-db sync command. Another zoho:sync-db sync command is already running.</error>');
154
        }
155
    }
156
}
157