-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransferController.php
More file actions
60 lines (50 loc) · 1.43 KB
/
TransferController.php
File metadata and controls
60 lines (50 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace App\Http\Controllers;
use App\Goal;
use App\Wallet;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class TransferController extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return JsonResponse
*/
public function store(Request $request)
{
// todo need more refactoring
$from_id = $request->from_id;
$to_id = $request->to_id;
$from = ($request->from_type == 'wallet') ?
Wallet::find($from_id) :
Goal::find($from_id);
$to = ($request->to_type == 'wallet') ?
Wallet::find($to_id) :
Goal::find($to_id);
[$to_amount, $from_amount] = $this->singleToManyCurrency($request);
$from->transfer($to, $from_amount, $to_amount);
return response()->json([
'new_from_amount' => $from->balance(),
'new_to_amount' => $to->balance(),
]);
}
/**
* @param Request $request
* @return array
*
* @deprecated
*/
private function singleToManyCurrency(Request $request): array
{
if ($request->amount) {
$from_amount = $to_amount = $request->amount;
} else {
$from_amount = $request->from_amount;
$to_amount = $request->to_amount;
}
return [$to_amount, $from_amount];
}
}