From 0f858ca5394a9fcabd8c810c6e4848001d6044ca Mon Sep 17 00:00:00 2001 From: "DESKTOP-9DOKON5\\Adam" Date: Fri, 15 Jun 2018 10:01:25 +0100 Subject: [PATCH] Formatting amendments --- src/Vtiger.php | 77 ++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 49 deletions(-) diff --git a/src/Vtiger.php b/src/Vtiger.php index d93ae28..e6693fd 100644 --- a/src/Vtiger.php +++ b/src/Vtiger.php @@ -67,19 +67,18 @@ 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)) { @@ -87,21 +86,23 @@ protected function sessionid() 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; - } /** @@ -116,7 +117,6 @@ protected function sessionid() */ protected function login($sessionData) { - $token = $sessionData->token; // Create unique key using combination of challengetoken and accesskey @@ -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; - } /** @@ -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; - } /** @@ -209,7 +211,6 @@ protected function storesession() */ protected function gettoken() { - // perform API GET request $response = $this->client->request('GET', $this->url, [ 'query' => [ @@ -228,7 +229,6 @@ protected function gettoken() ); return $output; - } /** @@ -242,7 +242,6 @@ protected function gettoken() */ protected function close($sessionid) { - if ($this->persistConnection) { return true; } @@ -256,7 +255,6 @@ protected function close($sessionid) ]); return $this->_processResult($response); - } /** @@ -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 @@ -286,7 +283,6 @@ public function query($query) self::close($sessionid); return $this->_processResult($response); - } /** @@ -302,7 +298,6 @@ public function query($query) */ public function retrieve($id) { - $sessionid = self::sessionid(); // send a request to retrieve a record @@ -317,7 +312,6 @@ public function retrieve($id) self::close($sessionid); return $this->_processResult($response); - } /** @@ -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 @@ -359,7 +352,6 @@ public function create(string $elem, string $data) self::close($sessionid); return $this->_processResult($response); - } /** @@ -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 @@ -391,7 +382,6 @@ public function update($object) self::close($sessionid); return $this->_processResult($response); - } /** @@ -407,7 +397,6 @@ public function update($object) */ public function delete($id) { - $sessionid = self::sessionid(); // send a request to delete a record @@ -422,7 +411,6 @@ public function delete($id) self::close($sessionid); return $this->_processResult($response); - } /** @@ -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 @@ -452,7 +439,6 @@ public function describe($elementType) self::close($sessionid); return $this->_processResult($response); - } /** @@ -465,14 +451,13 @@ 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) { @@ -480,7 +465,6 @@ protected function _processResult($response) } return $data; - } /** @@ -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); } - } /** @@ -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); - } - -} +} \ No newline at end of file