/
GenerateBoringAvatars.php
59 lines (47 loc) · 1.85 KB
/
GenerateBoringAvatars.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
* This file is part of ianm/boring-avatars.
*
* Copyright (c) 2024 IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace IanM\BoringAvatars\Console;
use Flarum\Bus\Dispatcher;
use Flarum\User\User;
use IanM\BoringAvatars\BoringAvatar;
use IanM\BoringAvatars\Command\GenerateAvatar;
use Illuminate\Console\Command;
class GenerateBoringAvatars extends Command
{
protected $signature = 'boringavatars:generate {--force : Generate avatars for all users regardless of their current state}';
protected $description = 'Generates Boring Avatars for all users based on the current settings.';
protected $updateCount = 0;
protected $notRequired = 0;
public function __construct(public Dispatcher $bus)
{
parent::__construct();
}
public function handle()
{
$this->info('Generating avatars. This may take some time if you have many users!');
$this->output->progressStart(User::query()->count());
User::query()->each(function (User $user) {
// If the force option is used or the user doesn't have an avatar, generate one
if ($this->option('force') || $user->user_svg === null) {
if ($user->user_svg !== null) {
$user->user_svg = null;
$user->save();
}
$this->bus->dispatch(new GenerateAvatar($user, BoringAvatar::$defaultGenerationSize, BoringAvatar::$defaultSquareAvatar));
$this->updateCount++;
} else {
$this->notRequired++;
}
$this->output->progressAdvance();
});
$this->output->progressFinish();
$this->info("Updated {$this->updateCount} users, {$this->notRequired} users did not need updating.");
}
}