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