|
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); |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function delete($id): bool |
|
52
|
|
|
{ |
|
53
|
|
|
return Account::destroy($id); |
|
|
|
|
|
|
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
|
|
|
|