| Conditions | 8 |
| Paths | 86 |
| Total Lines | 86 |
| Code Lines | 59 |
| 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 |
||
| 82 | public function replayEvents(OutputInterface $output, int $increments): void |
||
| 83 | { |
||
| 84 | $preparationProgress = new ProgressBar($output, 3); |
||
| 85 | $preparationProgress->setFormat('event_replay'); |
||
| 86 | |||
| 87 | $preparationProgress->setMessage('Starting Transaction'); |
||
| 88 | $this->connectionHelper->beginTransaction(); |
||
| 89 | $preparationProgress->clear(); |
||
| 90 | $preparationProgress->advance(); |
||
| 91 | |||
| 92 | try { |
||
| 93 | $preparationProgress->setMessage('Removing data from Read Tables'); |
||
| 94 | $preparationProgress->clear(); |
||
| 95 | $this->wipeReadTables($output); |
||
| 96 | |||
| 97 | $preparationProgress->setMessage('Done wiping'); |
||
| 98 | $preparationProgress->clear(); |
||
| 99 | $preparationProgress->advance(); |
||
| 100 | |||
| 101 | $preparationProgress->setMessage('Determining amount of events to replay...'); |
||
| 102 | $preparationProgress->clear(); |
||
| 103 | $totalEvents = $this->eventHydrator->getCount(); |
||
| 104 | |||
| 105 | $preparationProgress->advance(); |
||
| 106 | |||
| 107 | if ($totalEvents == 0) { |
||
| 108 | // Spaces are needed to overwrite the previous message. |
||
| 109 | $preparationProgress->setMessage('There are no events to replay. Done. '); |
||
| 110 | $preparationProgress->clear(); |
||
| 111 | $preparationProgress->finish(); |
||
| 112 | return; |
||
| 113 | } else { |
||
| 114 | $defaultMessage = sprintf( |
||
| 115 | 'Found <comment>%s</comment> Events, replaying in increments of <comment>%d</comment>', |
||
| 116 | $totalEvents, |
||
| 117 | $increments, |
||
| 118 | ); |
||
| 119 | $preparationProgress->setMessage($defaultMessage); |
||
| 120 | $preparationProgress->clear(); |
||
| 121 | $preparationProgress->finish(); |
||
| 122 | } |
||
| 123 | |||
| 124 | $replayProgress = new ProgressBar($output, $totalEvents); |
||
| 125 | $replayProgress->setFormat('event_replay'); |
||
| 126 | $replayProgress->setMessage($defaultMessage); |
||
| 127 | |||
| 128 | for ($count = 0; $count < $totalEvents; $count += $increments) { |
||
| 129 | $eventStream = $this->eventHydrator->getFromTill($increments, $count); |
||
| 130 | |||
| 131 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) { |
||
| 132 | $messages = []; |
||
| 133 | foreach ($eventStream->getIterator() as $event) { |
||
| 134 | /** @var DomainMessage $event */ |
||
| 135 | $messages[] = sprintf( |
||
| 136 | ' > <info>Publishing Event <comment>%s</comment> "<comment>%s</comment>" for UUID <comment>"%s</comment>"</info>', |
||
| 137 | $event->getRecordedOn()->toString(), |
||
| 138 | $event->getType(), |
||
| 139 | $event->getId(), |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | $output->writeln($messages); |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->eventBus->publish($eventStream); |
||
| 147 | $this->eventBus->flush(); |
||
| 148 | |||
| 149 | unset($eventStream); |
||
| 150 | $steps = (($count + $increments < $totalEvents) ? $increments : ($totalEvents - $count)); |
||
| 151 | $replayProgress->advance($steps); |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->connectionHelper->commit(); |
||
| 155 | $replayProgress->finish(); |
||
| 156 | |||
| 157 | $output->writeln(['', '<info>Done</info>', '']); |
||
| 158 | } catch (Throwable $e) { |
||
| 159 | echo $e->getMessage()."\n"; |
||
| 160 | |||
| 161 | $this->connectionHelper->rollBack(); |
||
| 162 | if (isset($replayProgress)) { |
||
| 163 | $replayProgress->setMessage(sprintf('<error>ERROR OCCURRED: "%s"</error>', $e->getMessage())); |
||
| 164 | $replayProgress->finish(); |
||
| 165 | } |
||
| 166 | |||
| 167 | throw $e; |
||
| 168 | } |
||
| 215 |