Completed
Push — master ( 505db6...72d5e4 )
by Arthur
05:13
created

BalanceController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
c 1
b 0
f 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 3
1
<?php namespace BB\Http\Controllers;
2
3
use BB\Entities\User;
4
use BB\Exceptions\AuthenticationException;
5
use BB\Exceptions\ValidationException;
6
use BB\Repo\PaymentRepository;
7
use Illuminate\Http\Request;
8
9
class BalanceController extends Controller
10
{
11
12
    /**
13
     * @var \BB\Repo\UserRepository
14
     */
15
    private $userRepository;
16
    /**
17
     * @var \BB\Services\Credit
18
     */
19
    private $bbCredit;
20
    /**
21
     * @var PaymentRepository
22
     */
23
    private $paymentRepository;
24
25
    public function __construct(\BB\Repo\UserRepository $userRepository, \BB\Services\Credit $bbCredit, PaymentRepository $paymentRepository)
26
    {
27
        $this->userRepository = $userRepository;
28
        $this->bbCredit = $bbCredit;
29
        $this->paymentRepository = $paymentRepository;
30
    }
31
32
    public function index($userId)
33
    {
34
        //Verify the user can access this user record
35
        $user = User::findWithPermission($userId);
36
        $this->bbCredit->setUserId($user->id);
37
38
        $userBalance = $this->bbCredit->getBalanceFormatted();
39
40
        $payments = $this->bbCredit->getBalancePaymentsPaginated();
41
42
        $memberList = $this->userRepository->getAllAsDropdown();
43
44
        return \View::make('account.bbcredit.index')
45
            ->with('user', $user)
46
            ->with('payments', $payments)
47
            ->with('userBalance', $userBalance)
48
            ->with('memberList', $memberList);
49
    }
50
51
    /**
52
     * This is a basic method for recording a payment transfer between two people
53
     * This should not exist and the normal balance payment controller should be used
54
     * If any more work is needed here please take the time and move it over!
55
     *
56
     * @param Request $request
57
     * @param integer $userId
58
     *
59
     * @return mixed
60
     * @throws ValidationException
61
     * @throws AuthenticationException
62
     */
63
    public function recordTransfer(Request $request, $userId)
64
    {
65
        $user = User::findWithPermission($userId);
66
        $this->bbCredit->setUserId($user->id);
67
68
        $amount       = $request->get('amount');
69
        $targetUserId = $request->get('target_user_id');
70
        $targetUser   = $this->userRepository->getById($targetUserId);
71
72
        if ($targetUserId === $userId) {
73
            throw new ValidationException('Your\'e trying to send money to yourself, no!');
74
        }
75
76
        //What is the users balance
77
        $userBalance = $this->bbCredit->getBalance();
78
79
        //With this payment will the users balance go to low?
80
        if (($userBalance - $amount) < 0) {
81
82
            \Notification::error("You don't have the money for this");
83
            return \Redirect::route('account.balance.index', $user->id);
0 ignored issues
show
Bug introduced by
The method route() does not seem to exist on object<redirect>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
        }
85
86
        $this->paymentRepository->recordBalanceTransfer($user->id, $targetUser->id, $amount);
87
88
        \Notification::success("Transfer made");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Transfer made does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
89
        return \Redirect::route('account.balance.index', $user->id);
0 ignored issues
show
Bug introduced by
The method route() does not seem to exist on object<redirect>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
    }
91
92
}