Passed
Push — master ( de070a...e0aded )
by Dan Michael O.
06:03
created

PurgeNotifications   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 24 4
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Notifications\ExtendedDatabaseNotification;
6
use Carbon\Carbon;
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon 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...
7
8
class PurgeNotifications extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'bibrex:purge-notifications';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Purge old notifications.';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return mixed
28
     */
29
    public function handle()
30
    {
31
        $storageTime = (int) config('bibrex.storage_time.notifications');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        $storageTime = (int) /** @scrutinizer ignore-call */ config('bibrex.storage_time.notifications');
Loading history...
32
        if ($storageTime <= 0) {
33
            $this->logError('The `bibrex.storage_time.notifications` config value is invalid.');
34
            return;
35
        }
36
37
        $n = 0;
38
        $m = 0;
39
40
        // Check imported users
41
        $notifications = ExtendedDatabaseNotification::where('created_at', '<', Carbon::now()->subDays($storageTime))
42
            ->get();
43
44
        foreach ($notifications as $notification) {
45
            if ($notification->loan->trashed()) {
46
                $notification->delete();
47
                $m++;
48
            }
49
            $n++;
50
        }
51
52
        $this->logInfo("Sletting av gamle meldinger: $m av $n meldinger eldre enn $storageTime dager ble slettet.");
53
    }
54
}
55