Skip to content

Commit c2fbdcc

Browse files
author
Eren AKPINAR
committed
Initial Commit
1 parent fb485ef commit c2fbdcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+9427
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

Api/Client.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Parasut\Api;
4+
5+
use Parasut\Options;
6+
use Parasut\ParasutException;
7+
8+
class Client
9+
{
10+
/**
11+
* @return resource
12+
*/
13+
private function getCh()
14+
{
15+
return curl_init(Options::getBaseUrl());
16+
}
17+
18+
/**
19+
* @param $ch
20+
* @param $url
21+
* @return mixed
22+
*/
23+
private function send($ch, $url)
24+
{
25+
curl_setopt($ch, CURLOPT_URL, $url);
26+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
27+
'Authorization: Bearer ' . Connector::getAccessToken(),
28+
'Content-Type: application/json'
29+
));
30+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
31+
32+
return curl_exec($ch);
33+
}
34+
35+
/**
36+
* @param $url
37+
* @return mixed
38+
*/
39+
protected function getRequest($url)
40+
{
41+
return $this->sendRequest($this->getCh(), $url);
42+
}
43+
44+
/**
45+
* @param $url
46+
* @param $data
47+
* @return mixed
48+
*/
49+
protected function postRequest($url, $data)
50+
{
51+
$ch = $this->getCh();
52+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
53+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
54+
55+
return $this->sendRequest($ch, $url);
56+
}
57+
58+
/**
59+
* @param $url
60+
* @param $data
61+
* @return mixed
62+
*/
63+
protected function putRequest($url, $data)
64+
{
65+
$ch = $this->getCh();
66+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
67+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
68+
69+
return $this->sendRequest($ch, $url);
70+
}
71+
72+
/**
73+
* @param $ch
74+
* @param $url
75+
* @param $recursive
76+
* @return mixed
77+
* @throws ParasutException
78+
*/
79+
private function sendRequest($ch, $url, $recursive = false)
80+
{
81+
if (!$recursive) {
82+
$url = sprintf('%s/%s/%s', Options::getBaseUrl(), Options::getCompanyId(), $url);
83+
}
84+
85+
$response = json_decode(
86+
$this->send($ch, $url)
87+
);
88+
89+
if (!$this->errorHandler($response)) {
90+
$response = $this->sendRequest($ch, $url, true);
91+
}
92+
93+
return $response;
94+
}
95+
96+
/**
97+
* @param $response
98+
* @return bool
99+
* @throws ParasutException
100+
*/
101+
private function errorHandler($response)
102+
{
103+
if (isset($response->error) || empty($response)) {
104+
Options::setAccessToken(null);
105+
return false;
106+
}
107+
108+
if (isset($response->errors)) {
109+
$exception = new ParasutException('Something went wrong. Please check details.');
110+
$exception->setDetails($response->errors);
111+
throw $exception;
112+
}
113+
114+
return true;
115+
}
116+
}

Api/Connector.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Parasut\Api;
4+
5+
use League\OAuth2\Client\Provider\GenericProvider;
6+
use Parasut\Options;
7+
8+
class Connector extends Options
9+
{
10+
/**
11+
* Connector constructor.
12+
*/
13+
private function __construct()
14+
{
15+
if (empty(parent::getClient())) {
16+
parent::setClient(
17+
new GenericProvider([
18+
'clientId' => parent::getClientId(),
19+
'clientSecret' => parent::getClientSecret(),
20+
'redirectUri' => parent::getRedirectUri(),
21+
'urlAuthorize' => parent::getUrlAuthorize(),
22+
'urlAccessToken' => parent::getUrlAccessToken(),
23+
'urlResourceOwnerDetails' => parent::getUrlResourceOwnerDetails()
24+
])
25+
);
26+
}
27+
}
28+
29+
/**
30+
* @return GenericProvider
31+
*/
32+
public static function getInstance()
33+
{
34+
if (empty(parent::getClient())) {
35+
new self;
36+
}
37+
38+
return parent::getClient();
39+
}
40+
41+
/**
42+
* @return \League\OAuth2\Client\Token\AccessToken|null
43+
*/
44+
public static function getAccessToken()
45+
{
46+
$provider = self::getInstance();
47+
48+
if (empty(parent::getAccessToken())) {
49+
parent::setAccessToken(
50+
$provider->getAccessToken('password', [
51+
'username' => parent::getUsername(),
52+
'password' => parent::getPassword()
53+
])->getToken()
54+
);
55+
}
56+
57+
return parent::getAccessToken();
58+
}
59+
}

0 commit comments

Comments
 (0)