Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
Formatting amendments
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-godfrey committed Jun 15, 2018
1 parent efb3701 commit 0f858ca
Showing 1 changed file with 28 additions and 49 deletions.
77 changes: 28 additions & 49 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,42 @@ public function connection($url, $username, $accesskey)
*/
protected function sessionid()
{

// Check the session file exists
switch ($this->sessionDriver) {
case "file":
case 'file':
if (Storage::disk('local')->exists('session.json')) {
$sessionData = json_decode(Storage::disk('local')->get('session.json'));
}
break;
case "redis":
case 'redis':
$sessionData = json_decode(Redis::get('clystnet_vtiger'));
break;
default:
throw new VtigerError("Session driver type of ".$this->sessionDriver." is not supported", 4);
throw new VtigerError('Session driver type of ' . $this->sessionDriver . ' is not supported', 4);
}

if (isset($sessionData)) {
if (isset($sessionData) && property_exists($sessionData, 'expireTime') && property_exists($sessionData, 'token')) {
if ($sessionData->expireTime < time() || empty($sessionData->token)) {
$sessionData = $this->storesession();
}
} else {
}
else {
$sessionData = $this->storesession();
}
} else {
}
else {
$sessionData = $this->storesession();
}

if (isset($json->sessionid)) {
$sessionid = $json->sessionid;
} else {
}
else {
$sessionid = $this->login($sessionData);
}

return $sessionid;

}

