Completed
Push — 1.0 ( db36e4...c33c9d )
by David
8s
created

ZohoCopyDatabaseCommand::execute()   B

Complexity

Conditions 5
Paths 27

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 13
nc 27
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\Output\OutputInterface;
10
use Wabel\Zoho\CRM\AbstractZohoDao;
11
12
class ZohoCopyDatabaseCommand extends Command
13
{
14
    /**
15
     * The list of Zoho DAOs to copy.
16
     *
17
     * @var AbstractZohoDao[]
18
     */
19
    private $zohoDaos;
20
21
    /**
22
     * @var ZohoDatabaseCopier
23
     */
24
    private $zohoDatabaseCopier;
25
26
    /**
27
     * @var Lock
28
     */
29
    private $lock;
30
31
    /**
32
     * @param ZohoDatabaseCopier                $zohoDatabaseCopier
33
     * @param \Wabel\Zoho\CRM\AbstractZohoDao[] $zohoDaos           The list of Zoho DAOs to copy
34
     * @param Lock                              $lock               A lock that can be used to avoid running the same command twice at the same time
35
     */
36
    public function __construct(ZohoDatabaseCopier $zohoDatabaseCopier, array $zohoDaos, Lock $lock = null)
37
    {
38
        parent::__construct();
39
        $this->zohoDatabaseCopier = $zohoDatabaseCopier;
40
        $this->zohoDaos = $zohoDaos;
41
        $this->lock = $lock;
42
    }
43
44
    protected function configure()
45
    {
46
        $this
47
            ->setName('zoho:copy-db')
48
            ->setDescription('Copies the Zoho database in local DB tables');
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        try {
54
            if ($this->lock) {
55
                $this->lock->acquireLock();
56
            }
57
            $output->writeln('Starting copying Zoho data into local database.');
58
            foreach ($this->zohoDaos as $zohoDao) {
59
                $output->writeln(sprintf('Copying data using <info>%s</info>', get_class($zohoDao)));
60
                $this->zohoDatabaseCopier->copy($zohoDao);
61
            }
62
            $output->writeln('Zoho data successfully copied.');
63
            if ($this->lock) {
64
                $this->lock->releaseLock();
65
            }
66
        } catch (LockException $e) {
67
            $output->writeln('Could not start zoho:copy-db command. Another zoho:copy-db command is already running.');
68
        }
69
    }
70
}
71