AddLotMiddleware::eraseSummFromWallet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Lot;
6
use App\Repositories\LotRepository;
7
use Closure;
8
use Illuminate\Contracts\Auth\Guard;
9
10
class AddLotMiddleware
11
{
12
    /**
13
     * @var Guard
14
     */
15
    protected $auth;
16
17
    /**
18
     * @var LotRepository
19
     */
20
    protected $lots;
21
22
    /**
23
     * AddLotMiddleware constructor.
24
     * @param Guard $auth
25
     * @param LotRepository $lotRepository
26
     */
27
    public function __construct(Guard $auth, LotRepository $lotRepository)
28
    {
29
        $this->auth = $auth;
30
        $this->lots = $lotRepository;
31
    }
32
33
    /**
34
     * Handle an incoming request.
35
     *
36
     * @param  \Illuminate\Http\Request  $request
37
     * @param  \Closure  $next
38
     * @param  string|null  $guard
39
     * @return mixed
40
     */
41
    public function handle($request, Closure $next, $guard = null)
0 ignored issues
show
Unused Code introduced by
The parameter $guard 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...
42
    {
43
        if($lot = $request->route('lot'))
44
        {
45
            if($lot->status == Lot::STATUS_DRAFTED)
46
            {
47
                /*$category = $lot->category;*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
48
                if($this->issetAmount($lot, $request->get('comision')))
49
                {
50
                    $lot->status = Lot::STATUS_COMPLETE;
51
                    $lot->save();
52
                    return $next($request);
53
                } else {
54
                    //return redirect()->back()->withErrors('No amount');
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% 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...
55
                    return response('No amount',402);
56
                }
57
            }
58
        }
59
60
        return redirect()->back()->withSuccess('Something Wrong!');
61
    }
62
63
    private function issetAmount($lot, $requestComision)
64
    {
65
        $comision = $this->lots->userLotsPendingComision(\Auth::user(),$lot->id) + $requestComision;
66
        $wallet   = $this->auth->user()->wallet;
0 ignored issues
show
Bug introduced by
Accessing wallet on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
        if ($wallet->amount >= $comision) {
68
            return true;
69
        }
70
        return false;
71
    }
72
73
    private function blockAmount($lot, $tax)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
74
    {
75
        $user = $this->auth->user();
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
        $amount = $lot->yield_amount;
77
        return $this->blockSumm($tax, $amount);
78
    }
79
80 View Code Duplication
    public function blockSumm($tax, $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...
81
    {
82
        $wallet = $this->auth->user()->wallet;
0 ignored issues
show
Bug introduced by
Accessing wallet on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
83
84
        $summ = $this->calcEraseSumm($tax, $amount);
85
86
        $calc = $wallet->amount - $summ;
87
        if($calc > 0)
88
        {
89
            return $wallet->fill([
90
                //'amount'       => $calc,
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
91
                'amount_block' => $summ
92
            ])->save();
93
        }
94
95
        return false;
96
    }
97
98
99
    /**
100
     * @param $lot
101
     * @param $tax
102
     * @return bool
103
     */
104
    private function eraseFromWallet($lot, $tax)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
105
    {
106
        $user = $this->auth->user();
107
        $amount = $lot->yield_amount;
108
109
        if(! $user->isAdmin())
110
        {
111
            return $this->eraseSummFromWallet($tax, $amount);
112
        } else {
113
            $lot->status = Lot::STATUS_COMPLETE;
114
            $lot->save();
115
116
            return true;
117
        }
118
    }
119
    /**
120
     * @param $tax
121
     * @param $amount
122
     * @return bool
123
     */
124 View Code Duplication
    public function eraseSummFromWallet($tax, $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...
125
    {
126
        $wallet = $this->auth->user()->wallet;
0 ignored issues
show
Bug introduced by
Accessing wallet on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
127
128
        $summ = $this->calcEraseSumm($tax, $amount);
129
130
        $calc = $wallet->amount - $summ;
131
        if($calc > 0)
132
        {
133
            return $wallet->fill([
134
                'amount' => $calc
135
            ])->save();
136
        }
137
138
        return false;
139
    }
140
141
    /**
142
     * @param $tax
143
     * @param $amount
144
     * @return float|int
145
     */
146
    public function calcEraseSumm($tax, $amount)
147
    {
148
        return ($amount / 100 * $tax);
149
    }
150
}