Passed
Branch tests (1701da)
by Chris
02:51
created

CP_TDinstance::searchAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Slackbot001;
4
5
use Illuminate\Support\Facades\Log;
6
7
class CP_TDauth
8
{
9
    protected $auth = '';
10
    protected $expires = '';
11
    protected $BEID = '';
12
    protected $WebServicesKey = '';
13
    protected $urlroot = '';
14
    protected $appsroot = '';
15
    protected $appid = '';
16
    protected $authstring = '';
17
    protected $authsig = '';
18
    protected $header = '';
19
20
    public function __construct()
21
    {
22
        $args = func_get_args();
23
        $argcount = func_num_args();
24
        if (method_exists($this, $func = '__construct'.$argcount)) {
25
            call_user_func_array([$this, $func], $args);
26
        }
27
    }
28
29
    public function __construct0()
30
    {
31
        Log::info('CP_TDauth: Constructing empty self.');
32
    }
33
34
    public function __construct5($beid, $wskey, $urlroot, $appid, $env)
35
    {
36
        Log::info('CP_TDauth: Constructing self with new authorization.');
37
        $this->setEnv($env, $urlroot);
38
        $this->authorize($beid, $wskey, $this->urlroot);
39
        $this->BEID = $beid;
40
        $this->WebServicesKey = $wskey;
41
        $this->appid = $appid;
42
    }
43
44
    public function __construct6($beid, $wskey, $urlroot, $appid, $env, $auth)
45
    {
46
        Log::info('CP_TDauth: Constructing self with existing authorization.');
47
        $this->setEnv($env, $urlroot);
48
        $this->BEID = $beid;
49
        $this->WebServicesKey = $wskey;
50
        $this->appid = $appid;
51
52
        $parts = explode('.', $auth);
53 View Code Duplication
        if (count($parts) == 3) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            list($JWTheader, $JWTpayload, $JWTsig) = $parts;
55
            $this->auth = $auth;
56
            $this->expires = json_decode(base64_decode($JWTpayload))->exp;
57
            $this->authstring = 'Authorization: Bearer '.$this->auth;
58
            $this->header = $JWTheader;
59
            $this->authsig = $JWTsig;
60
61
            return;
62
        }
63
        Log::info('CP_TDauth: Invalid token.');
64
    }
65
66
    private function setEnv($env, $urlroot)
67
    {
68
        if ($env == 'prod') {
69
            Log::info('CP_TDauth: Setup for production.');
70
            $this->urlroot = $urlroot.'TDWebApi/api/';
71
            $this->appsroot = $urlroot.'TDNext/Apps/';
72
        } elseif ($env == 'sandbox') {
73
            Log::info('CP_TDauth: Setup for sandbox.');
74
            $this->urlroot = $urlroot.'SBTDWebApi/api/';
75
            $this->appsroot = $urlroot.'SBTDNext/Apps/';
76
        }
77
    }
78
79
    private function authorize($beid, $wskey, $urlroot)
80
    {
81
        Log::info('CP_TDauth: authorize method called.');
82
        Log::info('CP_TDauth: authorize requesting at ['.$urlroot.'auth/loginadmin].');
83
        $ch = curl_init($urlroot.'auth/loginadmin');
84
        $payload = json_encode(['BEID' => $beid, 'WebServicesKey' => $wskey]);
85
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
86
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
87
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
88
        $bearer = curl_exec($ch);
89
        curl_close($ch);
90 View Code Duplication
        if (!$bearer) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
            Log::info('CP_TDauth: Authorization failed.');
92
        } else {
93
            Log::info('CP_TDauth: Authorization successful.');
94
            list($JWTheader, $JWTpayload, $JWTsig) = explode('.', $bearer);
95
            $this->auth = $bearer;
96
            $this->expires = json_decode(base64_decode($JWTpayload))->exp;
97
            $this->authstring = 'Authorization: Bearer '.$this->auth;
98
            $this->header = $JWTheader;
99
            $this->authsig = $JWTsig;
100
        }
101
    }
102
103
    public function checkToken()
104
    {
105
        Log::info('CP_TDauth: checkToken method called.');
106
        if ($this->authstring) {
107
            if (($this->expires - time()) <= 10) {
108
                $this->authorize($this->BEID, $this->WebServicesKey, $this->urlroot);
109
                Log::info('CP_TDauth: Token was expired. Replaced with new token.');
110
            } else {
111
                $remain = $this->expires - time();
112
                Log::info('CP_TDauth: Token ok, time remaining: '.$remain);
113
            }
114
115
            return true;
116
        } else {
117
            Log::info('CP_TDauth: No token.');
118
119
            return false;
120
        }
121
    }
