Completed
Push — master ( 20f323...6ae55b )
by Jan
05:32
created

FrontendController::_saveTransation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 6
1
<?php
2
/**
3
 * Frontend module controller
4
 * 
5
 * Basic parent controller for frontend functions, controller is not ready yet
6
 * 
7
 * @category Controller
8
 * @subpackage Frontend
9
 * @package Olapus
10
 * @author Jan Drda <[email protected]>
11
 * @copyright Jan Drda
12
 * @license https://opensource.org/licenses/MIT MIT
13
 */
14
15
namespace App\Http\Controllers\Frontend;
16
17
18
use App\Http\Controllers\Controller;
19
use App\Settings;
20
use Illuminate\Support\Facades\View;
21
use App\Page;
22
use App\Transaction;
23
use App\User;
24
25
26
27
class FrontendController extends Controller
28
{
29
    /**
30
     * Data for view
31
     *
32
     * @var array
33
     */
34
    protected $_arViewData = [];
35
36
    protected $_arViewGeneratedData = [];
37
38
    /**
39
     * Check if view exists, if so - return, if not throw 404
40
     *
41
     * @param string $strView
42
     * @return View|void
43
     */
44
    protected function _showViewOr404($strView){
45
46
47
        if (view()->exists($strView)) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
            return view($strView, $this->_arViewData);
49
        }
50
        else{
51
            return abort(404);
52
        }
53
    }
54
55
    /**
56
     * Save all inputs to session
57
     * @param $request
58
     */
59
    protected function _saveSessionFields($request){
60
61
        foreach ($request->all() as $key=>$value){
62
            $arData[$key] = $value;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
63
        }
64
65
        $request->session()->put('frontend_fields', $arData);
0 ignored issues
show
Bug introduced by
The variable $arData does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
66
    }
67
68
    /**
69
     * Reset all session fields
70
     */
71
    protected function _resetSessionFields($request){
72
73
        $request->session()->forget('frontend_fields');
74
    }
75
76
    /**
77
     * Calculate price
78
     *
79
     * @param $request
80
     */
81
    protected function _calculatePrice($request){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
83
        return 0;
84
    }
85
86
    /**
87
     * Get frontend fields
88
     *
89
     * @param $request
90
     * @return mixed
91
     */
92
    protected function _getSessionFields($request){
93
94
        $fields = $request->session()->get('frontend_fields');
95
96
        if(isset($request->id)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
97
            //$fields['price'] = $this->_calculatePrice($request);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
        }
99
100
        if(isset($fields['city'])) {
101
            $arCityInfo = explode('|', $fields['city']);
102
103
            $fields['citypart'] = @trim($arCityInfo[0]);
104
            $fields['citymain'] = @trim($arCityInfo[1]);
105
            $fields['county'] = @trim($arCityInfo[2]);
106
            $fields['zip'] = @trim($arCityInfo[3]);
107
        }
108
109
        $this->_arViewData['fields'] = $fields;
110
    }
111
112
    /**
113
     * Load page by URL
114
     *
115
     * @param $url
116
     * @return mixed
117
     */
118
    protected function _loadPageByUrl($url){
119
120
        return Page::where('url', $url)->firstOrFail();
121
    }
122
123
    /**
124
     * Load page by id
125
     *
126
     * @param $id
127
     * @return mixed
128
     */
129
    protected function _loadPageById($id){
130
        return Page::findOrFail($id);
131
    }
132
133
    protected function _getSettings($id){
134
135
        $settings = Settings::find($id);
136
137
        if(!empty($settings)){
138
            return $settings['value'];
139
        }
140
        else{
141
            return NULL;
142
        }
143
    }
144
145
    /**
146
     * Save transaction
147
     *
148
     * @param int $typeId
0 ignored issues
show
Bug introduced by
There is no parameter named $typeId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
149
     * @param string $text
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
150
     * @param $userId
151
     * @param $amount
152
     */
153 View Code Duplication
    protected function _saveTransation($status_id = 1, $user_id, $amount,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
                                       $campaign_id = null, $payment_id = null, $recommendation_id = null){
155
156
        /**
157
         * Save transaction
158
         */
159
        $transaction = new Transaction();
160
        $transaction->transactionstatus_id = $status_id;
161
        $transaction->user_id = $user_id;
162
        $transaction->amount = $amount;
163
        $transaction->campaign_id = $campaign_id;
164
        $transaction->payment_id = $payment_id;
165
        $transaction->recommendation_id = $recommendation_id;
166
        $transaction->save();
167
168
        /**
169
         * Update user wallet
170
         */
171
        $user = User::find($user_id);
172
        $user->wallet = $user->wallet + $amount;
173
        $user->save();
174
    }
175
}
176