Skip to content

Commit fbf8898

Browse files
committed
Initial commit
0 parents  commit fbf8898

Some content is hidden

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

46 files changed

+1351
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.phar
2+
/vendor/
3+
.vscode/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Paul Gossen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# PHP Enhance API Wrapper
2+
3+
```
4+
composer require gosuccess/php-enhance-api-wrapper
5+
```

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "gosuccess/php-enhance-api-wrapper",
3+
"description": "PHP Enhance API Wrapper",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"GoSuccess\\Enhance\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "GoSuccess",
14+
"homepage": "https://gosuccess.io"
15+
}
16+
],
17+
"require": {}
18+
}

composer.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/API.php

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
namespace GoSuccess\Enhance;
4+
5+
use GoSuccess\Enhance\Controllers\CustomerController;
6+
use GoSuccess\Enhance\Controllers\LoginController;
7+
use GoSuccess\Enhance\Controllers\MemberController;
8+
use GoSuccess\Enhance\Controllers\OrganizationController;
9+
use GoSuccess\Enhance\Controllers\PlanController;
10+
use GoSuccess\Enhance\Controllers\StatusController;
11+
use GoSuccess\Enhance\Controllers\SubscriptionController;
12+
use GoSuccess\Enhance\Controllers\VersionController;
13+
use GoSuccess\Enhance\Controllers\WebsiteController;
14+
use GoSuccess\Enhance\Enumerations\HttpMethod;
15+
use GoSuccess\Enhance\Models\Exception;
16+
use stdClass;
17+
18+
class API
19+
{
20+
private array $errors = [];
21+
22+
public string $host;
23+
24+
public string $org_id;
25+
26+
public string $access_token;
27+
28+
public VersionController $version;
29+
30+
public StatusController $status;
31+
32+
public OrganizationController $organization;
33+
34+
public LoginController $login;
35+
36+
public CustomerController $customer;
37+
38+
public MemberController $member;
39+
40+
public PlanController $plan;
41+
42+
public SubscriptionController $subscription;
43+
44+
public WebsiteController $website;
45+
46+
public function __construct( string $host, string $org_id, string $access_token )
47+
{
48+
try
49+
{
50+
$this->host = $host;
51+
$this->org_id = $org_id;
52+
$this->access_token = $access_token;
53+
54+
$this->version = new VersionController( $this );
55+
$this->status = new StatusController( $this );
56+
$this->organization = new OrganizationController( $this );
57+
$this->login = new LoginController( $this );
58+
$this->customer = new CustomerController( $this );
59+
$this->member = new MemberController( $this );
60+
$this->plan = new PlanController( $this );
61+
$this->subscription = new SubscriptionController( $this );
62+
$this->website = new WebsiteController( $this );
63+
}
64+
catch( Exception $e )
65+
{
66+
$this->errors[] = $e;
67+
}
68+
}
69+
70+
public function set_host( string $host ): void
71+
{
72+
$this->host = $host;
73+
}
74+
75+
public function set_org_id( string $org_id ): void
76+
{
77+
$this->org_id = $org_id;
78+
}
79+
80+
public function set_access_token( string $access_token ): void
81+
{
82+
$this->access_token = $access_token;
83+
}
84+
85+
public function request( HttpMethod $method, string $endpoint = '/version', object|string $payload = '', int $timeout = 30, bool $verify_ssl = false ): string|stdClass|bool|null
86+
{
87+
$response = null;
88+
89+
try
90+
{
91+
$headers = [
92+
'Content-type: application/json',
93+
'Accept-Charset: utf-8',
94+
'Accept: application/json',
95+
'Authorization: Bearer ' . (string) $this->access_token
96+
];
97+
98+
$payload = json_encode( $payload );
99+
100+
$request_url = (string) $this->host . $endpoint;
101+
102+
$ch = curl_init();
103+
104+
curl_setopt_array(
105+
$ch,
106+
[
107+
CURLOPT_RETURNTRANSFER => true,
108+
CURLOPT_FOLLOWLOCATION => false,
109+
CURLOPT_ENCODING => '',
110+
CURLOPT_TIMEOUT => $timeout,
111+
CURLOPT_SSL_VERIFYPEER => $verify_ssl,
112+
CURLOPT_URL => $request_url,
113+
CURLOPT_CUSTOMREQUEST => $method->value,
114+
CURLOPT_HTTPHEADER => $headers,
115+
CURLOPT_POSTFIELDS => $payload
116+
]
117+
);
118+
119+
$result = curl_exec( $ch );
120+
$http_code = curl_getinfo( $ch, CURLINFO_RESPONSE_CODE );
121+
122+
curl_close( $ch );
123+
124+
switch( $http_code )
125+
{
126+
case 400:
127+
$this->errors[] = [
128+
'status' => 'Invalid input',
129+
'message' => $result
130+
];
131+
break;
132+
133+
case 401:
134+
$this->errors[] = [
135+
'status' => 'Invalid session',
136+
'message' => $result
137+
];
138+
break;
139+
140+
case 403:
141+
$this->errors[] = [
142+
'status' => 'Insufficient privileges',
143+
'message' => $result
144+
];
145+
break;
146+
147+
case 404:
148+
$this->errors[] = [
149+
'status' => 'Not found',
150+
'message' => $result
151+
];
152+
break;
153+
154+
case 409:
155+
$this->errors[] = [
156+
'status' => 'Already exists',
157+
'message' => $result
158+
];
159+
break;
160+
161+
default:
162+
if ( empty( $result ) )
163+
{
164+
$response = true;
165+
}
166+
else
167+
{
168+
$json = json_decode( $result );
169+
$response = $json === null ? str_replace( '"', '', $result ) : $json;
170+
}
171+
break;
172+
}
173+
}
174+
catch( Exception $e )
175+
{
176+
$this->errors[] = $e->getMessage();
177+
return null;
178+
}
179+
180+
return $response;
181+
}
182+
183+
public function get_errors(): array
184+
{
185+
return $this->errors;
186+
}
187+
188+
public function get_last_error(): Exception
189+
{
190+
return end( $this->errors );
191+
}
192+
}

src/Abstracts/Controller.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace GoSuccess\Enhance\Abstracts;
4+
5+
use GoSuccess\Enhance\API;
6+
7+
class Controller
8+
{
9+
protected API $api;
10+
11+
public function __construct( API $api )
12+
{
13+
$this->api = $api;
14+
}
15+
}

0 commit comments

Comments
 (0)