Passed
Push — master ( bd13ba...272332 )
by Sander
04:42 queued 02:26
created

DefaultStrategy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteInactiveUsers() 0 10 1
A execute() 0 15 1
A notifyInactiveUsers() 0 12 1
1
<?php
2
3
namespace Soved\Laravel\Gdpr\Jobs\Cleanup\Strategies;
4
5
use App\User;
0 ignored issues
show
Bug introduced by
The type App\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Collection;
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
     * @param  \Illuminate\Database\Eloquent\Collection  $users
18
     * @return void
19
     */
20
    public function execute(Collection $users)
21
    {
22
        $config = $this->config->get('gdpr.cleanup.defaultStrategy');
23
24
        // Users are considered inactive if their last activity is older than this timestamp
25
        $inactivity = Carbon::now()
26
            ->subMonths($config['keepInactiveUsersForMonths']);
27
28
        $this->notifyInactiveUsers(
29
            $inactivity,
30
            $config['notifyUsersDaysBeforeDeletion'],
31
            $users
32
        );
33
34
        $this->deleteInactiveUsers($inactivity, $users);
35
    }
36
37
    /**
38
     * Notify inactive users about their deletion.
39
     *
40
     * @param  \Carbon\Carbon  $inactivity
41
     * @param  int  $notificationThreshold
42
     * @param  \Illuminate\Database\Eloquent\Collection  $users
43
     * @return void
44
     */
45
    private function notifyInactiveUsers(
46
        Carbon $inactivity,
47
        int $notificationThreshold,
48
        Collection $users
49
    ) {
50
        $users->filter(
51
            function (User $user) use ($inactivity, $notificationThreshold) {
52
                return $user->last_activity->diffInDays($inactivity)
53
                    === $notificationThreshold;
54
            }
55
        )->each(function (User $user) {
56
            event(new GdprInactiveUser($user));
57
        });
58
    }
59
60
    /**
61
     * Delete inactive users.
62
     *
63
     * @param  \Carbon\Carbon  $inactivity
64
     * @param  \Illuminate\Database\Eloquent\Collection  $users
65
     * @return void
66
     */
67
    private function deleteInactiveUsers(
68
        Carbon $inactivity,
69
        Collection $users
70
    ) {
71
        $users->filter(function (User $user) use ($inactivity) {
72
            return $user->last_activity < $inactivity;
73
        })->each(function (User $user) {
74
            $user->delete();
75
76
            event(new GdprInactiveUserDeleted($user));
77
        });
78
    }
79
}
80