69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Compliance\AccountAnonymizer;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class AnonymizeAccount implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
public function __construct(private readonly ?int $userId = null, private readonly ?int $tenantId = null)
|
|
{
|
|
if ($this->userId === null && $this->tenantId === null) {
|
|
throw new \InvalidArgumentException('An anonymization job requires either a user or tenant id.');
|
|
}
|
|
|
|
$this->onQueue('default');
|
|
}
|
|
|
|
public function handle(AccountAnonymizer $anonymizer): void
|
|
{
|
|
if ($this->userId) {
|
|
$user = User::with('tenant')->find($this->userId);
|
|
|
|
if (! $user || $user->tenant?->anonymized_at) {
|
|
return;
|
|
}
|
|
|
|
$anonymizer->anonymize($user);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($this->tenantId) {
|
|
$tenant = Tenant::with('user')->find($this->tenantId);
|
|
|
|
if (! $tenant || $tenant->anonymized_at) {
|
|
return;
|
|
}
|
|
|
|
if ($tenant->user) {
|
|
$anonymizer->anonymize($tenant->user);
|
|
} else {
|
|
$anonymizer->anonymizeTenantOnly($tenant);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function userId(): ?int
|
|
{
|
|
return $this->userId;
|
|
}
|
|
|
|
public function tenantId(): ?int
|
|
{
|
|
return $this->tenantId;
|
|
}
|
|
}
|