GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DataAnonymizerCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Netgen\Bundle\InformationCollectionBundle\Command;
4
5
use Netgen\InformationCollection\Core\Persistence\Anonymizer\AnonymizerServiceFacade;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Command\HelpCommand;
8
use Symfony\Component\Console\Input\InputDefinition;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ConfirmationQuestion;
13
use DateInterval;
14
use DateTime;
15
use Exception;
16
17
class DataAnonymizerCommand extends Command
18
{
19
    protected static $defaultName = 'nginfocollector:anonymize';
20
21
    /**
22
     * @var \Netgen\InformationCollection\Core\Persistence\Anonymizer\AnonymizerServiceFacade
23
     */
24
    protected $anonymizer;
25
26
    /**
27
     * @var \DateInterval
28
     */
29
    protected $period;
30
31
    public function __construct(AnonymizerServiceFacade $anonymizerServiceFacade)
32
    {
33
        $this->anonymizer = $anonymizerServiceFacade;
34
35
        // Parent constructor call is mandatory for commands registered as services
36
        parent::__construct();
37
    }
38
39
    protected function configure()
40
    {
41
        $this->setName("nginfocollector:anonymize");
42
        $this->setDescription("Anonymizes collected data in collected info tables.");
43
        $this->setHelp("This command allows you to anonymize data collected by this library in collected info tables.");
44
45
        $this->setDefinition(
46
            new InputDefinition(
47
                [
48
                    new InputOption('content-id', 'c', InputOption::VALUE_REQUIRED, "Content id."),
49
                    new InputOption('field-identifiers', 'f', InputOption::VALUE_REQUIRED, "Field definition identifiers list."),
50
                    new InputOption('period', 'p', InputOption::VALUE_REQUIRED, "Attributes older that this period will be anonymized."),
51
                    new InputOption('all', 'a', InputOption::VALUE_NONE, "Anonymize all fields."),
52
                    new InputOption('neglect', 'nn', InputOption::VALUE_NONE, "Do not ask for confirmation."),
53
                ]
54
            )
55
        );
56
57
        $this->addUsage("--content-id=123 --field-identifiers=title,name,last_name");
58
        $this->addUsage("--info-collection-id=456 --field-identifiers=title,name,last_name");
59
    }
60
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63 View Code Duplication
        if (is_null($input->getOption('content-id'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            $output->writeln("<error>                                       </error>");
65
            $output->writeln("<error>     Missing content-id parameter.     </error>");
66
            $output->writeln("<error>                                       </error>");
67
68
            return $this->displayHelp($input, $output);
69
        }
70
71 View Code Duplication
        if (is_null($input->getOption('field-identifiers')) && !$input->getOption('all')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            $output->writeln("<error>                                              </error>");
73
            $output->writeln("<error>     Missing field-identifiers parameter.     </error>");
74
            $output->writeln("<error>                                              </error>");
75
76
            return $this->displayHelp($input, $output);
77
        }
78
79
        $contentId = intval($input->getOption('content-id'));
80
        $fields = $this->getFields($input);
81
82
        $info = sprintf("Command will anonymize <info>%s</info> fields for content #%d", empty($fields) ? 'all': implode(", ", $fields), $contentId);
83
        $output->writeln($info);
84
85
        if ($this->proceedWithAction($input, $output)) {
86
            $output->write("<info>Running.... </info>");
87
            $count = $this->anonymizer->anonymize($contentId, $fields, $this->getDateFromPeriod());
0 ignored issues
show
Documentation introduced by
$this->getDateFromPeriod() is of type object<DateTime>, but the function expects a object<DateTimeImmutable>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
            $output->writeln("<info>Done.</info>");
89
            $output->writeln("<info>Anonymized #{$count} collections.</info>");
90
            return 0;
91
        }
92
93
        $output->writeln("<info>Canceled.</info>");
94
    }
95
96
    protected function initialize(InputInterface $input, OutputInterface $output)
97
    {
98
        if (!empty($input->getOption('period'))) {
99
100
            try {
101
                $period = $input->getOption('period');
102
                if (is_string($period)) {
103
                    $this->period = new DateInterval($period);
104
                }
105
            } catch (Exception $exception) {
106
                $output->writeln("Please enter valid DateInterval string.");
107
                exit(0);
108
            }
109
        }
110
    }
111
112
    protected function displayHelp(InputInterface $input, OutputInterface $output)
113
    {
114
        $help = new HelpCommand();
115
        $help->setCommand($this);
116
117
        return $help->run($input, $output);
118
    }
119
120
    protected function getFields(InputInterface $input)
121
    {
122
        if (!empty($input->getOption('all'))) {
123
            return [];
124
        }
125
126
        if (!is_null($input->getOption('field-identifiers'))) {
127
128
            $ids = [];
129
            $fieldIdentifiers = $input->getOption('field-identifiers');
130
131
            if (is_string($fieldIdentifiers)) {
132
                $ids = explode(",", $fieldIdentifiers);
133
            }
134
135
            if (is_array($fieldIdentifiers)) {
136
                $ids = array_filter($fieldIdentifiers);
137
            }
138
139
            return array_unique((array)$ids);
140
        }
141
142
        return [];
143
    }
144
145
    protected function proceedWithAction(InputInterface $input, OutputInterface $output)
146
    {
147
        if ($input->getOption('neglect')) {
148
            return true;
149
        }
150
        $helper = $this->getHelper('question');
151
        $question = new ConfirmationQuestion("Continue with this action? y/n ", false, '/^(y|j)/i');
152
153
        if ($helper->ask($input, $output, $question)) {
154
            return true;
155
        }
156
157
        return false;
158
    }
159
160
    protected function getDateFromPeriod()
161
    {
162
        $dt = new DateTime();
163
        $dt->sub($this->period);
164
165
        return $dt;
166
    }
167
}
168