| Conditions | 6 |
| Paths | 12 |
| Total Lines | 60 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 81 | private function duplicateSession(Session $session, bool $debug, OutputInterface $output): Session |
||
| 82 | { |
||
| 83 | // Calculate new session dates based on the duration of the original session |
||
| 84 | $duration = $session->getAccessEndDate()->diff($session->getAccessStartDate()); |
||
| 85 | $newStartDate = (clone $session->getAccessEndDate())->modify('+1 day'); |
||
| 86 | $newEndDate = (clone $newStartDate)->add($duration); |
||
| 87 | |||
| 88 | if ($debug) { |
||
| 89 | $output->writeln(sprintf('Duplicating session %d. New start date: %s, New end date: %s', |
||
| 90 | $session->getId(), |
||
| 91 | $newStartDate->format('Y-m-d H:i:s'), |
||
| 92 | $newEndDate->format('Y-m-d H:i:s') |
||
| 93 | )); |
||
| 94 | } |
||
| 95 | |||
| 96 | // Create a new session with the same details as the original session |
||
| 97 | $newSession = new Session(); |
||
| 98 | $newSession |
||
| 99 | ->setTitle($session->getTitle() . ' (Repetition ' . $session->getId() . ' - ' . time() . ')') |
||
| 100 | ->setAccessStartDate($newStartDate) |
||
| 101 | ->setAccessEndDate($newEndDate) |
||
| 102 | ->setDisplayStartDate($newStartDate) |
||
| 103 | ->setDisplayEndDate($newEndDate) |
||
| 104 | ->setCoachAccessStartDate($newStartDate) |
||
| 105 | ->setCoachAccessEndDate($newEndDate) |
||
| 106 | ->setVisibility($session->getVisibility()) |
||
| 107 | ->setDuration($session->getDuration()) |
||
| 108 | ->setDescription($session->getDescription() ?? '') |
||
| 109 | ->setShowDescription($session->getShowDescription() ?? false) |
||
| 110 | ->setCategory($session->getCategory()) |
||
| 111 | ->setPromotion($session->getPromotion()) |
||
| 112 | ->setLastRepetition(false); |
||
| 113 | |||
| 114 | // Copy the AccessUrls from the original session |
||
| 115 | $accessUrls = $session->getUrls(); |
||
| 116 | |||
| 117 | if ($accessUrls->isEmpty()) { |
||
| 118 | // Handle the case where the session does not have any AccessUrl |
||
| 119 | if ($debug) { |
||
| 120 | $output->writeln('No AccessUrl found for session ' . $session->getId() . '. Assigning default AccessUrl.'); |
||
| 121 | } |
||
| 122 | |||
| 123 | // Retrieve or create a default AccessUrl (you need to adjust this based on your system's needs) |
||
| 124 | $defaultAccessUrl = $this->getDefaultAccessUrl(); |
||
| 125 | $newSession->addAccessUrl($defaultAccessUrl); |
||
| 126 | } else { |
||
| 127 | foreach ($accessUrls as $accessUrl) { |
||
| 128 | $newSession->addAccessUrl($accessUrl->getUrl()); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // Save the new session |
||
| 133 | $this->entityManager->persist($newSession); |
||
| 134 | $this->entityManager->flush(); |
||
| 135 | |||
| 136 | if ($debug) { |
||
| 137 | $output->writeln(sprintf('New session %d created successfully.', $newSession->getId())); |
||
| 138 | } |
||
| 139 | |||
| 140 | return $newSession; |
||
| 141 | } |
||
| 206 |