Passed
Push — master ( d5fac0...e5b08c )
by Arthur
14:05
created

MachineService::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 5
    public function getByUserId($userId)
20
    {
21 5
        return Machine::where('user_id', $userId)->get();
22
    }
23
24 8
    public function find($id): ?Machine
25
    {
26 8
        if ($id instanceof Machine) {
27
            return $id;
28
        }
29
30 8
        return Machine::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\Machine\Entities\Machine::find($id) could return the type Illuminate\Database\Eloq...go\Abstracts\MongoModel which includes types incompatible with the type-hinted return Modules\Machine\Entities\Machine|null. Consider adding an additional type-check to rule them out.
Loading history...
31
    }
32
33 1
    public function update($id, $data): Machine
34
    {
35 1
        $machine = $this->find($id);
36 1
        $machine->update($data);
37 1
        event(new MachineUpdatedEvent($machine));
38
39 1
        return $machine;
40
    }
41
42 1
    public function create($data): Machine
43
    {
44 1
        $machine = Machine::create($data);
45 1
        event(new MachineRegisteredEvent($machine));
46
47 1
        return $machine;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $machine returns the type Illuminate\Database\Eloq...go\Abstracts\MongoModel which includes types incompatible with the type-hinted return Modules\Machine\Entities\Machine.
Loading history...
48
    }
49
50 1
    public function delete($id): bool
51
    {
52 1
        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...
53
    }
54
55
    public function heartbeat($id, $data): void
56
    {
57
        $this->update($id, [
58
            'last_heartbeat' => Carbon::now(),
59
            'memory_usage'   => $data['memory_usage'],
60
            'cpu_usage'      => $data['cpu_usage'],
61
            'online'         => true,
62
        ]);
63
    }
64
}
65