-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDataAnonymizerCommand.php
More file actions
169 lines (131 loc) · 5.79 KB
/
DataAnonymizerCommand.php
File metadata and controls
169 lines (131 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Netgen\Bundle\InformationCollectionBundle\Command;
use Netgen\InformationCollection\Core\Persistence\Anonymizer\AnonymizerServiceFacade;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use DateTimeInterface;
use DateInterval;
use DateTime;
use Exception;
class DataAnonymizerCommand extends Command
{
protected static $defaultName = 'nginfocollector:anonymize';
protected AnonymizerServiceFacade $anonymizer;
protected DateInterval $period;
public function __construct(AnonymizerServiceFacade $anonymizerServiceFacade)
{
$this->anonymizer = $anonymizerServiceFacade;
// Parent constructor call is mandatory for commands registered as services
parent::__construct();
}
protected function configure(): void
{
$this->setName("nginfocollector:anonymize");
$this->setDescription("Anonymizes collected data in collected info tables.");
$this->setHelp("This command allows you to anonymize data collected by this library in collected info tables.");
$this->setDefinition(
new InputDefinition(
[
new InputOption('content-id', 'c', InputOption::VALUE_REQUIRED, "Content id."),
new InputOption('field-identifiers', 'f', InputOption::VALUE_REQUIRED, "Field definition identifiers list."),
new InputOption('period', 'p', InputOption::VALUE_REQUIRED, "Attributes older that this period will be anonymized."),
new InputOption('all', 'a', InputOption::VALUE_NONE, "Anonymize all fields."),
new InputOption('neglect', 'nn', InputOption::VALUE_NONE, "Do not ask for confirmation."),
]
)
);
$this->addUsage("--content-id=123 --field-identifiers=title,name,last_name");
$this->addUsage("--info-collection-id=456 --field-identifiers=title,name,last_name");
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (is_null($input->getOption('content-id'))) {
$output->writeln("<error> </error>");
$output->writeln("<error> Missing content-id parameter. </error>");
$output->writeln("<error> </error>");
$this->displayHelp($input, $output);
return 1;
}
if (is_null($input->getOption('field-identifiers')) && !$input->getOption('all')) {
$output->writeln("<error> </error>");
$output->writeln("<error> Missing field-identifiers parameter. </error>");
$output->writeln("<error> </error>");
$this->displayHelp($input, $output);
return 1;
}
$contentId = intval($input->getOption('content-id'));
$fields = $this->getFields($input);
$info = sprintf("Command will anonymize <info>%s</info> fields for content #%d", empty($fields) ? 'all': implode(", ", $fields), $contentId);
$output->writeln($info);
if ($this->proceedWithAction($input, $output)) {
$output->write("<info>Running.... </info>");
$count = $this->anonymizer->anonymize($contentId, $fields, $this->getDateFromPeriod());
$output->writeln("<info>Done.</info>");
$output->writeln("<info>Anonymized #{$count} collections.</info>");
return 0;
}
$output->writeln("<info>Canceled.</info>");
return 0;
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
if (!empty($input->getOption('period'))) {
try {
$period = $input->getOption('period');
if (is_string($period)) {
$this->period = new DateInterval($period);
}
} catch (Exception $exception) {
$output->writeln("Please enter valid DateInterval string.");
exit(0);
}
}
}
protected function displayHelp(InputInterface $input, OutputInterface $output): void
{
$help = new HelpCommand();
$help->setCommand($this);
$help->run($input, $output);
}
protected function getFields(InputInterface $input): array
{
if (!empty($input->getOption('all'))) {
return [];
}
if (!is_null($input->getOption('field-identifiers'))) {
$ids = [];
$fieldIdentifiers = $input->getOption('field-identifiers');
if (is_string($fieldIdentifiers)) {
$ids = explode(",", $fieldIdentifiers);
}
if (is_array($fieldIdentifiers)) {
$ids = array_filter($fieldIdentifiers);
}
return array_unique((array)$ids);
}
return [];
}
protected function proceedWithAction(InputInterface $input, OutputInterface $output): bool
{
if ($input->getOption('neglect')) {
return true;
}
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion("Continue with this action? y/n ", false, '/^(y|j)/i');
if ($helper->ask($input, $output, $question)) {
return true;
}
return false;
}
protected function getDateFromPeriod(): DateTimeInterface
{
$dt = new DateTime();
$dt->sub($this->period);
return $dt;
}
}