Skip to content

Commit 32787a4

Browse files
authored
Laravel integration if function exists.
1 parent 6c81fe8 commit 32787a4

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

src/Client.php

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
<?php
22

33
namespace Parasut;
4+
45
use Exception;
56

67
class Client
78
{
8-
public $BASE_URL = 'https://api.parasut.com' ;
9-
public $version = "v4";
9+
public $BASE_URL = 'https://api.parasut.com';
10+
public $version = 'v4';
1011
public $config;
1112
public $access_token;
1213
public $company_id;
14+
public $file = 'token.ini';
1315

1416
public function __construct($config)
1517
{
1618
$this->config = $config;
1719
$this->company_id = $this->config['company_id'];
1820
$this->checkTokens();
21+
if (function_exists('storage_path')) {
22+
$this->file = storage_path('app/parasut/token.ini');
23+
}
1924
}
2025

2126
public function checkTokens()
2227
{
23-
$tokens = parse_ini_file('token.ini');
24-
if (!isset($tokens['access_token']) || !isset($tokens['created_at'])) {
28+
try {
29+
$tokens = parse_ini_file($this->file);
30+
} catch (\Exception $e) {
31+
unlink($this->file);
32+
}
33+
34+
if (! isset($tokens['access_token']) || ! isset($tokens['created_at'])) {
2535
return $this->authorize();
2636
}
27-
if (time() - (int)$tokens['created_at'] > 7200) {
37+
if (time() - (int) $tokens['created_at'] > 7200) {
2838
return $this->authorize();
2939
}
3040
$this->access_token = $tokens['access_token'];
41+
3142
return $tokens;
3243
}
3344

@@ -37,28 +48,17 @@ public function authorize()
3748
$resp = $this->authWithPassword();
3849
}
3950

40-
if (isset($resp["access_token"])) {
41-
$file = 'token.ini';
42-
$token = "";
51+
if (isset($resp['access_token'])) {
52+
$token = '';
4353
foreach ($resp as $key => $value) {
44-
$token .= $key."=".$value."\n";
54+
$token .= $key.'='.$value."\n";
4555
}
46-
file_put_contents($file, $token);
56+
file_put_contents($this->file, $token);
4757

4858
$this->access_token = $resp['access_token'];
4959
}
50-
return false;
51-
}
5260

53-
private function authWithPassword()
54-
{
55-
$path = $this->BASE_URL."/oauth/token";
56-
return $this->request(
57-
$path,
58-
$this->config,
59-
'POST',
60-
true
61-
);
61+
return false;
6262
}
6363

6464
public function call($class)
@@ -68,19 +68,20 @@ public function call($class)
6868

6969
public function request($path, $params = null, $method = 'POST', $fullPath = false)
7070
{
71-
$headers = [];
71+
$headers = [];
7272
$headers[] = 'Accept: application/json';
7373
$headers[] = 'Content-Type: application/json';
74-
$headers[] = 'Authorization: Bearer ' . $this->access_token;
74+
$headers[] = 'Authorization: Bearer '.$this->access_token;
7575

7676
$ch = curl_init();
77-
if (is_array($params) && $method == "GET" && count($params) > 0) {
78-
$path .= '?' . http_build_query($params);
77+
if (is_array($params) && $method == 'GET' && count($params) > 0) {
78+
$path .= '?'.http_build_query($params);
7979
}
80+
8081
if ($fullPath) {
8182
curl_setopt($ch, CURLOPT_URL, $path);
8283
} else {
83-
curl_setopt($ch, CURLOPT_URL, $this->BASE_URL."/".$this->version."/".$this->company_id."/".$path);
84+
curl_setopt($ch, CURLOPT_URL, $this->BASE_URL.'/'.$this->version.'/'.$this->company_id.'/'.$path);
8485
}
8586

8687
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
@@ -91,7 +92,7 @@ public function request($path, $params = null, $method = 'POST', $fullPath = fal
9192
curl_setopt($ch, CURLOPT_HEADER, false);
9293
switch ($method) {
9394
case 'PUT':
94-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
95+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
9596
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
9697
break;
9798
case 'POST':
@@ -133,4 +134,16 @@ public function request($path, $params = null, $method = 'POST', $fullPath = fal
133134
break;
134135
}
135136
}
137+
138+
private function authWithPassword()
139+
{
140+
$path = $this->BASE_URL.'/oauth/token';
141+
142+
return $this->request(
143+
$path,
144+
$this->config,
145+
'POST',
146+
true
147+
);
148+
}
136149
}

0 commit comments

Comments
 (0)