DeleteNotifications   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 17 4
1
<?php namespace JobApis\JobsToMail\Console\Commands;
2
3
use Carbon\Carbon;
4
use Illuminate\Console\Command;
5
use Illuminate\Foundation\Bus\DispatchesJobs;
6
use JobApis\JobsToMail\Models\CustomDatabaseNotification;
7
8
class DeleteNotifications extends Command
9
{
10
    use DispatchesJobs;
11
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'notifications:delete {--id=}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Removes old notifications from the database.';
25
26
    /**
27
     * @var CustomDatabaseNotification
28
     */
29
    public $notifications;
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct(CustomDatabaseNotification $notifications)
37
    {
38
        parent::__construct();
39
        $this->notifications = $notifications;
40
    }
41
42
    /**
43
     * Goes through each user and queues up a task to collect and email them their jobs.
44
     *
45
     * @return mixed
46
     */
47
    public function handle()
48
    {
49
        $count = 0;
50
        if ($id = $this->option('id')) {
51
            $this->notifications->where("id", $id)->delete();
52
            $count++;
53
        } else {
54
            $results = $this->notifications->where("created_at", "<", Carbon::now()->subDays(7))->get();
55
            if ($results) {
56
                foreach ($results as $notification) {
57
                    $notification->delete();
58
                    $count++;
59
                }
60
            }
61
        }
62
        return $this->info("{$count} notifications deleted.");
63
    }
64
}
65