Completed
Pull Request — 1.0 (#1)
by David
09:59
created

ZohoCopyDatabaseCommand::execute()   B

Complexity

Conditions 5
Paths 27

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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