Passed
Push — master ( f51c1b...16736d )
by Arthur
05:36
created

MachineService::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 04.10.18
6
 * Time: 16:17.
7
 */
8
9
namespace Modules\Machine\Services;
10
11
use Carbon\Carbon;
12
use Modules\Machine\Contracts\MachineServiceContract;
13
use Modules\Machine\Entities\Machine;
14
use Modules\Machine\Events\MachineRegisteredEvent;
15
use Modules\Machine\Events\MachineUpdatedEvent;
16
17
class MachineService implements MachineServiceContract
18
{
19
    public function allByUserId($userId)
20
    {
21
        return Machine::where('user_id', $userId)->get();
22
    }
23
24
    public function find($id): ?Machine
25
    {
26
        if ($id instanceof Machine)
27
            return $id;
28
        return Machine::find($id);
29
    }
30
31
    public function update($id, $data): Machine
32
    {
33
        $machine = $this->find($id);
34
        $machine->update($data);
35
        event(new MachineUpdatedEvent($machine));
36
        return $machine;
37
    }
38
39
    public function create($data): Machine
40
    {
41
        $machine = Machine::create($data);
42
        event(new MachineRegisteredEvent($machine));
43
44
        return $machine;
45
    }
46
47
    public function delete($id): bool
48
    {
49
        return Machine::destroy($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\Machine\E...s\Machine::destroy($id) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
50
    }
51
52
    public function heartbeat($id, $data): void
53
    {
54
        $this->update($id, [
55
            'last_heartbeat' => Carbon::now(),
56
            'memory_usage' => $data['memory_usage'],
57
            'cpu_usage' => $data['cpu_usage']
58
        ]);
59
    }
60
61
62
}
63