Completed
Pull Request — 2.0 (#7)
by Raphaël
11:08
created

ZohoSyncDatabaseCommand::getListFieldName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 3
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 Wabel\Zoho\CRM\AbstractZohoDao;
13
use Wabel\Zoho\CRM\Service\EntitiesGeneratorService;
14
use Wabel\Zoho\CRM\ZohoClient;
15
use Logger\Formatters\DateTimeFormatter;
16
use Mouf\Utils\Log\Psr\MultiLogger;
17
18
class ZohoSyncDatabaseCommand extends Command
19
{
20
    /**
21
     * The list of Zoho DAOs to copy.
22
     *
23
     * @var AbstractZohoDao[]
24
     */
25
    private $zohoDaos;
26
27
    /**
28
     * @var ZohoDatabaseModelSync
29
     */
30
    private $zohoDatabaseModelSync;
31
32
    /**
33
     * @var ZohoDatabaseCopier
34
     */
35
    private $zohoDatabaseCopier;
36
37
    /**
38
     * @var ZohoDatabasePusher
39
     */
40
    private $zohoDatabaseSync;
41
42
    /**
43
     *
44
     * @var MultiLogger
45
     */
46
    private $logger;
47
48
    /**
49
     * @var Lock
50
     */
51
    private $lock;
52
53
    /**
54
     * The Zoho Dao and Beans generator
55
     * @var EntitiesGeneratorService
56
     */
57
    private $zohoEntitiesGenerator;
58
59
    /**
60
     *
61
     * @var ZohoClient
62
     */
63
    private $zohoClient;
64
65
    private $pathZohoDaos;
66
67
    private $namespaceZohoDaos;
68
69
70
    /**
71
     * @param ZohoDatabaseModelSync $zohoDatabaseModelSync
72
     * @param ZohoDatabaseCopier    $zohoDatabaseCopier
73
     * @param ZohoDatabasePusher    $zohoDatabaseSync
74
     * @param EntitiesGeneratorService $zohoEntitiesGenerator The Zoho Dao and Beans generator
75
     * @param ZohoClient $zohoClient
76
     * @param string $pathZohoDaos Tht path where we need to generate the Daos.
77
     * @param string $namespaceZohoDaos Daos namespace
78
     * @param MultiLogger $logger
79
     * @param Lock                  $lock                  A lock that can be used to avoid running the same command (copy) twice at the same time
80
     */
81
    public function __construct(ZohoDatabaseModelSync $zohoDatabaseModelSync, ZohoDatabaseCopier $zohoDatabaseCopier, ZohoDatabasePusher $zohoDatabaseSync,
82
        EntitiesGeneratorService $zohoEntitiesGenerator, ZohoClient $zohoClient,
83
        $pathZohoDaos, $namespaceZohoDaos, MultiLogger $logger, Lock $lock = null)
84
    {
85
        parent::__construct();
86
        $this->zohoDatabaseModelSync = $zohoDatabaseModelSync;
87
        $this->zohoDatabaseCopier = $zohoDatabaseCopier;
88
        $this->zohoDatabaseSync = $zohoDatabaseSync;
89
        $this->zohoEntitiesGenerator =  $zohoEntitiesGenerator;
90
        $this->zohoClient = $zohoClient;
91
        $this->pathZohoDaos = $pathZohoDaos;
92
        $this->namespaceZohoDaos = $namespaceZohoDaos;
93
        $this->logger = $logger;
94
        $this->lock = $lock;
95
    }
96
97
    protected function configure()
98
    {
99
        $this
100
            ->setName('zoho:sync')
101
            ->setDescription('Synchronize the Zoho CRM data in a local database.')
102
            ->addOption('reset', 'r', InputOption::VALUE_NONE, 'Get a fresh copy of Zoho (rather than doing incremental copy)')
103
            ->addOption('skip-trigger', 's', InputOption::VALUE_NONE, 'Do not create or update the trigger')
104
            ->addOption('fetch-only', 'f', InputOption::VALUE_NONE, 'Fetch only the Zoho data in local database')
105
            ->addOption('push-only', 'p', InputOption::VALUE_NONE, 'Push only the local data to Zoho');
106
    }
107
108
    protected function execute(InputInterface $input, OutputInterface $output)
109
    {
110
        try {
111
            if ($this->lock) {
112
                $this->lock->acquireLock();
113
            }
114
            
115
            $this->logger->addLogger(new DateTimeFormatter(new ConsoleLogger($output)));
116
            if ($input->getOption('fetch-only') && $input->getOption('push-only')) {
117
                $output->writeln('<error>Options fetch-only and push-only are mutually exclusive.</error>');
118
            }
119
120
            $this->regenerateZohoDao($output);
121
            
122
            $this->syncModel($input, $output);
123
124
            if (!$input->getOption('push-only')) {
125
                $this->fetchDb($input, $output);
126
            }
127
            if (!$input->getOption('fetch-only')) {
128
                $this->pushDb($output);
129
            }
130
            if ($this->lock) {
131
                $this->lock->releaseLock();
132
            }
133
        } catch (LockException $e) {
134
            $output->writeln('<error>Could not start zoho:copy-db copy command. Another zoho:copy-db copy command is already running.</error>');
135
        }
136
    }
137
138
    /**
139
     * Sychronizes the model of the database with Zoho records.
140
     *
141
     * @param OutputInterface $output
142
     */
143
    private function syncModel(InputInterface $input, OutputInterface $output)
144
    {
145
        $twoWaysSync = !$input->getOption('fetch-only');
146
        $skipCreateTrigger = $input->getOption('skip-trigger');
147
148
        $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
149
        foreach ($this->zohoDaos as $zohoDao) {
150
            $this->zohoDatabaseModelSync->synchronizeDbModel($zohoDao, $twoWaysSync, $skipCreateTrigger);
151
        }
152
        $output->writeln('Zoho data successfully synchronized.');
153
    }
154
155
    /**
156
     * @param AbstractZohoDao $zohoDao
157
     *
158
     * @return array
159
     */
160
    private function getListFieldName(AbstractZohoDao $zohoDao)
161
    {
162
        $fieldNames= array();
163
        foreach ($zohoDao->getFields() as $fieldsDescriptor) {
0 ignored issues
show
Bug introduced by
The method getFields() cannot be called from this context as it is declared protected in class Wabel\Zoho\CRM\AbstractZohoDao.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
164
            foreach (array_values($fieldsDescriptor) as $fieldDescriptor) {
165
                $fieldNames[] = $fieldDescriptor['name'];
166
            }
167
        }
168
169
        return $fieldNames;
170
    }
171
    
172
    /**
173
     * Regerate Zoho Daos
174
     * @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...
175
     * @param OutputInterface $output
176
     */
177
    private function regenerateZohoDao(OutputInterface $output)
178
    {
179
        $output->writeln("Start to generate all the zoho daos.");
180
        $zohoModules = $this->zohoEntitiesGenerator->generateAll($this->pathZohoDaos,$this->namespaceZohoDaos);
181
        foreach ($zohoModules as $daoFullClassName) {
182
            /* @var $zohoDao AbstractZohoDao */
183
            $zohoDao = new $daoFullClassName($this->zohoClient);
184
            if(!in_array('lastActivityTime', $this->getListFieldName($zohoDao))){
185
                continue;
186
            }
187
            $this->zohoDaos [] = $zohoDao;
188
            $output->writeln(sprintf('<info>%s has created</info>', get_class($zohoDao)));
189
        }
190
        $output->writeln("Success to create all the zoho daos.");
191
    }
192
    
193
    
194
    /**
195
     * Run the fetch Db command.
196
     *
197
     * @param InputInterface  $input
198
     * @param OutputInterface $output
199
     */
200
    private function fetchDb(InputInterface $input, OutputInterface $output)
201
    {
202
        if ($input->getOption('reset')) {
203
            $incremental = false;
204
        } else {
205
            $incremental = true;
206
        }
207
208
        $twoWaysSync = !$input->getOption('fetch-only');
209
210
        $output->writeln('Starting copying Zoho data into local database.');
211
        foreach ($this->zohoDaos as $zohoDao) {
212
            $output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao)));
213
            $this->zohoDatabaseCopier->fetchFromZoho($zohoDao, $incremental, $twoWaysSync);
214
        }
215
        $output->writeln('Zoho data successfully copied.');
216
    }
217
218
    /**
219
     * Run the push Db command.
220
     *
221
     * @param OutputInterface $output
222
     */
223
    private function pushDb(OutputInterface $output)
224
    {
225
226
        $output->writeln('Starting synchronize Zoho data into Zoho CRM.');
227
        foreach ($this->zohoDaos as $zohoDao) {
228
            $this->zohoDatabaseSync->pushToZoho($zohoDao);
229
        }
230
        $output->writeln('Zoho data successfully synchronized.');
231
    }
232
}
233