122
123
    public function __toString()
124
    {
125
        Log::info('CP_TDauth: __toString method called.');
126
        $this->checkToken();
127
        Log::info('CP_TDauth: returning JWT.');
128
129
        return $this->auth;
130
    }
131
132
    public function getVersion()
133
    {
134
        Log::info('CP_TDauth: getVersion method called.');
135
136
        return '0.98b';
137
    }
138
}
139
140
class CP_TDinstance extends CP_TDauth
141
{
142
    private function connect($type, $point, $data)
143
    {
144
        Log::info('CP_TDinstance: connect method called.');
145
        $this->checkToken();
146
        $ch = curl_init($this->urlroot.$point);
147
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json', $this->authstring]);
148
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
149
        if ($type == 'post') {
150
            Log::info('CP_TDinstance: connect method POST.');
151
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
152
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
153
        } else {
154
            Log::info('CP_TDinstance: connect method GET.');
155
        }
156
        $result = curl_exec($ch);
157
158
        return json_decode($result, true);
159
    }
160
161
    private function flagCheck($search)
162
    {
163
        Log::info('CP_TDinstance: flagCheck method called.');
164
        $check = substr($search, -3);
165
        if (substr($check, 1, 1) == '-') {
166
            $flag = substr($check, 1, 2);
167
            Log::info('CP_TDinstance: flagCheck given flag '.$flag);
168
169
            return $flag;
170
        } else {
171
            return;
172
        }
173
    }
174
175
    public function ticket($ticketno)
176
    {
177
        Log::info('CP_TDinstance: ticket method called.');
178
        $ticket = $this->connect('get', $this->appid.'/tickets/'.$ticketno, '');
179
180
        return $ticket;
181
    }
182
183
    public function searchTicketsName($search)
184
    {
185
        Log::info('CP_TDinstance: searchTicketsName method called.');
186
        $flag = $this->flagCheck($search);
187
188
        if (!$flag) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $flag of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
189
            $people = $this->searchPeople($search);
190
            foreach ($people as $person) {
191
                $uids[] = $person['UID'];
192
            }
193
            if (isset($uids)) {
194
                $tickets = $this->searchResponsibility($uids);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uids seems to be defined by a foreach iteration on line 190. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
195
196
                return $tickets;
197
            } else {
198
                return;
199
            }
200
        } else {
201
            if ($flag == '-r') {
202
                $data = ['RequestorNameSearch' => substr($search, 0, -3)];
203
                $data_string = json_encode($data);
204
                $tickets = $this->connect('post', $this->appid.'/tickets/search', $data_string);
205
206
                return $tickets;
207
            }
208
        }
209
    }
210
211 View Code Duplication
    public function searchPeople($search)
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...
212
    {
213
        Log::info('CP_TDinstance: searchPeople method called.');
214
        $data = ['SearchText' => $search];
215
        $data_string = json_encode($data);
216
        $people = $this->connect('post', 'people/search', $data_string);
217
218
        return $people;
219
    }
220
221 View Code Duplication
    public function searchAssets($search)
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...
222
    {
223
        Log::info('CP_TDinstance: searchAssets method called.');
224
        $ticketids = [(int) $search];
225
        $data = ['TicketIDs' => $ticketids];
226
        $data_string = json_encode($data);
227
        $assets = $this->connect('post', 'assets/search', $data_string);
228
229
        return $assets;
230
    }
231
232
    public function searchResponsibility($search)
233
    {
234
        Log::info('CP_TDinstance: searchResponsibility method called.');
235
        if (!is_array($search)) {
236
            $search = [(string) $search];
237
        }
238
        $ResponsibilityUids = $search;
239
        $data = ['ResponsibilityUids' => $ResponsibilityUids];
240
        $data_string = json_encode($data);
241
        $tickets = $this->connect('post', '/tickets/search', $data_string);
242
243
        return $tickets;
244
    }
245
246
    public function rootAppsUrl()
247
    {
248
        Log::info('CP_TDinstance: rootAppsUrl method called.');
249
250
        return $this->appsroot.$this->appid.'/';
251
    }
252
}
253