sander3 /
laravel-gdpr
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Soved\Laravel\Gdpr\Jobs\Cleanup\Strategies; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Database\Eloquent\Collection; |
||
| 7 | use Illuminate\Contracts\Auth\Authenticatable; |
||
| 8 | use Soved\Laravel\Gdpr\Events\GdprInactiveUser; |
||
| 9 | use Soved\Laravel\Gdpr\Jobs\Cleanup\CleanupStrategy; |
||
| 10 | use Soved\Laravel\Gdpr\Events\GdprInactiveUserDeleted; |
||
| 11 | |||
| 12 | class DefaultStrategy extends CleanupStrategy |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Execute cleanup strategy. |
||
| 16 | * |
||
| 17 | * @return void |
||
| 18 | */ |
||
| 19 | public function execute(Collection $users) |
||
| 20 | { |
||
| 21 | $config = $this->config->get('gdpr.cleanup.defaultStrategy'); |
||
| 22 | |||
| 23 | // Users are considered inactive if their last activity is older than this timestamp |
||
| 24 | $inactivity = Carbon::now() |
||
| 25 | ->subMonths($config['keepInactiveUsersForMonths']); |
||
| 26 | |||
| 27 | $this->notifyInactiveUsers( |
||
| 28 | $inactivity, |
||
| 29 | $config['notifyUsersDaysBeforeDeletion'], |
||
| 30 | $users |
||
| 31 | ); |
||
| 32 | |||
| 33 | $this->deleteInactiveUsers($inactivity, $users); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Notify inactive users about their deletion. |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | private function notifyInactiveUsers( |
||
| 42 | Carbon $inactivity, |
||
| 43 | int $notificationThreshold, |
||
| 44 | Collection $users |
||
| 45 | ) { |
||
| 46 | $users->filter( |
||
| 47 | function (Authenticatable $user) use ($inactivity, $notificationThreshold) { |
||
| 48 | return $user->last_activity->diffInDays($inactivity) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 49 | === $notificationThreshold; |
||
| 50 | } |
||
| 51 | )->each(function (Authenticatable $user) { |
||
| 52 | event(new GdprInactiveUser($user)); |
||
| 53 | }); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Delete inactive users. |
||
| 58 | * |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | private function deleteInactiveUsers( |
||
| 62 | Carbon $inactivity, |
||
| 63 | Collection $users |
||
| 64 | ) { |
||
| 65 | $users->filter(function (Authenticatable $user) use ($inactivity) { |
||
| 66 | return $user->last_activity < $inactivity; |
||
|
0 ignored issues
–
show
|
|||
| 67 | })->each(function (Authenticatable $user) { |
||
| 68 | $user->delete(); |
||
| 69 | |||
| 70 | event(new GdprInactiveUserDeleted($user)); |
||
| 71 | }); |
||
| 72 | } |
||
| 73 | } |
||
| 74 |