DbAnonymizeCommand::fire()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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