/**
Expand All @@ -116,7 +117,6 @@ protected function sessionid()
*/
protected function login($sessionData)
{

$token = $sessionData->token;

// Create unique key using combination of challengetoken and accesskey
Expand All @@ -137,45 +137,48 @@ protected function login($sessionData)
// If api login failed
if ($response->getStatusCode() !== 200 || !$loginResult->success) {
if (!$loginResult->success) {
if ($loginResult->error->code == "INVALID_USER_CREDENTIALS" || $loginResult->error->code == "INVALID_SESSIONID") {
if ($loginResult->error->code == 'INVALID_USER_CREDENTIALS' || $loginResult->error->code == 'INVALID_SESSIONID') {
if ($this->sessionDriver == 'file') {
if (Storage::disk('local')->exists('session.json')) {
Storage::disk('local')->delete('session.json');
}
} elseif ($this->sessionDriver == 'redis') {
}
elseif ($this->sessionDriver == 'redis') {
Redis::del('clystnet_vtiger');
}
} else {
}
else {
$this->_processResult($response);
}
} else {
}
else {
$this->_checkResponseStatusCode($response);
}
} else {
}
else {
// login ok so get sessionid and update our session
$sessionid = $loginResult->result->sessionName;

switch ($this->sessionDriver) {
case "file":
case 'file':
if (Storage::disk('local')->exists('session.json')) {
$json = json_decode(Storage::disk('local')->get('session.json'));
$json->sessionid = $sessionid;
Storage::disk('local')->put('session.json', json_encode($json));
}
break;
case "redis":
case 'redis':
Redis::incr('loggedin');
$json = json_decode(Redis::get('clystnet_vtiger'));
$json->sessionid = $sessionid;
Redis::set('clystnet_vtiger', json_encode($json));
break;
default:
throw new VtigerError("Session driver type of ".$this->sessionDriver." is not supported", 4);
throw new VtigerError('Session driver type of ' . $this->sessionDriver . ' is not supported', 4);
}
}

return $sessionid;

}

/**
Expand All @@ -186,18 +189,17 @@ protected function login($sessionData)
*/
protected function storesession()
{

$updated = $this->gettoken();

$output = (object)$updated;

if ($this->sessionDriver == 'file') {
Storage::disk('local')->put('session.json', json_encode($output));
} elseif ($this->sessionDriver == 'redis') {
}
elseif ($this->sessionDriver == 'redis') {
Redis::set('clystnet_vtiger', json_encode($output));
}

return $output;

}

/**
Expand All @@ -209,7 +211,6 @@ protected function storesession()
*/
protected function gettoken()
{

// perform API GET request
$response = $this->client->request('GET', $this->url, [
'query' => [
Expand All @@ -228,7 +229,6 @@ protected function gettoken()
);

return $output;

}

/**
Expand All @@ -242,7 +242,6 @@ protected function gettoken()
*/
protected function close($sessionid)
{

if ($this->persistConnection) {
return true;
}
Expand All @@ -256,7 +255,6 @@ protected function close($sessionid)
]);

return $this->_processResult($response);

}

/**
Expand All @@ -271,7 +269,6 @@ protected function close($sessionid)
*/
public function query($query)
{

$sessionid = self::sessionid();

// send a request using a database query to get back any matching records
Expand All @@ -286,7 +283,6 @@ public function query($query)
self::close($sessionid);

return $this->_processResult($response);

}

/**
Expand All @@ -302,7 +298,6 @@ public function query($query)
*/
public function retrieve($id)
{

$sessionid = self::sessionid();

// send a request to retrieve a record
Expand All @@ -317,7 +312,6 @@ public function retrieve($id)
self::close($sessionid);

return $this->_processResult($response);

}

/**
Expand All @@ -343,7 +337,6 @@ public function retrieve($id)
*/
public function create(string $elem, string $data)
{

$sessionid = self::sessionid();

// send a request to create a record
Expand All @@ -359,7 +352,6 @@ public function create(string $elem, string $data)
self::close($sessionid);

return $this->_processResult($response);

}

/**
Expand All @@ -376,7 +368,6 @@ public function create(string $elem, string $data)
*/
public function update($object)
{

$sessionid = self::sessionid();

// send a request to update a record
Expand All @@ -391,7 +382,6 @@ public function update($object)
self::close($sessionid);

return $this->_processResult($response);

}

/**
Expand All @@ -407,7 +397,6 @@ public function update($object)
*/
public function delete($id)
{

$sessionid = self::sessionid();

// send a request to delete a record
Expand All @@ -422,7 +411,6 @@ public function delete($id)
self::close($sessionid);

return $this->_processResult($response);

}

/**
Expand All @@ -437,7 +425,6 @@ public function delete($id)
*/
public function describe($elementType)
{

$sessionid = self::sessionid();

// send a request to describe a module (which returns a list of available fields) for a Vtiger module
Expand All @@ -452,7 +439,6 @@ public function describe($elementType)
self::close($sessionid);

return $this->_processResult($response);

}

/**
Expand All @@ -465,22 +451,20 @@ public function describe($elementType)
*/
protected function _processResult($response)
{

$this->_checkResponseStatusCode($response);

// decode the response
$data = json_decode($response->getBody()->getContents());

if (!isset($data->success)) {
throw new VtigerError("Success property not set on VTiger response", 2);
throw new VtigerError('Success property not set on VTiger response', 2);
}

if ($data->success == false) {
$this->_processResponseError($data);
}

return $data;

}

/**
Expand All @@ -492,11 +476,9 @@ protected function _processResult($response)
*/
protected function _checkResponseStatusCode($response)
{

if ($response->getStatusCode() !== 200) {
throw new VtigerError("API request did not complete correctley - Response code: ".$response->getStatusCode(), 1);
throw new VtigerError('API request did not complete correctley - Response code: ' . $response->getStatusCode(), 1);
}

}

/**
Expand All @@ -508,13 +490,10 @@ protected function _checkResponseStatusCode($response)
*/
protected function _processResponseError($processedData)
{

if (!isset($processedData->error)) {
throw new VtigerError("Error property not set on VTiger response when success is false", 3);
throw new VtigerError('Error property not set on VTiger response when success is false', 3);
}

throw new VtigerError($processedData->error->message, 4, $processedData->error->code);

}

}
}

0 comments on commit 0f858ca

Please sign in to comment.