Completed
Push — master ( 3631c8...ad7b80 )
by Nekrasov
05:10 queued 03:51
created

DbAnonymizeCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\LaravelDataAnonymization\Commands;
4
5
use Arrilot\DataAnonymization\Anonymizer as CoreAnonymizer;
6
use Arrilot\LaravelDataAnonymization\AbstractAnonymizer;
7
use Arrilot\DataAnonymization\Database\SqlDatabase;
8
use Illuminate\Console\Command;
9
use Illuminate\Console\ConfirmableTrait;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class DbAnonymizeCommand extends Command
13
{
14
    use ConfirmableTrait;
15
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'db:anonymize';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Anonymize the database';
29
30
    /**
31
     * Get the console command options.
32
     *
33
     * @return array
34
     */
35
    protected function getOptions()
36
    {
37
        return [
38
            ['class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root anonymizer', 'DatabaseAnonymizer'],
39
40
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to anonymize'],
41
42
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
43
        ];
44
    }
45
46
    public function handle()
47
    {
48
    	return $this->fire();
49
    }
50
51
    /**
52
     * Execute the console command.
53
     *
54
     * @return void
55
     */
56
    public function fire()
57
    {
58
        if (! $this->confirmToProceed()) {
59
            return;
60
        }
61
62
        $coreAnonymizer = $this->getCoreAnonymizer();
63
64
        // collect configuration into $coreAnonymizer
65
        $this->getAnonymizer($coreAnonymizer)->run();
66
67
        // change database
68
        $coreAnonymizer->run();
69
    }
70
71
    /**
72
     * Get an anonymizer instance from the container.
73
     *
74
     * @param CoreAnonymizer $coreAnonymizer
75
     *
76
     * @return AbstractAnonymizer
77
     */
78
    protected function getAnonymizer($coreAnonymizer)
79
    {
80
        $className = $this->input->getOption('class');
81
82
        return (new $className($coreAnonymizer))->setCommand($this);
83
    }
84
85
    /**
86
     * Get core anonymizer from parent package.
87
     *
88
     * @return CoreAnonymizer
89
     */
90
    protected function getCoreAnonymizer()
91
    {
92
        $db = $this->getDatabaseConfiguration($this->input->getOption('database'));
93
94
        $databaseInteractor = new SqlDatabase($db['dsn'], $db['username'], $db['password']);
95
96
        return new CoreAnonymizer($databaseInteractor);
97
    }
98
99
    /**
100
     * Get database configuration from laravel config
101
     *
102
     * @param string $selected
103
     *
104
     * @return array
105
     */
106
    protected function getDatabaseConfiguration($selected)
107
    {
108
        $database = $selected ?: $this->laravel['config']['database.default'];
109
110
        $connection = $this->laravel['config']['database.connections.'.$database];
111
112
        return [
113
            'dsn' => "{$connection['driver']}:dbname={$connection['database']};host={$connection['host']};charset={$connection['charset']}",
114
            'username' => $connection['username'],
115
            'password' => $connection['password'],
116
        ];
117
    }
118
}
119