FlushOldDevices::fire()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PiFinder\Console\Commands;
4
5
use Carbon\Carbon;
6
use PiFinder\Device;
7
use Illuminate\Console\Command;
8
9
class FlushOldDevices extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'pi:flush';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Remove all devices from the list which don\'t poked us in the last 15 minutes.';
24
25
    /**
26
     * Create a new command instance.
27
     */
28 6
    public function __construct()
29
    {
30 6
        parent::__construct();
31 6
    }
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return mixed
37
     */
38 1
    public function fire()
39
    {
40 1
        $affectedRows = $this->getDevices();
41
42 1
        $message = $this->getMessage($affectedRows);
43
44 1
        $this->info($message);
45 1
    }
46
47
    /**
48
     * @param $affectedRows
49
     *
50
     * @return string
51
     */
52 1
    public function getMessage($affectedRows)
53
    {
54 1
        $message = "Deleted $affectedRows device".($affectedRows == 1 ? '' : 's').'.';
55
56 1
        return $message;
57
    }
58
59
    /**
60
     * Get all devices which should get deleted.
61
     *
62
     * @return mixed
63
     */
64 1
    public function getDevices()
65
    {
66 1
        $date = Carbon::now()->subMinutes(15);
67
68 1
        $devices = Device::where('updated_at', '<', $date)->get();
69
70 1
        $affectedRows = $devices->count();
71
72 1
        foreach ($devices as $device) {
73 1
            $device->delete();
74
        }
75
76 1
        return $affectedRows;
77
    }
78
}
79