avatarFetchEnabled()) { $this->error("Avatar fetching is disabled on this instance."); return self::FAILURE; } if ($this->option('users-without-avatars')) { return $this->processUsers(User::query()->whereDoesntHave('avatar')->get()->all(), $userAvatar); } if ($this->option('all')) { return $this->processUsers(User::query()->get()->all(), $userAvatar); } try { $user = $this->fetchProvidedUser(); return $this->processUsers([$user], $userAvatar); } catch (Exception $exception) { $this->error($exception->getMessage()); return self::FAILURE; } } /** * @param User[] $users */ private function processUsers(array $users, UserAvatars $userAvatar): int { $dryRun = !$this->option('force'); $this->info(count($users) . " user(s) found to update avatars for."); if (count($users) === 0) { return self::SUCCESS; } if (!$dryRun) { $fetchHost = parse_url($userAvatar->getAvatarUrl(), PHP_URL_HOST); $this->warn("This will destroy any existing avatar images these users have, and attempt to fetch new avatar images from {$fetchHost}."); $proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to proceed?'); if (!$proceed) { return self::SUCCESS; } } $this->info(""); $exitCode = self::SUCCESS; foreach ($users as $user) { $linePrefix = "[ID: {$user->id}] $user->email -"; if ($dryRun) { $this->warn("{$linePrefix} Not updated"); continue; } if ($this->fetchAvatar($userAvatar, $user)) { $this->info("{$linePrefix} Updated"); } else { $this->error("{$linePrefix} Not updated"); $exitCode = self::FAILURE; } } if ($dryRun) { $this->comment(""); $this->comment("Dry run, no avatars were updated."); $this->comment('Run with -f or --force to perform the update.'); } return $exitCode; } private function fetchAvatar(UserAvatars $userAvatar, User $user): bool { $oldId = $user->avatar->id ?? 0; $userAvatar->fetchAndAssignToUser($user); $user->refresh(); $newId = $user->avatar->id ?? $oldId; return $oldId !== $newId; } }