Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
45 changes: 35 additions & 10 deletions src/Wheniwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ class Wheniwork
/**
* HTTP Methods
*/
const METHOD_GET = 'get';
const METHOD_POST = 'post';
const METHOD_PUT = 'put';
const METHOD_PATCH = 'patch';
const METHOD_DELETE = 'delete';
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_PATCH = 'PATCH';
const METHOD_DELETE = 'DELETE';

private $api_token;
private $api_endpoint = 'https://api.wheniwork.com/2';
private $api_headers = [];
private $verify_ssl = false;
private $ca_path = null;
private $ca_info = null;

/**
* 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`
*/
function __construct($api_token = null, $options = [])
public function __construct($api_token = null, $options = [])
{
$this->api_token = $api_token;

Expand All @@ -49,6 +51,15 @@ function __construct($api_token = null, $options = [])
if (!empty($options['headers'])) {
$this->setHeaders($options['headers'], true);
}
if (!empty($options['verify_ssl'])) {
$this->verify_ssl = $options['verify_ssl'];
}
if (!empty($options['ca_path'])) {
$this->ca_path = $options['ca_path'];
}
if (!empty($options['ca_info'])) {
$this->ca_info = $options['ca_info'];
}
}

/**
Expand Down Expand Up @@ -106,10 +117,17 @@ public function getEndpoint()
*/
public function setHeaders(array $headers, $reset = false)
{
if ($reset === true) {
if ($reset === true || !$this->api_headers) {
$this->api_headers = $headers;
} else {
$this->api_headers += $headers;
// Build a map so we can replace header names in an insensive manner while maintaining supplied case
$headerMap = array_change_key_case(array_combine(array_keys($this->api_headers), array_keys($this->api_headers)));
foreach ($headers as $name => $value){
if (array_key_exists(strtolower($name), $headerMap)){
unset($this->api_headers[ $headerMap[strtolower($name)] ]);
}
$this->api_headers[$name] = $value;
}
}

return $this;
Expand Down Expand Up @@ -231,6 +249,12 @@ private function makeRequest($method, $request, $params = [], $headers = [])
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
if ($this->ca_path) {
curl_setopt($ch, CURLOPT_CAPATH, $this->ca_path);
}
if ($this->ca_info) {
curl_setopt($ch, CURLOPT_CAINFO, $this->ca_info);
}

if (in_array($request, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
Expand All @@ -249,9 +273,10 @@ private function makeRequest($method, $request, $params = [], $headers = [])
* @param $key Developer API key
* @param $email Email of the user logging in
* @param $password Password of the user
* @param $options Allows you to set the `headers` and the `endpoint` for login
* @return
*/
public static function login($key, $email, $password)
public static function login($key, $email, $password, $options = [])
{
$params = [
"username" => $email,
Expand All @@ -262,7 +287,7 @@ public static function login($key, $email, $password)
'W-Key' => $key
];

$login = new static();
$login = new static(null, $options);
$response = $login->makeRequest("login", self::METHOD_POST, $params, $headers);

return $response;
Expand Down
28 changes: 28 additions & 0 deletions tests/WheniworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function testSetHeaders()

$wiw->setHeaders(['Another-Sample' => 'Test two']);
$this->assertEquals(2, count($wiw->getHeaders()));

$wiw->setHeaders(['another-sample' => 'Test three']);
$this->assertEquals(2, count($wiw->getHeaders()));

$this->assertArrayHasKey("another-sample", $wiw->getHeaders());
}

public function testSetToken()
Expand All @@ -45,5 +50,28 @@ public function testSetToken()

$this->assertNotEquals($token, $wiw->getToken());
}

public function testOptionsSetAtConstruction(){
$options = [
"endpoint"=>"http://example.com/2",
"verify_ssl"=>true,
"ca_path"=>"/path/to/ca",
"ca_info"=>"CA Cert"
];

$wiw = new Wheniwork("test", $options);
$refl = new ReflectionObject($wiw);

$this->assertEquals($options["endpoint"], $this->getPrivatePropValue($refl, $wiw, "api_endpoint"));
$this->assertEquals($options["verify_ssl"], $this->getPrivatePropValue($refl, $wiw, "verify_ssl"));
$this->assertEquals($options["ca_path"], $this->getPrivatePropValue($refl, $wiw, "ca_path"));
$this->assertEquals($options["ca_info"], $this->getPrivatePropValue($refl, $wiw, "ca_info"));
}

private function getPrivatePropValue(\ReflectionObject $refl, $obj, $property){
$prop = $refl->getProperty($property);
$prop->setAccessible(true);
return $prop->getValue($obj);
}

}