Completed
Push — master ( b8bfab...12d0ed )
by Arthur
05:27
created

BalanceController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
c 3
b 1
f 0
lcom 2
cbo 9
dl 0
loc 116
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A index() 0 21 1
A withdrawal() 0 23 1
B recordTransfer() 0 28 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
     * @var \BB\Validators\WithdrawalValidator
26
     */
27
    private $withdrawalValidator;
28
29
    public function __construct(\BB\Repo\UserRepository $userRepository, \BB\Services\Credit $bbCredit, PaymentRepository $paymentRepository, \BB\Validators\WithdrawalValidator $withdrawalValidator)
30
    {
31
        $this->userRepository = $userRepository;
32
        $this->bbCredit = $bbCredit;
33
        $this->paymentRepository = $paymentRepository;
34
        $this->withdrawalValidator = $withdrawalValidator;
35
    }
36
37
    public function index($userId)
38
    {
39
        //Verify the user can access this user record
40
        $user = User::findWithPermission($userId);
41
        $this->bbCredit->setUserId($user->id);
42
43
        $userBalance = $this->bbCredit->getBalanceFormatted();
44
        $userBalanceSign = $this->bbCredit->getBalanceSign();
45
46
        $payments = $this->bbCredit->getBalancePaymentsPaginated();
47
48
        $memberList = $this->userRepository->getAllAsDropdown();
49
50
        return \View::make('account.bbcredit.index')
51
            ->with('user', $user)
52
            ->with('payments', $payments)
53
            ->with('userBalance', $userBalance)
54
            ->with('userBalanceSign', $userBalanceSign)
55
            ->with('rawBalance', number_format($user->cash_balance / 100, 2))
0 ignored issues
show
Documentation introduced by
The property cash_balance does not exist on object<BB\Entities\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
            ->with('memberList', $memberList);
57
    }
58
59
    public function withdrawal(Request $request, $userId)
60
    {
61
        $this->withdrawalValidator->validate(\Request::only(['amount', 'sort_code', 'account_number']));
62
63
        $user          = User::findWithPermission($userId);
64
        $amount        = $request->get('amount');
65
        $sortCode      = $request->get('sort_code');
66
        $accountNumber = $request->get('account_number');
67
68
        $memberName = $user->name;
69
        \Mail::queue('emails.withdrawal', [
70
            'memberName'    => $memberName,
71
            'amount'        => $amount,
72
            'sortCode'      => $sortCode,
73
            'accountNumber' => $accountNumber
74
        ], function ($message) {
75
            $message->to('[email protected]', 'Arthur Guy')->subject('User requested a withdrawal');
76
        });
77
78
        \Notification::success("Request sent");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Request sent 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...
79
        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...
80
81
    }
82
83
    /**
84
     * This is a basic method for recording a payment transfer between two people
85
     * This should not exist and the normal balance payment controller should be used
86
     * If any more work is needed here please take the time and move it over!
87
     *
88
     * @param Request $request
89
     * @param integer $userId
90
     *
91
     * @return mixed
92
     * @throws ValidationException
93
     * @throws AuthenticationException
94
     */
95
    public function recordTransfer(Request $request, $userId)
96
    {
97
        $user = User::findWithPermission($userId);
98
        $this->bbCredit->setUserId($user->id);
99
100
        $amount       = $request->get('amount');
101
        $targetUserId = $request->get('target_user_id');
102
        $targetUser   = $this->userRepository->getById($targetUserId);
103
104
        if ($targetUserId === $userId) {
105
            throw new ValidationException('Your\'e trying to send money to yourself, no!');
106
        }
107
108
        //What is the users balance
109
        $userBalance = $this->bbCredit->getBalance();
110
111
        //With this payment will the users balance go to low?
112
        if (($userBalance - $amount) < 0) {
113
114
            \Notification::error("You don't have the money for this");
115
            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...
116
        }
117
118
        $this->paymentRepository->recordBalanceTransfer($user->id, $targetUser->id, $amount);
119
120
        \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...
121
        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...
122
    }
123
124
}