Conditions | 22 |
Paths | 1159 |
Total Lines | 146 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
61 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
62 | { |
||
63 | if (!$this->lock()) { |
||
64 | $output->writeln('The command is already running in another process.'); |
||
65 | |||
66 | return Command::SUCCESS; |
||
67 | } |
||
68 | |||
69 | $io = new SymfonyStyle($input, $output); |
||
70 | |||
71 | $keep = $input->getArgument('keep'); |
||
72 | $keep = (\is_array($keep) ? $keep[0] : $keep); |
||
73 | |||
74 | /** @var string $date */ |
||
75 | $date = $input->getOption('date'); |
||
76 | $until = null; |
||
77 | |||
78 | if ($date) { |
||
79 | // Use custom date if provided |
||
80 | try { |
||
81 | $until = new DateTimeImmutable($date); |
||
82 | } catch (Exception $e) { |
||
83 | $io->error(sprintf('Invalid date format provided: %s', $date)); |
||
84 | } |
||
85 | } else { |
||
86 | // Fall back to default retention period |
||
87 | $until = $this->validateKeepArgument($keep, $io); |
||
88 | } |
||
89 | |||
90 | if (null === $until) { |
||
91 | return Command::SUCCESS; |
||
92 | } |
||
93 | |||
94 | /** @var DoctrineProvider $provider */ |
||
95 | $provider = $this->auditor->getProvider(DoctrineProvider::class); |
||
96 | $schemaManager = new SchemaManager($provider); |
||
97 | |||
98 | /** @var StorageService[] $storageServices */ |
||
99 | $storageServices = $provider->getStorageServices(); |
||
100 | |||
101 | // auditable classes by storage entity manager |
||
102 | $count = 0; |
||
103 | |||
104 | // Collect auditable classes from auditing storage managers |
||
105 | $rawExcludeValues = $input->getOption('exclude') ?? []; |
||
106 | $rawIncludeValues = $input->getOption('include') ?? []; |
||
107 | $excludeEntities = \is_array($rawExcludeValues) ? $rawExcludeValues : [$rawExcludeValues]; |
||
108 | $includeEntities = \is_array($rawIncludeValues) ? $rawIncludeValues : [$rawIncludeValues]; |
||
109 | $repository = $schemaManager->collectAuditableEntities(); |
||
110 | $filteredRepository = []; |
||
111 | |||
112 | foreach ($repository as $name => $entityClasses) { |
||
113 | foreach ($entityClasses as $entityClass => $table) { |
||
114 | if ( |
||
115 | !\in_array($entityClass, $excludeEntities, true) |
||
116 | && ( |
||
117 | empty($includeEntities) |
||
118 | || \in_array($entityClass, $includeEntities, true) |
||
119 | ) |
||
120 | ) { |
||
121 | $filteredRepository[$name][$entityClass] = $table; |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | $repository = $filteredRepository; |
||
127 | |||
128 | foreach ($repository as $name => $entities) { |
||
129 | $count += \count($entities); |
||
130 | } |
||
131 | |||
132 | $message = sprintf( |
||
133 | "You are about to clean audits created before <comment>%s</comment>: %d classes involved.\n Do you want to proceed?", |
||
134 | $until->format(self::UNTIL_DATE_FORMAT), |
||
135 | $count |
||
136 | ); |
||
137 | |||
138 | $confirm = $input->getOption('no-confirm') ? true : $io->confirm($message, false); |
||
139 | $dryRun = (bool) $input->getOption('dry-run'); |
||
140 | $dumpSQL = (bool) $input->getOption('dump-sql'); |
||
141 | |||
142 | if ($confirm) { |
||
143 | /** @var Configuration $configuration */ |
||
144 | $configuration = $provider->getConfiguration(); |
||
145 | |||
146 | $progressBar = new ProgressBar($output, $count); |
||
147 | $progressBar->setBarWidth(70); |
||
148 | $progressBar->setFormat("%message%\n".$progressBar->getFormatDefinition('debug')); |
||
149 | |||
150 | $progressBar->setMessage('Starting...'); |
||
151 | $progressBar->start(); |
||
152 | |||
153 | $queries = []; |
||
154 | |||
155 | /** |
||
156 | * @var string $name |
||
157 | * @var array<string, string> $classes |
||
158 | */ |
||
159 | foreach ($repository as $name => $classes) { |
||
160 | foreach ($classes as $entity => $tablename) { |
||
161 | $connection = $storageServices[$name]->getEntityManager()->getConnection(); |
||
162 | $auditTable = $schemaManager->resolveAuditTableName($entity, $configuration, $connection->getDatabasePlatform()); |
||
163 | |||
164 | $queryBuilder = $connection->createQueryBuilder(); |
||
165 | $queryBuilder |
||
166 | ->delete($auditTable) |
||
167 | ->where('created_at < :until') |
||
168 | ->setParameter('until', $until->format(self::UNTIL_DATE_FORMAT)) |
||
169 | ; |
||
170 | |||
171 | if ($dumpSQL) { |
||
172 | $queries[] = str_replace(':until', "'".$until->format(self::UNTIL_DATE_FORMAT)."'", $queryBuilder->getSQL()); |
||
173 | } |
||
174 | |||
175 | if (!$dryRun) { |
||
176 | DoctrineHelper::executeStatement($queryBuilder); |
||
177 | } |
||
178 | |||
179 | $progressBar->setMessage(sprintf('Cleaning audit tables... (<info>%s</info>)', $auditTable)); |
||
180 | $progressBar->advance(); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | $progressBar->setMessage('Cleaning audit tables... (<info>done</info>)'); |
||
185 | $progressBar->display(); |
||
186 | |||
187 | $io->newLine(); |
||
188 | if ($dumpSQL) { |
||
189 | $io->newLine(); |
||
190 | $io->writeln('SQL queries to be run:'); |
||
191 | foreach ($queries as $query) { |
||
192 | $io->writeln($query); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | $io->newLine(); |
||
197 | $io->success('Success.'); |
||
198 | } else { |
||
199 | $io->success('Cancelled.'); |
||
200 | } |
||
201 | |||
202 | // if not released explicitly, Symfony releases the lock |
||
203 | // automatically when the execution of the command ends |
||
204 | $this->release(); |
||
205 | |||
206 | return Command::SUCCESS; |
||
207 | } |
||
223 |