| Conditions | 12 |
| Paths | 15 |
| Total Lines | 105 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 59 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 60 | { |
||
| 61 | if (!$this->lock()) { |
||
| 62 | $output->writeln('The command is already running in another process.'); |
||
| 63 | |||
| 64 | return 0; |
||
| 65 | } |
||
| 66 | |||
| 67 | $io = new SymfonyStyle($input, $output); |
||
| 68 | |||
| 69 | $keep = $input->getArgument('keep'); |
||
| 70 | $keep = (\is_array($keep) ? $keep[0] : $keep); |
||
| 71 | $until = $this->validateKeepArgument($keep, $io); |
||
|
1 ignored issue
–
show
|
|||
| 72 | |||
| 73 | if (null === $until) { |
||
| 74 | return 0; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** @var DoctrineProvider $provider */ |
||
| 78 | $provider = $this->auditor->getProvider(DoctrineProvider::class); |
||
| 79 | $schemaManager = new SchemaManager($provider); |
||
| 80 | |||
| 81 | // $entities = $this->provider->getConfiguration()->getEntities(); |
||
| 82 | /** @var StorageService[] $storageServices */ |
||
| 83 | $storageServices = $provider->getStorageServices(); |
||
| 84 | |||
| 85 | // auditable entities by storage entity manager |
||
| 86 | $repository = []; |
||
| 87 | $count = 0; |
||
| 88 | |||
| 89 | // Collect auditable entities from auditing storage managers |
||
| 90 | [$repository, $count] = $this->collectAuditableEntities($provider, $schemaManager, $repository, $count); |
||
| 91 | |||
| 92 | $message = sprintf( |
||
| 93 | "You are about to clean audits created before <comment>%s</comment>: %d entities involved.\n Do you want to proceed?", |
||
| 94 | $until->format('Y-m-d'), |
||
| 95 | $count |
||
| 96 | ); |
||
| 97 | |||
| 98 | $confirm = $input->getOption('no-confirm') ? true : $io->confirm($message, false); |
||
| 99 | $dryRun = (bool) $input->getOption('dry-run'); |
||
| 100 | $dumpSQL = (bool) $input->getOption('dump-sql'); |
||
| 101 | |||
| 102 | if ($confirm) { |
||
| 103 | /** @var Configuration $configuration */ |
||
| 104 | $configuration = $provider->getConfiguration(); |
||
| 105 | |||
| 106 | $progressBar = new ProgressBar($output, $count); |
||
| 107 | $progressBar->setBarWidth(70); |
||
| 108 | $progressBar->setFormat("%message%\n".$progressBar->getFormatDefinition('debug')); |
||
| 109 | |||
| 110 | $progressBar->setMessage('Starting...'); |
||
| 111 | $progressBar->start(); |
||
| 112 | |||
| 113 | $queries = []; |
||
| 114 | foreach ($repository as $name => $entities) { |
||
| 115 | foreach ($entities as $entity => $tablename) { |
||
| 116 | $auditTable = $this->computeAuditTablename($tablename, $configuration); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var QueryBuilder |
||
| 120 | */ |
||
| 121 | $queryBuilder = $storageServices[$name]->getEntityManager()->getConnection()->createQueryBuilder(); |
||
| 122 | $queryBuilder |
||
| 123 | ->delete($auditTable) |
||
| 124 | ->where('created_at < :until') |
||
| 125 | ->setParameter(':until', $until->format('Y-m-d')) |
||
| 126 | ; |
||
| 127 | |||
| 128 | if ($dumpSQL) { |
||
| 129 | $queries[] = str_replace(':until', "'".$until->format('Y-m-d')."'", $queryBuilder->getSQL()); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (!$dryRun) { |
||
| 133 | $queryBuilder->execute(); |
||
| 134 | } |
||
| 135 | |||
| 136 | $progressBar->setMessage("Cleaning audit tables... (<info>{$auditTable}</info>)"); |
||
| 137 | $progressBar->advance(); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $progressBar->setMessage('Cleaning audit tables... (<info>done</info>)'); |
||
| 142 | $progressBar->display(); |
||
| 143 | |||
| 144 | $io->newLine(); |
||
| 145 | if ($dumpSQL) { |
||
| 146 | $io->newLine(); |
||
| 147 | $io->writeln('SQL queries to be run:'); |
||
| 148 | foreach ($queries as $query) { |
||
| 149 | $io->writeln($query); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $io->newLine(); |
||
| 154 | $io->success('Success.'); |
||
| 155 | } else { |
||
| 156 | $io->success('Cancelled.'); |
||
| 157 | } |
||
| 158 | |||
| 159 | // if not released explicitly, Symfony releases the lock |
||
| 160 | // automatically when the execution of the command ends |
||
| 161 | $this->release(); |
||
| 162 | |||
| 163 | return 0; |
||
| 164 | } |
||
| 232 |