Completed
Pull Request — master (#216)
by Arthur
07:36 queued 03:54
created

GoCardlessPaymentController::handleManualReturn()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 7.8235
c 0
b 0
f 0
cc 7
eloc 32
nc 36
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B GoCardlessPaymentController::handleBill() 0 37 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace BB\Http\Controllers;
2
3
use BB\Entities\User;
4
5
class GoCardlessPaymentController extends Controller
6
{
7
    /**
8
     * @var \BB\Repo\PaymentRepository
9
     */
10
    private $paymentRepository;
11
    /**
12
     * @var \BB\Helpers\GoCardlessHelper
13
     */
14
    private $goCardless;
15
16
    function __construct(\BB\Repo\PaymentRepository $paymentRepository, \BB\Helpers\GoCardlessHelper $goCardless)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
    {
18
        $this->paymentRepository = $paymentRepository;
19
20
        $this->middleware('role:member', array('only' => ['create', 'store']));
21
        $this->goCardless = $goCardless;
22
    }
23
24
    /**
25
     * Main entry point for all gocardless payments - not subscriptions
26
     * @param $userId
27
     * @return mixed
28
     * @throws \BB\Exceptions\AuthenticationException
29
     */
30
    public function create($userId)
31
    {
32
        $user = User::findWithPermission($userId);
33
34
        $requestData = \Request::only(['reason', 'amount', 'return_path']);
35
36
        $reason = $requestData['reason'];
37
        $amount = ($requestData['amount'] * 1) / 100;
38
        $returnPath = $requestData['return_path'];
39
        $ref = $this->getReference($reason);
40
41
        if ($user->payment_method == 'gocardless-variable') {
42
43
            return $this->handleBill($amount, $reason, $user, $ref, $returnPath);
44
45
        } elseif ($user->payment_method == 'gocardless') {
46
47
            return $this->ddMigratePrompt($returnPath);
48
49
        } else {
50
51
            abort(500, 'Not supported');
52
53
        }
54
    }
55
56
    private function ddMigratePrompt($returnPath)
57
    {
58
        if (\Request::wantsJson()) {
59
            return \Response::json(['error' => 'Please visit the "Your Membership" page and migrate your Direct Debit first, then return and make the payment'], 400);
60
        }
61
        \Notification::error("Please visit the \"Your Membership\" page and migrate your Direct Debit first, then return and make the payment");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Please visit the \"Your ...rn and make the payment 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...
62
        return \Redirect::to($returnPath);
63
    }
64
65
    /**
66
     * Process a direct debit payment when we have a preauth
67
     *
68
     * @param $amount
69
     * @param $reason
70
     * @param User $user
71
     * @param $ref
72
     * @param $returnPath
73
     * @return mixed
74
     */
75
    private function handleBill($amount, $reason, $user, $ref, $returnPath)
76
    {
77
        if (is_null($ref)) {
78
            $ref = '';
79
        }
80
        $bill = $this->goCardless->newBill($user->subscription_id, $amount * 100, $this->goCardless->getNameFromReason($reason));
81
82
        if ($bill) {
83
            //Store the payment
84
            $fee = 0;
85
            $paymentSourceId = $bill->id;
86
            $amount = $bill->amount / 100;
87
            $status = $bill->status;
88
            if ($status == 'pending_submission') {
89
                $status = 'pending';
90
            }
91
92
            //The record payment process will make the necessary record updates
93
            $this->paymentRepository->recordPayment($reason, $user->id, 'gocardless-variable', $paymentSourceId, $amount, $status, $fee, $ref);
94
95
            if (\Request::wantsJson()) {
96
                return \Response::json(['message' => 'The payment was submitted successfully']);
97
            }
98
99
            \Notification::success("The payment was submitted successfully");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal The payment was submitted successfully 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...
100
        } else {
101
            //something went wrong - we still have the pre auth though
102
103
            if (\Request::wantsJson()) {
104
                return \Response::json(['error' => 'There was a problem charging your account'], 400);
105
            }
106
107
            \Notification::error("There was a problem charging your account");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal There was a problem charging your account 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...
108
        }
109
110
        return \Redirect::to($returnPath);
111
    }
112
113
114
115
    private function getDescription($reason)
116
    {
117
        if ($reason == 'subscription') {
118
            return "Monthly Subscription Fee - Manual";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Monthly Subscription Fee - Manual 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...
119
        } elseif ($reason == 'induction') {
120
            return strtoupper(\Input::get('induction_key')) . " Induction Fee";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Induction Fee 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
        } elseif ($reason == 'door-key') {
122
            return "Door Key Deposit";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Door Key Deposit 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...
123
        } elseif ($reason == 'storage-box') {
124
            return "Storage Box Deposit";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Storage Box Deposit 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...
125
        } elseif ($reason == 'balance') {
126
            return "BB Credit Payment";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BB Credit Payment 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...
127
        } else {
128
            throw new \BB\Exceptions\NotImplementedException();
129
        }
130
    }
131
132
    private function getName($reason, $userId)
133
    {
134
        if ($reason == 'subscription') {
135
            return strtoupper("BBSUB" . $userId . ":MANUAL");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBSUB 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...
Coding Style Comprehensibility introduced by
The string literal :MANUAL 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...
136
        } elseif ($reason == 'induction') {
137
            return strtoupper("BBINDUCTION" . $userId . ":" . \Request::get('induction_key'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBINDUCTION 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...
Coding Style Comprehensibility introduced by
The string literal : 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...
138
        } elseif ($reason == 'door-key') {
139
            return strtoupper("BBDOORKEY" . $userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBDOORKEY 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...
140
        } elseif ($reason == 'storage-box') {
141
            return strtoupper("BBSTORAGEBOX" . $userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBSTORAGEBOX 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...
142
        } elseif ($reason == 'balance') {
143
            return strtoupper("BBBALANCE" . $userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBBALANCE 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...
144
        } else {
145
            throw new \BB\Exceptions\NotImplementedException();
146
        }
147
    }
148
149
    private function getReference($reason)
150
    {
151
        if ($reason == 'induction') {
152
            return \Request::get('ref');
153
        } elseif ($reason == 'balance') {
154
            return \Request::get('reference');
155
        }
156
        return false;
157
    }
158
}
159