Passed
Push — master ( b1742c...4b0d05 )
by Arthur
101:17 queued 94:42
created

AccountService::heartbeat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
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\Account\Services;
10
11
use Carbon\Carbon;
12
use Modules\Account\Contracts\AccountServiceContract;
13
use Modules\Account\Entities\Account;
14
use Modules\Account\Events\AccountCreatedEvent;
15
use Modules\Account\Events\AccountUpdatedEvent;
16
use Modules\Machine\Entities\Machine;
17
18
class AccountService implements AccountServiceContract
19
{
20
    public function getByUserId($userId)
21
    {
22
        return Account::where('user_id', $userId)->get();
23
    }
24
25
    public function find($id): ?Account
26
    {
27
        if ($id instanceof Account) {
28
            return $id;
29
        }
30
31
        return Account::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\Account\Entities\Account::find($id) could return the type Illuminate\Database\Eloq...go\Abstracts\MongoModel which includes types incompatible with the type-hinted return Modules\Account\Entities\Account|null. Consider adding an additional type-check to rule them out.
Loading history...
32
    }
33
34
    public function update($id, $data): Account
35
    {
36
        $Account = $this->find($id);
37
        $Account->update($data);
38
        event(new AccountUpdatedEvent($Account));
39
40
        return $Account;
41
    }
42
43
    public function create($data): Account
44
    {
45
        $Account = Account::create($data);
46
        event(new AccountCreatedEvent($Account));
47
48
        return $Account;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $Account returns the type Illuminate\Database\Eloq...go\Abstracts\MongoModel which includes types incompatible with the type-hinted return Modules\Account\Entities\Account.
Loading history...
49
    }
50
51
    public function delete($id): bool
52
    {
53
        return Account::destroy($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\Account\E...s\Account::destroy($id) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
54
    }
55
56
    public function heartbeat($id, $data): void
57
    {
58
        $this->update($id, [
59
            'last_heartbeat' => Carbon::now(),
60
            'online'         => true,
61
        ]);
62
    }
63
64
    public function assignToMachine($id, ?Machine $machine)
65
    {
66
        return $this->update($id, ['machine_id' => $machine->id ?? null]);
67
    }
68
69
    public function unlinkFromMachine($id)
70
    {
71
        return $this->assignToMachine($id, null);
72
    }
73
}
74