Completed
Push — 2.0 ( ef9d4b...036124 )
by Raphaël
14s
created

ZohoSyncDatabaseCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 3
Metric Value
c 6
b 1
f 3
dl 0
loc 14
rs 9.4285
cc 1
eloc 12
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
use Wabel\Zoho\CRM\Service\EntitiesGeneratorService;
14
use Wabel\Zoho\CRM\ZohoClient;
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
     * The Zoho Dao and Beans generator
47
     * @var EntitiesGeneratorService
48
     */
49
    private $zohoEntitiesGenerator;
50
51
    /**
52
     *
53
     * @var ZohoClient
54
     */
55
    private $zohoClient;
56
57
    private $pathZohoDaos;
58
59
    private $namespaceZohoDaos;
60
61
62
    /**
63
     * @param ZohoDatabaseModelSync $zohoDatabaseModelSync
64
     * @param ZohoDatabaseCopier    $zohoDatabaseCopier
65
     * @param ZohoDatabasePusher    $zohoDatabaseSync
66
     * @param EntitiesGeneratorService $zohoEntitiesGenerator The Zoho Dao and Beans generator
67
     * @param ZohoClient $zohoClient
68
     * @param string $pathZohoDaos Tht path where we need to generate the Daos.
69
     * @param string $namespaceZohoDaos Daos namespace
70
     * @param Lock                  $lock                  A lock that can be used to avoid running the same command (copy) twice at the same time
71
     */
72
    public function __construct(ZohoDatabaseModelSync $zohoDatabaseModelSync, ZohoDatabaseCopier $zohoDatabaseCopier, ZohoDatabasePusher $zohoDatabaseSync,
73
        EntitiesGeneratorService $zohoEntitiesGenerator, ZohoClient $zohoClient,
74
        $pathZohoDaos, $namespaceZohoDaos, Lock $lock = null)
75
    {
76
        parent::__construct();
77
        $this->zohoDatabaseModelSync = $zohoDatabaseModelSync;
78
        $this->zohoDatabaseCopier = $zohoDatabaseCopier;
79
        $this->zohoDatabaseSync = $zohoDatabaseSync;
80
        $this->zohoEntitiesGenerator =  $zohoEntitiesGenerator;
81
        $this->zohoClient = $zohoClient;
82
        $this->pathZohoDaos = $pathZohoDaos;
83
        $this->namespaceZohoDaos = $namespaceZohoDaos;
84
        $this->lock = $lock;
85
    }
86
87
    protected function configure()
88
    {
89
        $this
90
            ->setName('zoho:sync')
91
            ->setDescription('Synchronize the Zoho CRM data in a local database.')
92
            ->addOption('reset', 'r', InputOption::VALUE_NONE, 'Get a fresh copy of Zoho (rather than doing incremental copy)')
93
            ->addOption('skip-trigger', 's', InputOption::VALUE_NONE, 'Do not create or update the trigger')
94
            ->addOption('fetch-only', 'f', InputOption::VALUE_NONE, 'Fetch only the Zoho data in local database')
95
            ->addOption('push-only', 'p', InputOption::VALUE_NONE, 'Push only the local data to Zoho');
96
    }
97
98
    protected function execute(InputInterface $input, OutputInterface $output)
99
    {
100
        try {
101
            if ($this->lock) {
102
                $this->lock->acquireLock();
103
            }
104
            
105
            
106
            if ($input->getOption('fetch-only') && $input->getOption('push-only')) {
107
                $output->writeln('<error>Options fetch-only and push-only are mutually exclusive.</error>');
108
            }
109
110
            $this->regenerateZohoDao($output);
111
            
112
            $this->syncModel($input, $output);
113
114
            if (!$input->getOption('push-only')) {
115
                $this->fetchDb($input, $output);
116
            }
117
            if (!$input->getOption('fetch-only')) {
118
                $this->pushDb($output);
119
            }
120
            if ($this->lock) {
121
                $this->lock->releaseLock();
122
            }
123
        } catch (LockException $e) {
124
            $output->writeln('<error>Could not start zoho:copy-db copy command. Another zoho:copy-db copy command is already running.</error>');
125
        }
126
    }
127
128
    /**
129
     * Sychronizes the model of the database with Zoho records.
130
     *
131
     * @param OutputInterface $output
132
     */
133
    private function syncModel(InputInterface $input, OutputInterface $output)
134
    {
135
        $this->zohoDatabaseModelSync->setLogger(new ConsoleLogger($output));
136
137
        $twoWaysSync = !$input->getOption('fetch-only');
138
        $skipCreateTrigger = $input->getOption('skip-trigger');
139
140
        $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
141
        foreach ($this->zohoDaos as $zohoDao) {
142
            $this->zohoDatabaseModelSync->synchronizeDbModel($zohoDao, $twoWaysSync, $skipCreateTrigger);
143
        }
144
        $output->writeln('Zoho data successfully synchronized.');
145
    }
146
147
148
    /**
149
     * Regerate Zoho Daos
150
     * @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...
151
     * @param OutputInterface $output
152
     */
153
    private function regenerateZohoDao(OutputInterface $output)
154
    {
155
        $logger = new ConsoleLogger($output);
156
        $zohoModules = $this->zohoEntitiesGenerator->generateAll($this->pathZohoDaos,$this->namespaceZohoDaos);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $zohoModules is correct as $this->zohoEntitiesGener...his->namespaceZohoDaos) (which targets Wabel\Zoho\CRM\Service\E...rService::generateAll()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
        foreach ($zohoModules as $daoFullClassName) {
0 ignored issues
show
Bug introduced by
The expression $zohoModules of type null is not traversable.
Loading history...
158
            $zohoDao = new $daoFullClassName($this->zohoClient);
159
            $this->zohoDaos [] = $zohoDao;
160
            $logger->info(sprintf('<info>%s has created</info>', get_class($zohoDao)));
161
        }
162
        $logger->info("Success to create all the zoho daos.");
163
    }
164
    
165
    
166
    /**
167
     * Run the fetch Db command.
168
     *
169
     * @param InputInterface  $input
170
     * @param OutputInterface $output
171
     */
172
    private function fetchDb(InputInterface $input, OutputInterface $output)
173
    {
174
        if ($input->getOption('reset')) {
175
            $incremental = false;
176
        } else {
177
            $incremental = true;
178
        }
179
180
        $twoWaysSync = !$input->getOption('fetch-only');
181
        $this->zohoDatabaseCopier->setLogger(new ConsoleLogger($output));
182
183
        $output->writeln('Starting copying Zoho data into local database.');
184
        foreach ($this->zohoDaos as $zohoDao) {
185
            $output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao)));
186
            $this->zohoDatabaseCopier->fetchFromZoho($zohoDao, $incremental, $twoWaysSync);
187
        }
188
        $output->writeln('Zoho data successfully copied.');
189
    }
190
191
    /**
192
     * Run the push Db command.
193
     *
194
     * @param OutputInterface $output
195
     */
196
    private function pushDb(OutputInterface $output)
197
    {
198
        $this->zohoDatabaseSync->setLogger(new ConsoleLogger($output));
199
200
        $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
201
        foreach ($this->zohoDaos as $zohoDao) {
202
            $this->zohoDatabaseSync->pushToZoho($zohoDao);
203
        }
204
        $output->writeln('Zoho data successfully synchronized.');
205
    }
206
}
207