|
| 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 | +} |
0 commit comments