Completed
Push — master ( f7b7ba...c2f7a5 )
by Maarten
02:52
created

Synchronise::getTables()   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|table=* : Only run for given table(s) (Only used if optional --tables is given) }
21
        { --l|limit= : Limit query rows (defaults to 5000) }
22
        { --truncate : Truncate before inserting data }
23
        { --tables : Use tables specified through config or options }
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
            $synchronizer = new DatabaseSynchronizer(
42
                $this->option('from') ?? config('database-synchronizer.from'),
43
                $this->option('to') ?? config('database-synchronizer.to'),
44
                $this
45
            );
46
        } catch (DatabaseConnectionException $e) {
47
            $this->error($e->getMessage());
48
49
            return;
50
        }
51
52
        if ($this->option('tables')) {
53
            $synchronizer->tables = $this->getTables();
54
        }
55
56
        if ($this->option('limit')) {
57
            $synchronizer->limit = (int) $this->option('limit');
58
        }
59
60
        $synchronizer->truncate = $this->option('truncate');
61
62
        $synchronizer->run();
63
64
        $this->info(PHP_EOL.'Synchronization done!');
65
    }
66
67
    private function getTables()
68
    {
69
        return empty($this->option('table')) ?
70
            config('database-synchronizer.tables') : $this->option('table');
71
    }
72
}
73