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

ZohoCopyDatabaseCommand::syncDb()   B

Complexity

Conditions 5
Paths 37

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.5125
cc 5
eloc 18
nc 37
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
     * @param ZohoDatabaseCopier                $zohoDatabaseCopier
47
     * @param \Wabel\Zoho\CRM\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 twice at the same time
0 ignored issues
show
Bug introduced by
There is no parameter named $lock. 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...
49
     */
50
    public function __construct(ZohoDatabaseCopier $zohoDatabaseCopier, ZohoDatabaseSyncZoho $zohoDatabaseSync, array $zohoDaos, Lock $lockCopy = null, Lock $lockSync = null)
51
    {
52
        parent::__construct();
53
        $this->zohoDatabaseCopier = $zohoDatabaseCopier;
54
        $this->zohoDatabaseSync = $zohoDatabaseSync;
55
        $this->zohoDaos = $zohoDaos;
56
        $this->lockCopy = $lockCopy;
57
        $this->lockSync = $lockSync;
58
    }
59
60
    protected function configure()
61
    {
62
        $this
63
            ->setName('zoho:copy-db')
64
            ->setDescription('Copies the Zoho database in local DB tables and synchronize Zoho CRM from the Zoho database.')
65
            ->addArgument("action",  InputArgument::REQUIRED, "Specify 'copy' or 'sync'")
66
            ->addOption("reset", "r", InputOption::VALUE_NONE, 'Get a fresh copy of Zoho (rather than doing incremental copy)')
67
            ->addOption("trigger", "t", InputOption::VALUE_NONE, 'Create or update the triggers');
68
    }
69
70
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72
        $action = $input->getArgument('action');
73
        switch ($action) {
74
            case 'copy':
75
                $this->copyDb($input, $output);
76
                break;
77
            case 'sync':
78
                $this->syncDb($input, $output);
79
                break;
80
            default:
81
                throw new InvalidArgumentException('Named argument not found.');
82
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
83
        }
84
    }
85
86
    /**
87
     * Run the copy Db command.
88
     * @param InputInterface $input
89
     * @param OutputInterface $output
90
     */
91
    private function copyDb(InputInterface $input, OutputInterface $output){
92
        try {
93
            if ($this->lockCopy) {
94
                $this->lockCopy->acquireLock();
95
            }
96
97
            if ($input->getOption('reset')) {
98
                $incremental = false;
99
            } else {
100
                $incremental = true;
101
            }
102
103
            $forceCreateTrigger = false;
104
105
            if($input->getOption('trigger')){
106
                $forceCreateTrigger = true;
107
            }
108
            $twoWaysSync = true;
109
            $this->zohoDatabaseCopier->setLogger(new ConsoleLogger($output));
110
111
            $output->writeln('Starting copying Zoho data into local database.');
112
            foreach ($this->zohoDaos as $zohoDao) {
113
                $output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao)));
114
                $this->zohoDatabaseCopier->copy($zohoDao, $incremental, $twoWaysSync, $forceCreateTrigger);
115
            }
116
            $output->writeln('Zoho data successfully copied.');
117
            if ($this->lockCopy) {
118
                $this->lockCopy->releaseLock();
119
            }
120
        } catch (LockException $e) {
121
            $output->writeln('<error>Could not start zoho:copy-db copy command. Another zoho:copy-db copy command is already running.</error>');
122
        }
123
    }
124
125
    /**
126
     * Run he sync Db command.
127
     * @param InputInterface $input
128
     * @param OutputInterface $output
129
     */
130
    private function syncDb(InputInterface $input, OutputInterface $output){
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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