Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| API | |
0.00% |
0 / 26 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| api_call | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| phone_hash | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace BuyerKiosk\FiveStars; |
| 3 | |
| 4 | class API { |
| 5 | |
| 6 | // the base URL of the API environment in use |
| 7 | private $api_url; |
| 8 | |
| 9 | // specific to environment |
| 10 | private $api_key; |
| 11 | |
| 12 | // specific to environment |
| 13 | private $api_secret; |
| 14 | |
| 15 | public function __construct($dev = null) { |
| 16 | if($dev == 1) { |
| 17 | $this->api_url = $_ENV['FS_API_URL_DEV']; |
| 18 | $this->api_key = $_ENV['FS_API_KEY_DEV']; |
| 19 | $this->api_secret = $_ENV['FS_API_SECRET_DEV']; |
| 20 | } else { |
| 21 | $this->api_url = $_ENV['FS_API_URL']; |
| 22 | $this->api_key = $_ENV['FS_API_KEY']; |
| 23 | $this->api_secret = $_ENV['FS_API_SECRET']; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /* |
| 28 | * This function handles all API calls. |
| 29 | * Inbound arguments adjust for GET/POST/PATCH |
| 30 | */ |
| 31 | public function api_call( |
| 32 | $endpoint='', |
| 33 | $data=null, |
| 34 | $patch=false |
| 35 | ) { |
| 36 | $ch = curl_init($this->api_url . $endpoint); |
| 37 | |
| 38 | // if data is sent in, POST request |
| 39 | if ($data !== null) { |
| 40 | curl_setopt($ch, CURLOPT_POSTFIELDS, |
| 41 | json_encode($data) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | // some API calls use PATCH |
| 46 | if ($patch) { |
| 47 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
| 48 | } |
| 49 | |
| 50 | // API call require JSON API headers |
| 51 | curl_setopt($ch, CURLOPT_HTTPHEADER, |
| 52 | array('Content-Type:application/json') |
| 53 | ); |
| 54 | |
| 55 | // uses env specific auth |
| 56 | curl_setopt($ch, CURLOPT_USERPWD, |
| 57 | $this->api_key . ":" . $this->api_secret |
| 58 | ); |
| 59 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 60 | $result = curl_exec($ch); |
| 61 | curl_close($ch); |
| 62 | // print_r($result); |
| 63 | |
| 64 | // JSON API result needs decoding |
| 65 | $result = json_decode($result, true); |
| 66 | return $result; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Hashes phone numbers for the API |
| 71 | */ |
| 72 | public function phone_hash($phone_number) { |
| 73 | return sha1($phone_number); |
| 74 | } |
| 75 | } |