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

163 lines
4.6 KiB
PHP
Raw Normal View History

2026-01-11 18:13:18 +01:00
<?php
namespace App\Console\Commands;
use App\Services\Dolibarr\DolibarrService;
use App\Services\ISPConfig\ISPConfigMailService;
use App\Services\Nextcloud\NextcloudService;
use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Log;
2026-01-18 21:51:57 +01:00
use Symfony\Component\Console\Command\Command as CommandAlias;
2026-01-11 18:13:18 +01:00
class HandleExpiredMembersDolibarr extends Command
{
2026-01-19 10:27:13 +01:00
protected $signature = 'members:cleanup-expired
{email? : Adresse email d\'un adhérent à traiter uniquement}
{--dry-run}';
2026-01-11 18:13:18 +01:00
public function __construct(
protected DolibarrService $dolibarr,
protected ISPConfigMailService $mailService,
protected NextcloudService $nextcloud
) {
parent::__construct();
}
/**
* @throws ConnectionException
*/
public function handle(): int
{
2026-01-18 21:51:57 +01:00
$dryRun = $this->option('dry-run');
2026-01-19 10:27:13 +01:00
$emailFilter = $this->argument('email');
2026-01-18 21:51:57 +01:00
$this->info(
$dryRun
? 'DRY-RUN activé aucune modification ne sera effectuée'
: 'Mode réel activé'
);
2026-01-19 10:27:13 +01:00
if ($emailFilter) {
$this->info("Mode utilisateur unique : {$emailFilter}");
}
2026-01-11 18:13:18 +01:00
$this->info('Récupération des adhérents Dolibarr');
$members = collect($this->dolibarr->getAllMembers());
2026-01-18 21:51:57 +01:00
$today = now()->startOfDay();
2026-01-11 18:13:18 +01:00
2026-01-18 21:51:57 +01:00
$expiredMembers = $members->filter(function ($member) use ($today) {
if (($member['statut'] ?? null) != 1) {
return false;
}
if (empty($member['datefin'])) {
return false;
}
2026-01-11 18:13:18 +01:00
2026-01-18 21:51:57 +01:00
return \Carbon\Carbon::parse($member['datefin'])->lt($today);
});
2026-01-18 14:35:41 +01:00
2026-01-19 10:27:13 +01:00
if ($emailFilter) {
$expiredMembers = $expiredMembers->filter(function ($member) use ($emailFilter) {
$email = $this->extractRetzienEmail($member['email'] ?? null);
return $email === $emailFilter;
});
if ($expiredMembers->isEmpty()) {
$this->warn("Aucun adhérent expiré trouvé pour {$emailFilter}");
return CommandAlias::SUCCESS;
}
}
2026-01-11 18:13:18 +01:00
$this->info("{$expiredMembers->count()} adhérent(s) expiré(s)");
foreach ($expiredMembers as $member) {
try {
2026-01-18 21:51:57 +01:00
$this->processMember($member, $dryRun);
2026-01-11 18:13:18 +01:00
} catch (\Throwable $e) {
Log::error('Erreur traitement adhérent', [
'member_id' => $member['id'],
'error' => $e->getMessage(),
]);
}
}
2026-01-18 21:51:57 +01:00
$this->info(
$dryRun
? 'DRY-RUN terminé aucune action effectuée'
: 'Traitement terminé'
);
return CommandAlias::SUCCESS;
2026-01-11 18:13:18 +01:00
}
2026-01-18 21:51:57 +01:00
/**
* @throws ConnectionException
*/
protected function processMember(array $member, bool $dryRun): void
2026-01-11 18:13:18 +01:00
{
2026-01-18 21:51:57 +01:00
$email = $this->extractRetzienEmail($member['email'] ?? null);
2026-01-11 18:13:18 +01:00
2026-01-18 21:51:57 +01:00
$this->line("{$member['id']} - {$email}");
2026-01-11 18:13:18 +01:00
2026-01-18 21:51:57 +01:00
// Résiliation Dolibarr
if ($dryRun) {
$this->info("[DRY-RUN] Résiliation Dolibarr");
} else {
$this->dolibarr->setMemberStatus($member['id'], '0');
}
2026-01-11 18:13:18 +01:00
2026-01-18 21:51:57 +01:00
// Désactivation mail
2026-01-11 18:13:18 +01:00
if ($email) {
2026-01-18 21:51:57 +01:00
$this->disableMailAccount($email, $dryRun);
2026-01-11 18:13:18 +01:00
}
2026-01-18 21:51:57 +01:00
// Désactivation Nextcloud
2026-01-11 18:13:18 +01:00
if ($email) {
2026-01-18 21:51:57 +01:00
if ($dryRun) {
$this->info("[DRY-RUN] Désactivation Nextcloud");
} else {
$this->nextcloud->disableUserByEmail($email);
}
2026-01-11 18:13:18 +01:00
}
}
2026-01-18 21:51:57 +01:00
protected function disableMailAccount(string $email, bool $dryRun): void
2026-01-11 18:13:18 +01:00
{
$details = $this->mailService->getMailUserDetails($email);
if (!$details) {
$this->warn("Boîte mail inexistante : {$email}");
return;
}
2026-01-18 21:51:57 +01:00
if ($dryRun) {
$this->info("[DRY-RUN] Mail désactivé");
return;
}
2026-01-11 18:13:18 +01:00
$this->mailService->updateMailUser($email, [
'postfix' => 'n',
'disablesmtp' => 'y',
'disableimap' => 'y',
'disablepop3' => 'y',
]);
2026-01-18 14:35:41 +01:00
$this->info("Mail désactivé");
2026-01-11 18:13:18 +01:00
}
2026-01-18 14:35:41 +01:00
2026-01-18 21:51:57 +01:00
protected function extractRetzienEmail(?string $emails): ?string
{
if (!$emails) {
return null;
}
return collect(explode(';', $emails))
->map(fn (string $email): string => trim($email))
->filter(fn (string $email): bool => str_contains($email, '@retzien.fr'))
->first();
}
2026-01-11 18:13:18 +01:00
}