Passed
Push — main ( e886d9...331d2d )
by Shubham
02:07
created
src/Api/Shoonya.php 1 patch
Spacing   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -7,10 +7,10 @@  discard block
 block discarded – undo
7 7
 
8 8
 class Shoonya {
9 9
 
10
-    protected $guzzle, $jKey, $userName, $accountId, $pwd, $uid,$exarr,$brkname,$email;
11
-    protected $cred,$logger,$orderNo = [];
10
+    protected $guzzle, $jKey, $userName, $accountId, $pwd, $uid, $exarr, $brkname, $email;
11
+    protected $cred, $logger, $orderNo = [];
12 12
     protected const Delivery = 'C', Intraday = 'I', Normal = 'M', CF = 'M';
13
-    protected const FeedTouchLine = 1,FeedSnapQuoate=2;
13
+    protected const FeedTouchLine = 1, FeedSnapQuoate = 2;
14 14
     protected const PriceMarket = 'MKT', PriceLimit = 'LMT', PrinceSLLmit = 'SL-LMT', PriceSLM = 'SL-MKT';
15 15
     Protected const Buy = 'B', Sell = 'S';
16 16
     
@@ -59,9 +59,9 @@  discard block
 block discarded – undo
59 59
      */
60 60
     public function login():bool {
61 61
         $this->cred['pwd'] = hash('sha256', utf8_encode($this->cred['pwd']));
62
-        $this->cred['appkey'] = hash('sha256', utf8_encode($this->cred['uid'] . '|' . $this->cred['appkey']));
62
+        $this->cred['appkey'] = hash('sha256', utf8_encode($this->cred['uid'].'|'.$this->cred['appkey']));
63 63
         $req = $this->request('login', $this->cred, false);
64
-        if($this->log($req, ['logged in successfully!','falied to loggedin!'])){
64
+        if ($this->log($req, ['logged in successfully!', 'falied to loggedin!'])) {
65 65
             $this->sessionData($req);
66 66
             return true;
67 67
         }
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
      */
75 75
     public function logout() :bool {
76 76
         $req = $this->request('logout', ['ordersource' => 'API', 'uid' => $this->uid]);
77
-        if($this->log($req, ['logout successfully!','failed to logout!'])){
77
+        if ($this->log($req, ['logout successfully!', 'failed to logout!'])) {
78 78
             $this->jKey = null;
79 79
             $this->userName = null;
80 80
             $this->accountId = null;
@@ -98,8 +98,8 @@  discard block
 block discarded – undo
98 98
             'pan' => $pan,
99 99
             'dob' => $dob
100 100
         ];
101
-        $req = $this->request('forgot_password', $values,false);
102
-        return $this->log($req, ['Password Changed Successfully','Could not changed password!']);   
101
+        $req = $this->request('forgot_password', $values, false);
102
+        return $this->log($req, ['Password Changed Successfully', 'Could not changed password!']);   
103 103
     }
104 104
     
105 105
     /**
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
      * @param string $exchange
109 109
      * @return array
110 110
      */
111
-    public function searchScrip(string $searchtext, string $exchange ='BSE'):array {
111
+    public function searchScrip(string $searchtext, string $exchange = 'BSE'):array {
112 112
         $values = [
113 113
             'uid' => $this->uid,
114 114
             'exch' => $exchange,
@@ -124,25 +124,25 @@  discard block
 block discarded – undo
124 124
      * @param string $exch
125 125
      * @return type
126 126
      */
127
-    public function getLimits($prd=null,$seg=null,$exch=null) {
127
+    public function getLimits($prd = null, $seg = null, $exch = null) {
128 128
         $values = [
129 129
             'uid' => $this->uid,
130 130
             'actid' => $this->accountId
131 131
         ];
132 132
         
133
-        if(!is_null($prd)){
133
+        if (!is_null($prd)) {
134 134
             $values['prd'] = $prd;
135 135
         }
136
-        if(!is_null($seg)) {
136
+        if (!is_null($seg)) {
137 137
             $values['seg'] = $seg;
138 138
         }
139
-        if(!is_null($exch)){
139
+        if (!is_null($exch)) {
140 140
             $values['exch'] = $exch;
141 141
         }
142 142
         return $this->request('limits', $values);
143 143
     }
144 144
     
145
-    public function getOptionChain(string $tsym, int $strprc,int $count=5, string $exch='NFO') {
145
+    public function getOptionChain(string $tsym, int $strprc, int $count = 5, string $exch = 'NFO') {
146 146
         $values = [
147 147
             'uid'=> $this->uid,
148 148
             'exch'=>$exch,
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
      * @param string $token
159 159
      * @param string $exch
160 160
      */
161
-    public function getScripInfo(string $token,string $exch='BSE') {
161
+    public function getScripInfo(string $token, string $exch = 'BSE') {
162 162
         $tkNum = parse_ini_file('scrip/bse.ini');
163 163
         $values = [
164 164
             'uid'=> $this->uid,
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
      * @param string $exchange
175 175
      * @return array
176 176
      */
177
-    public function getQuotes(string $token, string $exchange ='BSE' ){
177
+    public function getQuotes(string $token, string $exchange = 'BSE') {
178 178
         $tkNum = parse_ini_file('scrip/bse.ini');
179 179
         $values = [
180 180
             'uid'=> $this->accountId,
@@ -192,13 +192,13 @@  discard block
 block discarded – undo
192 192
      * @param string $interval 1, 3, 5 , 10, 15, 30, 60, 120, 240
193 193
      * @param string $exch
194 194
      */
195
-    public function getTimePriceSeries(string $token,string $startTime = null, string $endTime=null, string $interval='15', string $exch='BSE') {
195
+    public function getTimePriceSeries(string $token, string $startTime = null, string $endTime = null, string $interval = '15', string $exch = 'BSE') {
196 196
         $tkNum = parse_ini_file('scrip/bse.ini');
197
-        if(is_null($startTime)) {
198
-            $startTime = (string)strtotime(date('d-m-Y'));
197
+        if (is_null($startTime)) {
198
+            $startTime = (string) strtotime(date('d-m-Y'));
199 199
         }
200
-        else{
201
-            $startTime = (string)strtotime($startTime);
200
+        else {
201
+            $startTime = (string) strtotime($startTime);
202 202
         }
203 203
         
204 204
         $values = [
@@ -207,10 +207,10 @@  discard block
 block discarded – undo
207 207
             'token'=>$tkNum[$token],
208 208
             'st'=> $startTime
209 209
         ];
210
-        if(!is_null($endTime)) {
211
-            $values['et'] = (string)strtotime($endTime);
210
+        if (!is_null($endTime)) {
211
+            $values['et'] = (string) strtotime($endTime);
212 212
         }
213
-        if(!is_null($interval)) {
213
+        if (!is_null($interval)) {
214 214
             $values['intrv'] = (string) $interval;
215 215
         }
216 216
         return $this->request('TPSeries', $values);
@@ -224,18 +224,18 @@  discard block
 block discarded – undo
224 224
      * @param string $exch
225 225
      * @return type
226 226
      */
227
-    public function getDailyPriceSeries(string $token,string $startDate, string $endDate=null, string $exch='BSE') {
227
+    public function getDailyPriceSeries(string $token, string $startDate, string $endDate = null, string $exch = 'BSE') {
228 228
         $tkNum = parse_ini_file('scrip/bse.ini');
229
-        if(is_null($endDate)) {
229
+        if (is_null($endDate)) {
230 230
             $et = (string) strtotime(date('d-m-Y'));
231
-        }else {
232
-            $et=  (string) strtotime($endDate);
231
+        } else {
232
+            $et = (string) strtotime($endDate);
233 233
         }
234 234
         $st = (string) strtotime($startDate);
235 235
         
236 236
         $values = [
237 237
             'uid'=> $this->accountId,
238
-            'sym'=>" $exch : $tkNum[$token]" ,
238
+            'sym'=>" $exch : $tkNum[$token]",
239 239
             'from'=>$st,
240 240
             'to'=> $et
241 241
         ];
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
      * @return type
258 258
      */
259 259
     public function singleOrderHistory(int $orderNo) {
260
-        return $this->request('singleorderhistory', ['uid'=> $this->uid,'norenordno'=>$orderNo]);
260
+        return $this->request('singleorderhistory', ['uid'=> $this->uid, 'norenordno'=>$orderNo]);
261 261
     }
262 262
 
263 263
     /**
@@ -273,7 +273,7 @@  discard block
 block discarded – undo
273 273
      * @return type
274 274
      */
275 275
     public function getTradebook() {
276
-        return $this->request('tradebook', ['uid' => $this->uid,'actid'=> $this->accountId]);
276
+        return $this->request('tradebook', ['uid' => $this->uid, 'actid'=> $this->accountId]);
277 277
     }
278 278
 
279 279
     /**
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
      * @return array | stdClass
283 283
      */
284 284
     public function getHoldings($productType = self::Delivery) {
285
-        return $this->request('holdings', ['uid' => $this->uid,'actid' => $this->accountId,'prd' => $productType]);
285
+        return $this->request('holdings', ['uid' => $this->uid, 'actid' => $this->accountId, 'prd' => $productType]);
286 286
     }
287 287
     
288 288
     /**
@@ -290,7 +290,7 @@  discard block
 block discarded – undo
290 290
      * @return array | stdClass
291 291
      */
292 292
     public function getPositions() {
293
-        return $this->request('positions',['uid' => $this->uid, 'actid' => $this->accountId]);
293
+        return $this->request('positions', ['uid' => $this->uid, 'actid' => $this->accountId]);
294 294
     }
295 295
 
296 296
     public function placeOrder($buy_or_sell, $productType, $exchange, $tradingSymbol, $quantity, $discloseQty,
@@ -305,11 +305,11 @@  discard block
 block discarded – undo
305 305
             'prd' => $productType,
306 306
             'exch' => $exchange,
307 307
             'tsym' => ($tradingSymbol), //urllib . parse . quote_plus
308
-            'qty' => (string)($quantity),
309
-            'dscqty' => (string)($discloseQty),
308
+            'qty' => (string) ($quantity),
309
+            'dscqty' => (string) ($discloseQty),
310 310
             'prctyp' => $priceType,
311
-            'prc' => (string)($price),
312
-            'trgprc' => (string)($triggerPrice),
311
+            'prc' => (string) ($price),
312
+            'trgprc' => (string) ($triggerPrice),
313 313
             'ret' => $retention,
314 314
             'remarks' => $remarks,
315 315
             'amo' => $amo
@@ -317,24 +317,24 @@  discard block
 block discarded – undo
317 317
 
318 318
         #if cover order or high leverage order
319 319
         if ($productType == 'H') {
320
-            $values["blprc"] = (string)($booklossPrice);
320
+            $values["blprc"] = (string) ($booklossPrice);
321 321
             #trailing price
322 322
             if ($trailPrice != 0.0) {
323
-                $values["trailprc"] = (string)($trailPrice);
323
+                $values["trailprc"] = (string) ($trailPrice);
324 324
             }
325 325
         }
326 326
 
327 327
         #bracket order
328 328
         if ($productType == 'B') {
329
-            $values["blprc"] = (string)($booklossPrice);
330
-            $values["bpprc"] = (string)($bookprofitPrice);
329
+            $values["blprc"] = (string) ($booklossPrice);
330
+            $values["bpprc"] = (string) ($bookprofitPrice);
331 331
             #trailing price
332 332
             if ($trailPrice != 0.0) {
333
-                $values["trailprc"] = (string)($trailPrice);
333
+                $values["trailprc"] = (string) ($trailPrice);
334 334
             }
335 335
         }
336 336
         $req = $this->request('placeorder', $values);
337
-        if($this->log($req, ["order placed, ON:- $req->norenordno", 'failed to place order!'])) {
337
+        if ($this->log($req, ["order placed, ON:- $req->norenordno", 'failed to place order!'])) {
338 338
             $this->orderNo[] = $req->norenordno;
339 339
             return true;
340 340
         }
@@ -342,7 +342,7 @@  discard block
 block discarded – undo
342 342
     }
343 343
     
344 344
     public function getSessionData() {
345
-        return ['jKey' => $this->jKey,'uid'=>$this->uid,'actid'=> $this->accountId, 'uname'=>$this->userName, $this->exarr, $this->brkname];
345
+        return ['jKey' => $this->jKey, 'uid'=>$this->uid, 'actid'=> $this->accountId, 'uname'=>$this->userName, $this->exarr, $this->brkname];
346 346
     }
347 347
     
348 348
     protected function sessionData($data) {
@@ -365,39 +365,39 @@  discard block
 block discarded – undo
365 365
      * @param array $msg
366 366
      * @return boolean
367 367
      */
368
-    protected function log($req,array $msg) {
369
-        if($req->stat == 'Ok') {
370
-            $this->logger->info("User {$this->cred['uid']} " . $msg[0]);
368
+    protected function log($req, array $msg) {
369
+        if ($req->stat == 'Ok') {
370
+            $this->logger->info("User {$this->cred['uid']} ".$msg[0]);
371 371
             return true;
372 372
         }
373
-        $this->logger->notice("User {$this->cred['uid']} " . $msg[1]);
374
-        return false ;
373
+        $this->logger->notice("User {$this->cred['uid']} ".$msg[1]);
374
+        return false;
375 375
     }
376 376
 
377 377
 
378
-    protected function request(string $routes, array $jData,$iskey=true){
379
-        $request = $this->post($this->routes[$routes], $this->jData($jData),$iskey);
378
+    protected function request(string $routes, array $jData, $iskey = true) {
379
+        $request = $this->post($this->routes[$routes], $this->jData($jData), $iskey);
380 380
         $decode = $this->decode($request->getBody());
381 381
         return $decode;
382 382
     }
383 383
 
384 384
     protected function post($routes, $body, $contentType = 'application/json') {
385
-        $url = $this->urls['endpoint'] . $routes;
385
+        $url = $this->urls['endpoint'].$routes;
386 386
         return $this->guzzle->post($url, [
387 387
                     'header' => ['Content-Type' => $contentType],
388 388
                     'body' => $body
389 389
         ]);
390 390
     }
391 391
 
392
-    protected function jData($data,$isKey = true) {
393
-        if($isKey){
394
-            return 'jData=' .json_encode($data) . $this->jKey();
392
+    protected function jData($data, $isKey = true) {
393
+        if ($isKey) {
394
+            return 'jData='.json_encode($data).$this->jKey();
395 395
         }
396
-        return 'jData=' . json_encode($data);
396
+        return 'jData='.json_encode($data);
397 397
     }
398 398
 
399 399
     protected function jKey() {
400
-        return '&jKey=' . $this->jKey;
400
+        return '&jKey='.$this->jKey;
401 401
     }
402 402
 
403 403
 }
Please login to merge, or discard this patch.