Completed
Push — master ( 59fb2e...bf9165 )
by Manuel
03:41
created

UpdateStatistics::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PiFinder\Handlers\Events;
4
5
use Carbon\Carbon;
6
use PiFinder\Poke;
7
use PiFinder\DeviceArchive;
8
use Illuminate\Support\Facades\DB;
9
use PiFinder\Events\ServerWasPoked;
10
use PiFinder\Utilities\ExtractNetwork;
11
12
class UpdateStatistics
13
{
14
    /**
15
     * Handle the event.
16
     *
17
     * @param ServerWasPoked $event
18
     *
19
     * @return void
20
     */
21 5
    public function handle(ServerWasPoked $event)
22
    {
23 5
        $device = $event->getDevice()->toArray();
24
25
        // device count
26 5
        DeviceArchive::updateOrCreate(
27 5
            ['mac_hash' => md5($device['mac'])],
28 5
            ['updated_at' => Carbon::now()]
29
        );
30
31
        // network distribution
32 5
        $network = ExtractNetwork::fromIp($device['ip']);
33 5
        DB::table('network_distribution')->where('network', $network)->increment('pokes');
34
35
        // pokes
36 5
        $date = Carbon::now()->toDateString();
37 5
        Poke::firstOrCreate(['date' => $date])->increment('pokes');
38 5
    }
39
}
40