LoginController::userOrg()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 64
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 42
c 1
b 0
f 1
nc 7
nop 1
dl 0
loc 64
rs 8.0035

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
2
3
namespace App\Http\Controllers\Auth;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use GuzzleHttp\Client;
8
use Illuminate\Foundation\Auth\AuthenticatesUsers;
9
10
class LoginController extends Controller
11
{
12
    /*
13
    |--------------------------------------------------------------------------
14
    | Login Controller
15
    |--------------------------------------------------------------------------
16
    |
17
    | This controller handles authenticating users for the application and
18
    | redirecting them to your home screen. The controller uses a trait
19
    | to conveniently provide its functionality to your applications.
20
    |
21
    */
22
23
24
25
    use AuthenticatesUsers;
0 ignored issues
show
introduced by
The trait Illuminate\Foundation\Auth\AuthenticatesUsers requires some properties which are not provided by App\Http\Controllers\Auth\LoginController: $maxAttempts, $decayMinutes
Loading history...
26
27
    /**
28
     * Where to redirect users after login.
29
     *
30
     * @var string
31
     */
32
    protected $redirectTo = '/';
33
34
    /**
35
     * @var string
36
     */
37
    protected $username = 'username';
38
39
40
    /**
41
     * Create a new controller instance.
42
     *
43
     * @return void
44
     */
45
    public function __construct()
46
    {
47
        $this->middleware('guest')->except('logout');
48
        $this->username = $this->findUsername();
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function findUsername()
55
    {
56
        $login = request()->input('username');
57
58
        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
59
60
        request()->merge([$fieldType => $login]);
61
62
        return $fieldType;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function username()
69
    {
70
        return $this->username;
71
    }
72
73
    public function userOrg(Request $request)
74
    {
75
        $client = new Client(['base_uri' => env('GRAFANA_URL').'/api/']);
76
        if ($request->user() && (!($request->user()->principal->isEmpty()))  && $request->user()->principal[0]->roles) {
77
            dd($request->user()->principal[0]->roles->code);
78
            switch ($request->user()->principal[0]->roles->code) {
79
                case 'PRINCIPAL':
80
                    $response = $client->request('post','orgs/2/users',[
81
                        'headers' => [
82
                            'Authorization' => 'Bearer '.env('GRAFANA_KEY'),
83
                            'Accept' => 'application/json',
84
                            'Content-Type' => 'application/json'
85
                        ],
86
                        'body' => [
87
                            'role' => 'Viewer',
88
                            'loginOrEmail' => $request->user()->email
89
                        ]
90
                    ]);
91
                    dd($response);
92
                    if($response){
93
                        $data =
94
                        [
95
                            [
96
                                "orgId" => 2,
97
                                "userId" => $request->user()->id,
98
                                "role" => 'Viewer',
99
                                "name" => 'Schools',
100
                                "email" => $request->user()->email,
101
                                "login" => 'Schools',
102
                            ]
103
                        ];
104
                    }
105
                    break;
106
                case 'PROVINCIAL_COORDINATOR':
107
                    $data =
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
108
                        [
109
                            [
110
                                "orgId" => 3,
111
                                "userId" => $request->user()->user()->id,
112
                                "role" => 'Viewer',
113
                                "name" => 'province',
114
                                "email" => $request->user()->user()->email,
115
                                "login" => 'province',
116
                            ]
117
                        ];
118
                case 'ZONAL_COORDINATOR':
119
                    $data =
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
120
                        [
121
                            [
122
                                "orgId" => 3,
123
                                "userId" => $request->user()->user()->id,
124
                                "role" => 'Viewer',
125
                                "name" => 'zone',
126
                                "email" => $request->user()->user()->email,
127
                                "login" => 'zone',
128
                            ]
129
                        ];
130
                default:
131
                    # code...
132
                    break;
133
            }
134
135
        }
136
        return $data;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
137
    }
138
}
139