Files
roxane/app/Console/Commands/SyncNextcloudMembers.php

119 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use App\Models\Member;
use App\Models\NextCloudMember;
use App\Services\Nextcloud\NextcloudService;
use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Command\Command as CommandAlias;
2026-01-30 15:27:23 +01:00
use function Laravel\Prompts\progress;
class SyncNextcloudMembers extends Command
{
protected $signature = 'nextcloud:sync-members
{--dry-run : Ne pas écrire en base}
{--member= : Synchroniser un seul member_id}';
protected $description = 'Synchronise les comptes Nextcloud avec les adhérents';
public function __construct(
protected NextcloudService $nextcloud
) {
parent::__construct();
}
/**
* @throws ConnectionException
*/
public function handle(): int
{
$dryRun = $this->option('dry-run');
$memberFilter = $this->option('member');
$this->info(
$dryRun
? 'DRY-RUN activé'
: 'Synchronisation Nextcloud → Members'
);
$members = Member::query()
->where('email', 'like', '%@retzien.fr%')
->when($memberFilter, fn ($q) => $q->where('id', $memberFilter))
->get()
->filter(fn (Member $m) => !empty($m->retzien_email))
->keyBy(fn (Member $m) => strtolower($m->retzien_email));
if ($members->isEmpty()) {
$this->warn('Aucun membre à synchroniser');
return CommandAlias::SUCCESS;
}
2026-01-30 15:27:23 +01:00
$this->info("{$members->count()} membres à synchroniser");
$userIds = $this->nextcloud->listUsers();
$this->info(count($userIds) . ' comptes Nextcloud trouvés');
2026-01-30 15:27:23 +01:00
$progress = null;
if (!$dryRun) {
$progress = progress(
label: 'Synchronisation des membres',
steps: $members->count()
);
$progress->start();
}
$synced = 0;
foreach ($userIds as $userId) {
try {
$details = $this->nextcloud->getUserDetails($userId);
$email = strtolower($details['email'] ?? '');
if (!$email || !$members->has($email)) {
continue;
}
$member = $members[$email];
if ($dryRun) {
$this->line("[DRY-RUN] {$member->id}{$userId}");
} else {
NextCloudMember::query()->updateOrCreate(
[
'member_id' => $member->id,
'nextcloud_user_id' => $userId,
],
2026-01-30 15:27:23 +01:00
[
'data' => json_encode($details, JSON_THROW_ON_ERROR),
]
);
2026-01-30 15:27:23 +01:00
$progress->advance();
}
$synced++;
} catch (\Throwable $e) {
Log::error('Erreur sync Nextcloud', [
'user_id' => $userId,
'error' => $e->getMessage(),
]);
}
}
2026-01-30 15:27:23 +01:00
if ($progress) {
$progress->finish();
$this->newLine();
}
$this->info("Synchronisation terminée ({$synced} comptes liés)");
return CommandAlias::SUCCESS;
}
}