diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..3d7edd3 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,14 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/src/Wheniwork.php b/src/Wheniwork.php index b61e0e5..5e689b4 100644 --- a/src/Wheniwork.php +++ b/src/Wheniwork.php @@ -12,22 +12,30 @@ * @author Daniel Olfelt * @version 0.1 */ -class Wheniwork { +class Wheniwork +{ + /** + * Library Version + */ + const VERSION = '0.1'; - 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'; - private $api_key; private $api_token; private $api_endpoint = 'https://api.wheniwork.com/2'; - private $api_headers = []; + private $api_headers = []; private $verify_ssl = false; /** * Create a new instance + * * @param string $api_token The user WhenIWork API token * @param array $options Allows you to set the `headers` and the `endpoint` */ @@ -36,148 +44,173 @@ function __construct($api_token = null, $options = []) $this->api_token = $api_token; if (!empty($options['endpoint'])) { - $this->setEndpoint($options['endpoint']); + $this->setEndpoint($options['endpoint']); } if (!empty($options['headers'])) { - $this->setHeaders($options['headers'], true); + $this->setHeaders($options['headers'], true); } } /** * Set the user token for all requests + * * @param string $api_token The user WhenIWork API token * @return Wheniwork */ - public function setToken($api_token) { + public function setToken($api_token) + { $this->api_token = $api_token; + return $this; } /** * Get the user token to save for future requests + * * @return string The user WhenIWork API token */ - public function getToken() { + public function getToken() + { return $this->api_token; } /** - * Set the endpoint for all requests - * @param string $endpoint The WhenIWork API endpoint to use - * @return Wheniwork - */ - public function setEndpoint($endpoint) { - $this->api_endpoint = $endpoint; - return $this; + * Set the endpoint for all requests + * + * @param string $endpoint The WhenIWork API endpoint to use + * @return Wheniwork + */ + public function setEndpoint($endpoint) + { + $this->api_endpoint = $endpoint; + + return $this; } /** - * Get the endpoint to use for future requests - * @return string The WhenIWork API endpoint - */ - public function getEndpoint() { - return $this->api_endpoint; + * Get the endpoint to use for future requests + * + * @return string The WhenIWork API endpoint + */ + public function getEndpoint() + { + return $this->api_endpoint; } /** - * Set the host for all requests - * @param array $headers Global headers for all future requests - * @return Wheniwork - */ - public function setHeaders(array $headers, $reset = false) { - if ($reset === true) { - $this->api_headers = $headers; - } - else { - $this->api_headers += $headers; - } + * Set the headers for all requests + * + * @param array $headers Global headers for all future requests + * @param bool $reset + * @return $this + */ + public function setHeaders(array $headers, $reset = false) + { + if ($reset === true) { + $this->api_headers = $headers; + } else { + $this->api_headers += $headers; + } - return $this; + return $this; } /** - * Get the host to use for future requests - * @return array Global headers array - */ - public function getHeaders() { - return $this->api_headers; + * Get the host to use for future requests + * + * @return array Global headers array + */ + public function getHeaders() + { + return $this->api_headers; } /** * 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 + * + * @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 + * + * @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 + * + * @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 + * + * @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 + * + * @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 $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 + * @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()) { - - $url = $this->getEndpoint().'/'.$method; + private function makeRequest($method, $request, $params = [], $headers = []) + { + $url = $this->getEndpoint() . '/' . $method; if ($params && ($request == self::METHOD_GET || $request == self::METHOD_DELETE)) { - $url .= '?'.http_build_query($params); + $url .= '?' . http_build_query($params); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_USERAGENT, 'WhenIWork-PHP/0.1'); + curl_setopt($ch, CURLOPT_USERAGENT, 'WhenIWork-PHP/' . static::VERSION); $headers += $this->getHeaders(); @@ -186,9 +219,9 @@ private function makeRequest($method, $request, $params = array(), $headers = ar $headers['W-Token'] = $this->api_token; } - $header_data = array(); - foreach ($headers as $k=>$v) { - $headers_data[] = $k.': '.$v; + $headers_data = []; + foreach ($headers as $k => $v) { + $headers_data[] = $k . ': ' . $v; } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_data); @@ -199,7 +232,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)); } @@ -210,27 +243,28 @@ 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 */ - public static function login($key, $email, $password) { - $params = array( + public static function login($key, $email, $password) + { + $params = [ "username" => $email, - "password" => $password - ); - $headers = array( + "password" => $password, + ]; + + $headers = [ 'W-Key' => $key - ); + ]; $login = new static(); $response = $login->makeRequest("login", self::METHOD_POST, $params, $headers); return $response; } - } diff --git a/tests/WheniworkTest.php b/tests/WheniworkTest.php index aa5a491..f9d78fc 100644 --- a/tests/WheniworkTest.php +++ b/tests/WheniworkTest.php @@ -1,5 +1,7 @@