-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Invertus/SMAR-16/ajax-error-handling
SMAR-16/ Ajax controller error handling improvements
- Loading branch information
Showing
3 changed files
with
214 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,94 +2,144 @@ | |
/** | ||
* Smartsupp Live Chat integration module. | ||
* | ||
* @package Smartsupp | ||
* @author Smartsupp <[email protected]> | ||
* @link http://www.smartsupp.com | ||
* @copyright 2016 Smartsupp.com | ||
* @license GPL-2.0+ | ||
* @package Smartsupp | ||
* @link http://www.smartsupp.com | ||
* | ||
* Plugin Name: Smartsupp Live Chat | ||
* Plugin URI: http://www.smartsupp.com | ||
* Description: Adds Smartsupp Live Chat code to PrestaShop. | ||
* Version: 2.2.0 | ||
* Text Domain: smartsupp | ||
* Author: Smartsupp | ||
* Author URI: http://www.smartsupp.com | ||
* Text Domain: smartsupp | ||
* License: GPL-2.0+ | ||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | ||
*/ | ||
|
||
use \Smartsupp\Auth\Api; | ||
use Smartsupp\Auth\Api; | ||
use Smartsupp\LiveChat\Validator\UserCredentialsValidator; | ||
|
||
class AdminSmartsuppAjaxController extends ModuleAdminController | ||
{ | ||
const FILE_NAME = 'AdminSmartsuppAjaxController'; | ||
public $ssl = true; | ||
|
||
private $partnerKey = 'h4w6t8hln9'; | ||
private $response = []; | ||
private $api; | ||
|
||
const LOGIN_ACTION = 'login'; | ||
const CREATE_ACTION = 'create'; | ||
const DEACTIVATE_ACTION = 'deactivate'; | ||
|
||
public function init() | ||
{ | ||
$api = new Api(); | ||
|
||
switch (Tools::getValue('action')) { | ||
case 'login': | ||
$this->response = $api->login([ | ||
'email' => Tools::getValue('email'), | ||
'password' => Tools::getValue('password'), | ||
'platform' => 'Prestashop ' . _PS_VERSION_, | ||
]); | ||
$this->updateCredentials(); | ||
break; | ||
case 'create': | ||
$this->response = $api->create([ | ||
'email' => Tools::getValue('email'), | ||
'password' => Tools::getValue('password'), | ||
'partnerKey' => $this->partnerKey, | ||
'consentTerms' => 1, | ||
'platform' => 'Prestashop ' . _PS_VERSION_, | ||
]); | ||
$this->updateCredentials(); | ||
break; | ||
case 'deactivate': | ||
Configuration::updateValue('SMARTSUPP_KEY', ''); | ||
Configuration::updateValue('SMARTSUPP_EMAIL', ''); | ||
break; | ||
$validator = new UserCredentialsValidator($this->module); | ||
|
||
$validator->validate(Tools::getAllValues()); | ||
$action = Tools::getValue('action'); | ||
|
||
if ($validator->getError() && $action !== self::DEACTIVATE_ACTION) { | ||
$this->handleError($validator->getMessage(), $validator->getError()); | ||
} | ||
|
||
header('Content-Type: application/json'); | ||
try { | ||
$this->api = new Api(); | ||
|
||
$responseData = [ | ||
'key' => Configuration::get('SMARTSUPP_KEY'), | ||
'email' => Configuration::get('SMARTSUPP_EMAIL'), | ||
'error' => isset($this->response['error']) ? $this->response['error'] : null, | ||
'message' => isset($this->response['message']) ? $this->response['message'] : null, | ||
]; | ||
switch ($action) { | ||
case self::LOGIN_ACTION: | ||
$this->handleLoginAction(); | ||
break; | ||
case self::CREATE_ACTION: | ||
$this->handleCreateAction(); | ||
break; | ||
case self::DEACTIVATE_ACTION: | ||
$this->handleDeactivateAction(); | ||
break; | ||
default: | ||
throw new Exception('Invalid action'); | ||
} | ||
} catch (Exception $e) { | ||
$this->handleError($e->getMessage()); | ||
} | ||
|
||
$responseData = array_filter($responseData, function ($val) { | ||
return $val !== null; | ||
}); | ||
$this->sendResponse(); | ||
} | ||
|
||
die(json_encode($responseData)); | ||
private function handleLoginAction() | ||
{ | ||
$this->response = $this->api->login([ | ||
'email' => Tools::getValue('email'), | ||
'password' => Tools::getValue('password'), | ||
'platform' => 'Prestashop ' . _PS_VERSION_, | ||
]); | ||
|
||
$this->updateCredentials(); | ||
} | ||
|
||
private function handleCreateAction() | ||
{ | ||
$this->response = $this->api->create([ | ||
'email' => Tools::getValue('email'), | ||
'password' => Tools::getValue('password'), | ||
'partnerKey' => $this->partnerKey, | ||
'consentTerms' => 1, | ||
'platform' => 'Prestashop ' . _PS_VERSION_, | ||
]); | ||
|
||
$this->updateCredentials(); | ||
} | ||
|
||
private function handleDeactivateAction() | ||
{ | ||
Configuration::updateValue('SMARTSUPP_KEY', ''); | ||
Configuration::updateValue('SMARTSUPP_EMAIL', ''); | ||
|
||
$this->sendResponse(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
private function updateCredentials() | ||
{ | ||
if (isset($this->response['account']['key'])) { | ||
Configuration::updateValue('SMARTSUPP_KEY', $this->response['account']['key']); | ||
Configuration::updateValue('SMARTSUPP_EMAIL', Tools::getValue('email')); | ||
|
||
return; | ||
} | ||
|
||
$this->response['error'] = isset($this->response['error']) ? $this->response['error'] : $this->module->l('Unknown Error Occurred', self::FILE_NAME); | ||
$this->response['message'] = isset($this->response['message']) ? $this->response['message'] : $this->module->l('Unknown Error Occurred', self::FILE_NAME); | ||
if (isset($this->response['error'])) { | ||
$this->sendResponse(); | ||
} | ||
|
||
Configuration::updateValue('SMARTSUPP_KEY', ''); | ||
Configuration::updateValue('SMARTSUPP_EMAIL', ''); | ||
$this->handleError($this->module->l('Unknown error occurred while processing your request.', self::FILE_NAME)); | ||
} | ||
|
||
private function handleError($message, $error = 'error') | ||
{ | ||
$this->response['key'] = Configuration::get('SMARTSUPP_KEY'); | ||
$this->response['email'] = Configuration::get('SMARTSUPP_EMAIL'); | ||
|
||
$this->response['error'] = $error; | ||
$this->response['message'] = $message; | ||
|
||
die(json_encode($this->response)); | ||
} | ||
|
||
private function sendResponse() | ||
{ | ||
header('Content-Type: application/json'); | ||
|
||
$responseData = [ | ||
'key' => Configuration::get('SMARTSUPP_KEY'), | ||
'email' => Configuration::get('SMARTSUPP_EMAIL'), | ||
'error' => isset($this->response['error']) ? $this->response['error'] : null, | ||
'message' => isset($this->response['message']) ? $this->response['message'] : null, | ||
]; | ||
|
||
$responseData = array_filter($responseData, function ($val) { | ||
return $val !== null; | ||
}); | ||
|
||
die(json_encode($responseData)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Smartsupp Live Chat integration module. | ||
* | ||
* @package Smartsupp | ||
* @author Smartsupp <[email protected]> | ||
* @link http://www.smartsupp.com | ||
* @copyright 2016 Smartsupp.com | ||
* @license GPL-2.0+ | ||
* | ||
* Plugin Name: Smartsupp Live Chat | ||
* Plugin URI: http://www.smartsupp.com | ||
* Description: Adds Smartsupp Live Chat code to PrestaShop. | ||
* Version: 2.2.0 | ||
* Author: Smartsupp | ||
* Author URI: http://www.smartsupp.com | ||
* Text Domain: smartsupp | ||
* License: GPL-2.0+ | ||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | ||
*/ | ||
|
||
namespace Smartsupp\LiveChat\Validator; | ||
|
||
class UserCredentialsValidator | ||
{ | ||
const FILE_NAME = 'UserCredentialsValidator'; | ||
|
||
/** | ||
* @var \Smartsupp $module | ||
*/ | ||
private $module; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $error = ''; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $message = ''; | ||
|
||
public function __construct($module) | ||
{ | ||
$this->module = $module; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return void | ||
*/ | ||
public function validate($data) | ||
{ | ||
$email = isset($data['email']) ? $data['email'] : ''; | ||
$password = isset($data['password']) ? $data['password'] : ''; | ||
|
||
if (empty($email) || empty($password)) { | ||
$this->error = $this->module->l('Empty values provided', self::FILE_NAME); | ||
$this->message = $this->module->l('Email and password fields can not be empty', self::FILE_NAME); | ||
|
||
return; | ||
} | ||
|
||
if (!$this->validateEmail($email)) { | ||
$this->error = $this->module->l('Invalid email format', self::FILE_NAME); | ||
$this->message = $this->module->l('Invalid email address', self::FILE_NAME); | ||
|
||
return; | ||
} | ||
|
||
// Validate password | ||
if (!$this->validatePassword($password)) { | ||
$this->error = $this->module->l('Password length is invalid', self::FILE_NAME); | ||
$this->message = $this->module->l('Password must be between 6-255 characters long', self::FILE_NAME); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getError() | ||
{ | ||
return $this->error; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMessage() | ||
{ | ||
return $this->message; | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* @return array|false | ||
*/ | ||
private function validateEmail($email) | ||
{ | ||
return filter_var($email, FILTER_VALIDATE_EMAIL); | ||
} | ||
|
||
/** | ||
* @param string $password | ||
* @return bool | ||
*/ | ||
private function validatePassword($password) | ||
{ | ||
return strlen($password) >= 6 && strlen($password) <= 255; | ||
} | ||
} |