Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
4.42% |
8 / 181 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| UserMergeCommand | |
4.42% |
8 / 181 |
|
20.00% |
1 / 5 |
2413.07 | |
0.00% |
0 / 1 |
| getAllowedAppEnvs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| configure | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
6 | |||
| getUserOverview | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
2 | |||
| makeChanges | |
0.00% |
0 / 123 |
|
0.00% |
0 / 1 |
2256 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Command; |
| 4 | |
| 5 | use Olz\Command\Common\OlzCommand; |
| 6 | use Olz\Entity\AccessToken; |
| 7 | use Olz\Entity\Anmelden\Booking; |
| 8 | use Olz\Entity\News\NewsEntry; |
| 9 | use Olz\Entity\NotificationSubscription; |
| 10 | use Olz\Entity\Quiz\SkillLevel; |
| 11 | use Olz\Entity\StravaLink; |
| 12 | use Olz\Entity\TelegramLink; |
| 13 | use Olz\Entity\Termine\TerminNotification; |
| 14 | use Olz\Entity\Termine\TerminNotificationTemplate; |
| 15 | use Olz\Entity\Users\User; |
| 16 | use Symfony\Component\Console\Attribute\AsCommand; |
| 17 | use Symfony\Component\Console\Command\Command; |
| 18 | use Symfony\Component\Console\Input\InputArgument; |
| 19 | use Symfony\Component\Console\Input\InputInterface; |
| 20 | use Symfony\Component\Console\Input\InputOption; |
| 21 | use Symfony\Component\Console\Output\OutputInterface; |
| 22 | |
| 23 | #[AsCommand(name: 'olz:user-merge')] |
| 24 | class UserMergeCommand extends OlzCommand { |
| 25 | /** @return array<string> */ |
| 26 | protected function getAllowedAppEnvs(): array { |
| 27 | return ['dev', 'test', 'staging', 'prod']; |
| 28 | } |
| 29 | |
| 30 | protected function configure(): void { |
| 31 | $this->addArgument('target', InputArgument::REQUIRED, 'Target user ID'); |
| 32 | $this->addArgument('source', InputArgument::REQUIRED, 'Source user ID'); |
| 33 | $this->addOption( |
| 34 | 'dry', |
| 35 | null, |
| 36 | InputOption::VALUE_NONE, |
| 37 | 'Do not actually make the changes', |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | protected function handle(InputInterface $input, OutputInterface $output): int { |
| 42 | $target_id = intval($input->getArgument('target')); |
| 43 | $source_id = intval($input->getArgument('source')); |
| 44 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 45 | $target_user = $user_repo->findOneBy(['id' => $target_id]); |
| 46 | $source_user = $user_repo->findOneBy(['id' => $source_id]); |
| 47 | $this->generalUtils()->checkNotNull($target_user, "Target user not found"); |
| 48 | $this->generalUtils()->checkNotNull($source_user, "Source user not found"); |
| 49 | array_map( |
| 50 | fn ($line) => $this->log()->info($line), |
| 51 | explode("\n", <<<ZZZZZZZZZZ |
| 52 | |
| 53 | Target |
| 54 | ------ |
| 55 | {$this->getUserOverview($target_user)} |
| 56 | |
| 57 | Source |
| 58 | ------ |
| 59 | {$this->getUserOverview($source_user)} |
| 60 | |
| 61 | ZZZZZZZZZZ), |
| 62 | ); |
| 63 | $dry = $input->getOption('dry'); |
| 64 | if (!$dry) { |
| 65 | $this->makeChanges($target_user, $source_user); |
| 66 | $this->entityManager()->flush(); |
| 67 | array_map( |
| 68 | fn ($line) => $this->log()->info($line), |
| 69 | explode("\n", <<<ZZZZZZZZZZ |
| 70 | |
| 71 | Result |
| 72 | ------ |
| 73 | {$this->getUserOverview($target_user)} |
| 74 | |
| 75 | ZZZZZZZZZZ), |
| 76 | ); |
| 77 | } |
| 78 | return Command::SUCCESS; |
| 79 | } |
| 80 | |
| 81 | protected function getUserOverview(User $user): string { |
| 82 | $overview = str_replace("\n", "\n ", $user->pretty()); |
| 83 | |
| 84 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
| 85 | $news_entries = $news_repo->findBy(['owner_user' => $user]); |
| 86 | $num_news_entries = count($news_entries); |
| 87 | |
| 88 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
| 89 | $subscriptions = $notification_subscription_repo->findBy(['user' => $user]); |
| 90 | $num_subscriptions = count($subscriptions); |
| 91 | |
| 92 | $telegram_link_repo = $this->entityManager()->getRepository(TelegramLink::class); |
| 93 | $telegram_links = $telegram_link_repo->findBy(['user' => $user]); |
| 94 | $num_telegram_links = count($telegram_links); |
| 95 | |
| 96 | $strava_link_repo = $this->entityManager()->getRepository(StravaLink::class); |
| 97 | $strava_links = $strava_link_repo->findBy(['user' => $user]); |
| 98 | $num_strava_links = count($strava_links); |
| 99 | |
| 100 | $access_token_repo = $this->entityManager()->getRepository(AccessToken::class); |
| 101 | $access_tokens = $access_token_repo->findBy(['user' => $user]); |
| 102 | $num_access_tokens = count($access_tokens); |
| 103 | |
| 104 | return <<<ZZZZZZZZZZ |
| 105 | {$overview} |
| 106 | News ({$num_news_entries}) |
| 107 | Notification subscriptions ({$num_subscriptions}) |
| 108 | Telegram links ({$num_telegram_links}) |
| 109 | Strava links ({$num_strava_links}) |
| 110 | Access tokens ({$num_access_tokens}) |
| 111 | |
| 112 | ZZZZZZZZZZ; |
| 113 | } |
| 114 | |
| 115 | protected function makeChanges(User $target_user, User $source_user): void { |
| 116 | if ($target_user->getFirstName() !== $source_user->getFirstName()) { |
| 117 | $this->log()->notice("FirstName {$target_user->getFirstName()} vs. {$source_user->getFirstName()}"); |
| 118 | } |
| 119 | if ($target_user->getLastName() !== $source_user->getLastName()) { |
| 120 | $this->log()->notice("LastName {$target_user->getLastName()} vs. {$source_user->getLastName()}"); |
| 121 | } |
| 122 | $this->log()->info("OldUsername {$target_user->getOldUsername()} => {$source_user->getUsername()}"); |
| 123 | $target_user->setOldUsername($source_user->getUsername()); |
| 124 | if (!$target_user->getEmail() && $source_user->getEmail()) { |
| 125 | $this->log()->info("Email merge"); |
| 126 | $target_user->setEmail($source_user->getEmail()); |
| 127 | } |
| 128 | if (!$target_user->getPasswordHash() && $source_user->getPasswordHash()) { |
| 129 | $this->log()->info("PasswordHash merge"); |
| 130 | $target_user->setPasswordHash($source_user->getPasswordHash()); |
| 131 | } |
| 132 | if ($target_user->getParentUserId() !== $source_user->getParentUserId()) { |
| 133 | $this->log()->notice("ParentUserId {$target_user->getParentUserId()} vs. {$source_user->getParentUserId()}"); |
| 134 | } |
| 135 | foreach ($source_user->getPermissionMap() as $key => $value) { |
| 136 | $this->log()->info("addPermission {$key}"); |
| 137 | $target_user->addPermission($key); |
| 138 | } |
| 139 | if ($target_user->getRoot() !== $source_user->getRoot()) { |
| 140 | $this->log()->notice("root {$target_user->getRoot()} vs. {$source_user->getRoot()}"); |
| 141 | } |
| 142 | if (!$target_user->getGender() && $source_user->getGender()) { |
| 143 | $this->log()->info("Gender merge"); |
| 144 | $target_user->setGender($source_user->getGender()); |
| 145 | } |
| 146 | if (!$target_user->getStreet() && $source_user->getStreet()) { |
| 147 | $this->log()->info("Street merge"); |
| 148 | $target_user->setStreet($source_user->getStreet()); |
| 149 | } |
| 150 | if (!$target_user->getPostalCode() && $source_user->getPostalCode()) { |
| 151 | $this->log()->info("PostalCode merge"); |
| 152 | $target_user->setPostalCode($source_user->getPostalCode()); |
| 153 | } |
| 154 | if (!$target_user->getCity() && $source_user->getCity()) { |
| 155 | $this->log()->info("City merge"); |
| 156 | $target_user->setCity($source_user->getCity()); |
| 157 | } |
| 158 | if (!$target_user->getRegion() && $source_user->getRegion()) { |
| 159 | $this->log()->info("Region merge"); |
| 160 | $target_user->setRegion($source_user->getRegion()); |
| 161 | } |
| 162 | if (!$target_user->getCountryCode() && $source_user->getCountryCode()) { |
| 163 | $this->log()->info("CountryCode merge"); |
| 164 | $target_user->setCountryCode($source_user->getCountryCode()); |
| 165 | } |
| 166 | if (!$target_user->getBirthdate() && $source_user->getBirthdate()) { |
| 167 | $this->log()->info("Birthdate merge"); |
| 168 | $target_user->setBirthdate($source_user->getBirthdate()); |
| 169 | } |
| 170 | if (!$target_user->getPhone() && $source_user->getPhone()) { |
| 171 | $this->log()->info("Phone merge"); |
| 172 | $target_user->setPhone($source_user->getPhone()); |
| 173 | } |
| 174 | if (!$target_user->getSolvNumber() && $source_user->getSolvNumber()) { |
| 175 | $this->log()->info("SolvNumber merge"); |
| 176 | $target_user->setSolvNumber($source_user->getSolvNumber()); |
| 177 | } |
| 178 | if (!$target_user->getSiCardNumber() && $source_user->getSiCardNumber()) { |
| 179 | $this->log()->info("SiCardNumber merge"); |
| 180 | $target_user->setSiCardNumber($source_user->getSiCardNumber()); |
| 181 | } |
| 182 | |
| 183 | // Roles |
| 184 | foreach ($source_user->getRoles() as $role) { |
| 185 | $this->log()->info("addRole {$role->getUsername()}"); |
| 186 | $target_user->addRole($role); |
| 187 | } |
| 188 | |
| 189 | // Avatar |
| 190 | $data_path = $this->envUtils()->getDataPath(); |
| 191 | $target_path = "{$data_path}img/users/{$target_user->getId()}"; |
| 192 | $source_path = "{$data_path}img/users/{$source_user->getId()}"; |
| 193 | if (!$target_user->getAvatarImageId() && $source_user->getAvatarImageId()) { |
| 194 | $this->log()->info("AvatarImageId merge"); |
| 195 | $was_successful = rename($source_path, $target_path); |
| 196 | if ($was_successful) { |
| 197 | $target_user->setAvatarImageId($source_user->getAvatarImageId()); |
| 198 | $this->log()->info("Avatar successfully moved"); |
| 199 | } else { |
| 200 | $this->log()->info("Failed moving avatar"); |
| 201 | } |
| 202 | } |
| 203 | $this->generalUtils()->removeRecursive($source_path); |
| 204 | |
| 205 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
| 206 | $news_entries = $news_repo->findBy(['author_user' => $source_user]); |
| 207 | foreach ($news_entries as $news_entry) { |
| 208 | $this->log()->info("NewsEntry::setAuthorUser {$news_entry->getId()}"); |
| 209 | $news_entry->setAuthorUser($target_user); |
| 210 | } |
| 211 | |
| 212 | $termin_notification_repo = $this->entityManager()->getRepository(TerminNotification::class); |
| 213 | $termin_notifications = $termin_notification_repo->findBy(['recipient_user' => $source_user]); |
| 214 | foreach ($termin_notifications as $termin_notification) { |
| 215 | $this->log()->info("TerminNotification::setRecipientUser {$termin_notification->getId()}"); |
| 216 | $termin_notification->setRecipientUser($target_user); |
| 217 | } |
| 218 | |
| 219 | $termin_notification_template_repo = $this->entityManager()->getRepository(TerminNotificationTemplate::class); |
| 220 | $termin_notification_templates = $termin_notification_template_repo->findBy(['recipient_user' => $source_user]); |
| 221 | foreach ($termin_notification_templates as $termin_notification_template) { |
| 222 | $this->log()->info("TerminNotificationTemplate::setRecipientUser {$termin_notification_template->getId()}"); |
| 223 | $termin_notification_template->setRecipientUser($target_user); |
| 224 | } |
| 225 | |
| 226 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); |
| 227 | $subscriptions = $notification_subscription_repo->findBy(['user' => $source_user]); |
| 228 | foreach ($subscriptions as $subscription) { |
| 229 | $this->log()->info("NotificationSubscription::setUser {$subscription->getId()}"); |
| 230 | $subscription->setUser($target_user); |
| 231 | } |
| 232 | |
| 233 | $telegram_link_repo = $this->entityManager()->getRepository(TelegramLink::class); |
| 234 | $telegram_links = $telegram_link_repo->findBy(['user' => $source_user]); |
| 235 | foreach ($telegram_links as $telegram_link) { |
| 236 | $this->log()->info("TelegramLink::setUser {$telegram_link->getId()}"); |
| 237 | $telegram_link->setUser($target_user); |
| 238 | } |
| 239 | |
| 240 | $strava_link_repo = $this->entityManager()->getRepository(StravaLink::class); |
| 241 | $strava_links = $strava_link_repo->findBy(['user' => $source_user]); |
| 242 | foreach ($strava_links as $strava_link) { |
| 243 | $this->log()->info("StravaLink::setUser {$strava_link->getId()}"); |
| 244 | $strava_link->setUser($target_user); |
| 245 | } |
| 246 | |
| 247 | $access_token_repo = $this->entityManager()->getRepository(AccessToken::class); |
| 248 | $access_tokens = $access_token_repo->findBy(['user' => $source_user]); |
| 249 | foreach ($access_tokens as $access_token) { |
| 250 | $this->log()->info("AccessToken::setUser {$access_token->getId()}"); |
| 251 | $access_token->setUser($target_user); |
| 252 | } |
| 253 | |
| 254 | $booking_repo = $this->entityManager()->getRepository(Booking::class); |
| 255 | $bookings = $booking_repo->findBy(['user' => $source_user]); |
| 256 | foreach ($bookings as $booking) { |
| 257 | $this->log()->info("Booking::setUser {$booking->getId()}"); |
| 258 | $booking->setUser($target_user); |
| 259 | } |
| 260 | |
| 261 | $skill_level_repo = $this->entityManager()->getRepository(SkillLevel::class); |
| 262 | $skill_levels = $skill_level_repo->findBy(['user' => $source_user]); |
| 263 | foreach ($skill_levels as $skill_level) { |
| 264 | $this->log()->info("SkillLevel remove {$skill_level->getId()}"); |
| 265 | $this->entityManager()->remove($skill_level); |
| 266 | } |
| 267 | |
| 268 | foreach ($this->entityUtils()->olzEntityClasses() as $class) { |
| 269 | $olz_entity_repo = $this->entityManager()->getRepository($class); |
| 270 | $olz_entities = $olz_entity_repo->findBy(['owner_user' => $source_user]); |
| 271 | foreach ($olz_entities as $olz_entity) { |
| 272 | $this->log()->info("{$class}::setOwnerUser (created at {$olz_entity->getCreatedAt()->format('Y-m-d H:i:s')})"); |
| 273 | $olz_entity->setOwnerUser($target_user); |
| 274 | } |
| 275 | $olz_entities = $olz_entity_repo->findBy(['created_by_user' => $source_user]); |
| 276 | foreach ($olz_entities as $olz_entity) { |
| 277 | $this->log()->info("{$class}::setCreatedByUser (created at {$olz_entity->getCreatedAt()->format('Y-m-d H:i:s')})"); |
| 278 | $olz_entity->setCreatedByUser($target_user); |
| 279 | } |
| 280 | $olz_entities = $olz_entity_repo->findBy(['last_modified_by_user' => $source_user]); |
| 281 | foreach ($olz_entities as $olz_entity) { |
| 282 | $this->log()->info("{$class}::setLastModifiedByUser (created at {$olz_entity->getCreatedAt()->format('Y-m-d H:i:s')})"); |
| 283 | $olz_entity->setLastModifiedByUser($target_user); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | $source_user->softDelete(); |
| 288 | } |
| 289 | } |