Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions src/Wheniwork.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Wheniwork;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a sub-namespace for every project/library:

namespace Wheniwork\Api;


/**
* Client library for the When I Work scheduling and attendance platform.
Expand All @@ -14,19 +14,23 @@
*/
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';

private $api_key;
private $api_token;
private $api_endpoint = 'https://api.wheniwork.com/2';
private $verify_ssl = false;
protected $verify_ssl = false;
protected $api_endpoint = 'https://api.wheniwork.com/2';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a constant too?


/**
* Create a new instance
*
* @param string $api_token The user WhenIWork API token
*/
function __construct($api_token = null)
Expand All @@ -36,6 +40,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) {
Expand All @@ -44,6 +49,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() {
Expand All @@ -52,69 +58,75 @@ 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;

Expand All @@ -131,7 +143,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;
}
Expand All @@ -144,7 +156,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));
}

Expand All @@ -158,19 +170,20 @@ 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();
$response = $login->makeRequest("login", self::METHOD_POST, $params, $headers);
Expand Down