ShopShipmentController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 90
c 1
b 0
f 0
dl 0
loc 107
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A page() 0 41 2
A goToCheckout() 0 61 2
1
<?php
2
namespace Mongi\Mongicommerce\Http\Controllers\shop;
3
4
use Illuminate\Http\Request;
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\Facades\Hash;
7
use Mongi\Mongicommerce\Models\Cart;
8
use Mongi\Mongicommerce\Models\User;
9
use Mongi\Mongicommerce\Models\ProductItem;
10
use Mongi\Mongicommerce\Http\Controllers\Controller;
11
use Mongi\Mongicommerce\Libraries\Template;
12
13
class ShopShipmentController extends Controller
14
{
15
    public function page(){
16
        //if logged in
17
        $piva = '';
18
        $rag_soc = '';
19
        $first_name = '';
20
        $last_name = '';
21
        $address = '';
22
        $telephone = '';
23
        $email_sped = '';
24
        $floor = '';
25
        $province = '';
26
        $city = '';
27
        $cap = '';
28
        if(Auth::check()){
29
                $user = Auth::user();
30
                $piva = $user->piva;
0 ignored issues
show
Bug introduced by
Accessing piva on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
                $rag_soc = $user->company;
0 ignored issues
show
Bug introduced by
Accessing company on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
32
                $first_name = $user->first_name;
0 ignored issues
show
Bug introduced by
Accessing first_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
                $last_name = $user->last_name;
0 ignored issues
show
Bug introduced by
Accessing last_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
34
                $address = $user->address;
0 ignored issues
show
Bug introduced by
Accessing address on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
35
                $telephone = $user->telephone;
0 ignored issues
show
Bug introduced by
Accessing telephone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
36
                $email_sped = $user->email;
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
37
                $floor = $user->floor;
0 ignored issues
show
Bug introduced by
Accessing floor on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
38
                $province = $user->province;
0 ignored issues
show
Bug introduced by
Accessing province on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
39
                $city = $user->city;
0 ignored issues
show
Bug introduced by
Accessing city on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
40
                $cap = $user->cap;
0 ignored issues
show
Bug introduced by
Accessing cap on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
41
                Template::MoveSessionToCart($user->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
42
        }
43
44
        return view('mongicommerce.pages.shipment',compact(
45
            'piva',
46
            'rag_soc',
47
            'first_name',
48
            'last_name',
49
            'address',
50
            'telephone',
51
            'email_sped',
52
            'floor',
53
            'province',
54
            'city',
55
            'cap'
56
        ));
57
    }
58
59
    public function goToCheckout(Request $r)
60
    {
61
        $r->validate([
62
            'first_name' => 'required',
63
            'last_name' => 'required',
64
            'address' => 'required',
65
            'telephone' => 'required',
66
            'email_sped' => 'required',
67
            'floor' => 'required',
68
            'province' => 'required',
69
            'city' => 'required',
70
            'cap' => 'required',
71
        ]);
72
73
74
        $piva = $r->get('piva');
75
        $rag_soc = $r->get('rag_soc');
76
        $first_name = $r->get('first_name');
77
        $last_name = $r->get('last_name');
78
        $address = $r->get('address');
79
        $telephone = $r->get('telephone');
80
        $email_sped = $r->get('email_sped');
81
        $floor = $r->get('floor');
82
        $province = $r->get('province');
83
        $city = $r->get('city');
84
        $cap = $r->get('cap');
85
86
        $user = User::where('email',$email_sped)->first();
87
        //if user exist
88
        if($user){
89
                $user->piva = $piva;
90
                $user->company = $rag_soc;
91
                $user->first_name = $first_name;
92
                $user->last_name = $last_name;
93
                $user->address = $address;
94
                $user->telephone = $telephone;
95
                $user->floor = $floor;
96
                $user->province = $province;
97
                $user->city = $city;
98
                $user->cap = $cap;
99
                $user->save();
100
        //if user doesn't exist
101
        }else{
102
            $user = new User();
103
            $user->piva = $piva;
104
            $user->company = $rag_soc;
105
            $user->first_name = $first_name;
106
            $user->last_name = $last_name;
107
            $user->email = $email_sped;
108
            $user->password = Hash::make('ciaociao');
109
            $user->address = $address;
110
            $user->telephone = $telephone;
111
            $user->floor = $floor;
112
            $user->province = $province;
113
            $user->city = $city;
114
            $user->cap = $cap;
115
            $user->save();
116
            Auth::login($user);
117
        }
118
        Template::MoveSessionToCart($user->id);
119
        return response()->json(['link' => route('shop.checkout')]);
120
121
    }
122
}
123