Completed
Push — master ( 53696a...6c6a1b )
by Maarten
13s
created

Synchronise::getSkipTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace mtolhuijs\LDS\Commands;
4
5
use Illuminate\Console\Command;
6
use mtolhuijs\LDS\DatabaseSynchronizer;
7
use mtolhuijs\LDS\Exceptions\DatabaseConnectionException;
8
9
class Synchronise extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = '
17
        db:sync
18
        { --from= : Synchronize data from this database instead of the one specified in config }
19
        { --to= : Synchronize data to this database instead of the one specified in config }
20
        { --t|tables=* : Only run for given table(s) }
21
        { --st|skip-tables=* : Skip given table(s) }
22
        { --l|limit= : Limit query rows (defaults to 5000) }
23
        { --truncate : Truncate before inserting data }
24
    ';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Synchronizes your \'from\' database with you\'re \'to\' database';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return mixed
37
     */
38
    public function handle()
39
    {
40
        try {
41
            (new DatabaseSynchronizer(
42
                $this->option('from') ?? config('database-synchronizer.from'),
43
                $this->option('to') ?? config('database-synchronizer.to'),
44
                $this
45
            ))
46
                ->setTables($this->getTables())
47
                ->setSkipTables($this->getSkipTables())
48
                ->setLimit((int) $this->getLimit())
49
                ->setOptions($this->options())
50
                ->run();
51
        } catch (DatabaseConnectionException $e) {
52
            $this->error($e->getMessage());
53
54
            return;
55
        }
56
57
        $this->info(PHP_EOL.'Synchronization done!');
58
    }
59
60
    private function getTables()
61
    {
62
        return empty($this->option('tables')) ?
63
            config('database-synchronizer.tables') : $this->option('tables');
64
    }
65
66
    private function getSkipTables()
67
    {
68
        return empty($this->option('skip-tables')) ?
69
            config('database-synchronizer.skip_tables') : $this->option('skip-tables');
70
    }
71
72
    private function getLimit()
73
    {
74
        return $this->option('limit') ?? config('database-synchronizer.limit', DatabaseSynchronizer::DEFAULT_LIMIT);
75
    }
76
}
77