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