diff --git a/src/Wheniwork.php b/src/Wheniwork.php index 00604ed..fcf0dbf 100644 --- a/src/Wheniwork.php +++ b/src/Wheniwork.php @@ -14,19 +14,24 @@ */ class Wheniwork { - const METHOD_GET = 'get'; - const METHOD_POST = 'post'; - const METHOD_PUT = 'put'; - const METHOD_PATCH = 'patch'; + /** + * HTTP Methods + */ + const METHOD_GET = 'get'; + const METHOD_POST = 'post'; + const METHOD_PUT = 'put'; + const METHOD_PATCH = 'patch'; const METHOD_DELETE = 'delete'; + const API_ENDPOINT = 'https://api.wheniwork.com/2'; + private $api_key; private $api_token; - private $api_endpoint = 'https://api.wheniwork.com/2'; - private $verify_ssl = false; + protected $verify_ssl = false; /** * Create a new instance + * * @param string $api_token The user WhenIWork API token */ function __construct($api_token = null) @@ -36,6 +41,7 @@ function __construct($api_token = null) /** * Set the user token for all requests + * * @param string $api_token The user WhenIWork API token */ public function setToken($api_token) { @@ -44,6 +50,7 @@ public function setToken($api_token) { /** * Get the user token to save for future requests + * * @return string The user WhenIWork API token */ public function getToken() { @@ -52,71 +59,77 @@ public function getToken() { /** * Get an object or list. + * * @param string $method The API method to call, e.g. '/users/' * @param array $params An array of arguments to pass to the method. * @param array $headers Array of custom headers to be passed * @return array Object of json decoded API response. */ - public function get($method, $params = array(), $headers = array()) { + public function get($method, $params = [], $headers = []) { return $this->makeRequest($method, self::METHOD_GET, $params, $headers); } /** * Post to an endpoint. + * * @param string $method The API method to call, e.g. '/shifts/publish/' * @param array $params An array of data used to create the object. * @param array $headers Array of custom headers to be passed * @return array Object of json decoded API response. */ - public function post($method, $params = array(), $headers = array()) { + public function post($method, $params = [], $headers = []) { return $this->makeRequest($method, self::METHOD_POST, $params, $headers); } /** * Create an object. Helper method for post. + * * @param string $method The API method to call, e.g. '/users/' * @param array $params An array of data used to create the object. * @param array $headers Array of custom headers to be passed * @return array Object of json decoded API response. */ - public function create($method, $params = array(), $headers = array()) { + public function create($method, $params = [], $headers = []) { return $this->post($method, $params, $headers); } /** * Update an object. Must include the ID. + * * @param string $method The API method to call, e.g. '/users/1' * @param array $params An array of data to update the object. Only changed fields needed. * @param array $headers Array of custom headers to be passed * @return array Object of json decoded API response. */ - public function update($method, $params = array(), $headers = array()) { + public function update($method, $params = [], $headers = []) { return $this->makeRequest($method, self::METHOD_PUT, $params, $headers); } /** * Delete an object. Must include the ID. + * * @param string $method The API method to call, e.g. '/users/1' * @param array $params An array of arguments to pass to the method. * @param array $headers Array of custom headers to be passed * @return array Object of json decoded API response. */ - public function delete($method, $params = array(), $headers = array()) { + public function delete($method, $params = [], $headers = []) { return $this->makeRequest($method, self::METHOD_DELETE, $params, $headers); } /** * Performs the underlying HTTP request. Exciting stuff happening here. Not really. + * * @param string $method The API method to be called * @param string $request The type of request * @param array $params Assoc array of parameters to be passed * @param array $headers Assoc array of custom headers to be passed * @return array Assoc array of decoded result */ - private function makeRequest($method, $request, $params = array(), $headers = array()) { + private function makeRequest($method, $request, $params = [], $headers = []) { - $url = $this->api_endpoint.'/'.$method; + $url = static::API_ENDPOINT.'/'.$method; if ($params && ($request == self::METHOD_GET || $request == self::METHOD_DELETE)) { $url .= '?'.http_build_query($params); @@ -131,7 +144,7 @@ private function makeRequest($method, $request, $params = array(), $headers = ar $headers['W-Token'] = $this->api_token; } - $header_data = array(); + $header_data = []; foreach ($headers as $k=>$v) { $headers_data[] = $k.': '.$v; } @@ -144,7 +157,7 @@ private function makeRequest($method, $request, $params = array(), $headers = ar curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl); - if (in_array($request, array(self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH))) { + if (in_array($request, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH])) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); } @@ -158,21 +171,22 @@ private function makeRequest($method, $request, $params = array(), $headers = ar /** * Login helper using developer key and credentials to get back a login response + * * @param $key Developer API key * @param $email Email of the user logging in * @param $password Password of the user - * @return + * @return object */ public static function login($key, $email, $password) { - $params = array( + $params = [ "username" => $email, "password" => $password - ); - $headers = array( + ]; + $headers = [ 'W-Key' => $key - ); + ]; - $login = new self(); + $login = new static; $response = $login->makeRequest("login", self::METHOD_POST, $params, $headers); return $response;