Passed
Push — master ( fe6e69...4f1c4f )
by Mohamed
09:34
created

UserOrgs::updateUserOrg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 16
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 22
rs 9.7333
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Str;
7
8
use Illuminate\Http\Request;
9
use function GuzzleHttp\Psr7\stream_for;
10
11
class UserOrgs extends Controller
12
{
13
14
    public function checkOrg(Request $request)
15
    {
16
        $userLogin = $request->user()->email;
17
        $header = [
18
            'Accept' => 'application/json',
19
            'Content-Type' => 'application/json',
20
        ];
21
22
        $client = new Client([
23
            'base_uri' => env('GRAFANA_URL'),
24
            'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
25
        ]);
26
        try {
27
            $response = $client->request('GET', "/api/users/lookup?loginOrEmail={$userLogin}", [
28
                'headers' =>  $header
29
            ])->getBody();
30
            $data = json_decode($response, true);
31
        } catch (\Throwable $th) {
32
            $data = [];
33
        }
34
        return $data;
35
    }
36
37
    public function updateUserOrg(Request $request, $data)
38
    {
39
        $header = [
40
            'Accept' => 'application/json',
41
            'Content-Type' => 'application/json',
42
        ];
43
        $client = new Client([
44
            'base_uri' => env('GRAFANA_URL'),
45
            'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
46
        ]);
47
        try {
48
            $client->request('POST', "/api/orgs/{$data['orgId']}/users", [
49
                'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
50
                'headers' =>  $header,
51
                'json' => [
52
                    'role' => 'Viewer',
53
                    'loginOrEmail' => $request->user()->username
54
                ]
55
            ]);
56
        } catch (\Throwable $th) {
57
            $this->createGrafanaUser($request);
58
            $this->updateUserOrg($request, $data);
59
        }
60
    }
61
62
    public function createGrafanaUser(Request $request)
63
    {
64
        try {
65
            $header = [
66
                'Accept' => 'application/json',
67
                'Content-Type' => 'application/json',
68
            ];
69
            $client = new Client([
70
                'base_uri' => env('GRAFANA_URL'),
71
                'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
72
            ]);
73
            $response = $client->request('POST', '/api/admin/users', [
74
                'headers' =>  $header,
75
                'json' => [
76
                    'name' => $request->user()->last_name,
77
                    'login' => $request->user()->username,
78
                    'email' => $request->user()->email,
79
                    'password' =>  Str::random(8),
80
                ]
81
            ])->getBody();
82
            $data = json_decode($response, true);
83
            $this->removeUserMainOrg($data);
84
        } catch (\Throwable $th) {
85
            //throw $th;
86
        }
87
    }
88
89
    public function removeUserMainOrg($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
    public function removeUserMainOrg(/** @scrutinizer ignore-unused */ $data)

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

Loading history...
90
    {
91
        $header = [
92
            'Accept' => 'application/json',
93
            'Content-Type' => 'application/json',
94
        ];
95
        $client = new Client([
96
            'base_uri' => env('GRAFANA_URL'),
97
            'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
98
        ]);
99
        $client->request('DELETE', "/api/orgs/1", [
100
            'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
101
            'headers' =>  $header
102
        ]);
103
    }
104
105
    public function getUserOrg(Request $request)
106
    {
107
        if ($request->user() &&  (!($request->user()->principal->isEmpty()))  && !is_null($request->user()->principal) && ($request->user()->principal[0]->roles->code == 'PRINCIPAL')) {
108
            $data = $this->checkOrg($request);
109
            if (empty($data) || ( (!empty($data['orgId']) && $data['orgId'] !== 2))) {
110
                $request['data'] = $data;
111
                $data['user'] = $data;
112
                $data['orgId'] = 2;
113
                $this->updateUserOrg($request, $data);
114
            }
115
            $data = [
116
                [
117
                    "orgId" => 2,
118
                    "userId" => $request->user()->id,
119
                    "role" => 'Viewer',
120
                    "name" => 'schools',
121
                    "email" => $request->user()->username,
122
                    "login" => 'schools',
123
                ]
124
            ];
125
        } elseif ($request->user()  &&  (!($request->user()->zonal_cordinator->isEmpty())) && !is_null($request->user()->zonal_cordinator) && ($request->user()->zonal_cordinator[0]->roles->code == 'ZONAL_COORDINATOR')) {
126
            $data = $this->checkOrg($request);
127
            if (empty($data) || ((!empty($data['orgId']) && $data['orgId'] !== 3))) {
128
                $request['data'] = $data;
129
                $data['user'] = $data;
130
                $data['orgId'] = 3;
131
                $this->updateUserOrg($request, $data);
132
            }
133
            $data = [
134
                [
135
                    "orgId" => 3,
136
                    "userId" => $request->user()->id,
137
                    "role" => 'Viewer',
138
                    "name" => 'zone',
139
                    "email" => $request->user()->username,
140
                    "login" => 'zone',
141
                ]
142
            ];
143
        } elseif ($request->user() &&  (!($request->user()->provincial_cordinator->isEmpty())) && !is_null($request->user()->provincial_cordinator) && ($request->user()->provincial_cordinator[0]->roles->code == 'PROVINCIAL_COORDINATOR')) {
144
            $data = $this->checkOrg($request);
145
            if (empty($data)  || ((!empty($data['orgId']) && $data['orgId'] !== 4))) {
146
                $request['data'] = $data;
147
                $data['user'] = $data;
148
                $data['orgId'] = 4;
149
                $this->updateUserOrg($request, $data);
150
            }
151
            $data = [
152
                [
153
                    "orgId" => 4,
154
                    "userId" => $request->user()->id,
155
                    "role" => 'Viewer',
156
                    "name" => 'province',
157
                    "email" => $request->user()->username,
158
                    "login" => 'province',
159
                ]
160
            ];
161
        }
162
        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...
163
    }
164
}
165