WPCRM fix phpcs issues

This commit is contained in:
Jérémy Munsch 2021-08-11 17:48:28 +02:00
parent 7b0b438ce3
commit 8dfd19988f
2 changed files with 48 additions and 46 deletions

View file

@ -16,6 +16,7 @@ namespace LDCCRM;
use Exception;
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// TABLE LDC CUSTOMERS
// CREATE TABLE `ldc_wp_customers` (
// `crm_id` BIGINT NOT NULL COMMENT 'Le CRM ID',
@ -40,9 +41,9 @@ use Exception;
class WPCrm extends ApiV1
{
private static $_sessionKey = 'LDC_CRM_USER';
private static $sessionKey = 'LDC_CRM_USER';
private static $_sessionDestroyed = false;
private static $sessionDestroyed = false;
/**
* The user object
@ -69,8 +70,8 @@ class WPCrm extends ApiV1
*/
public static function login($email, $password)
{
self::_bootSessionIfNeeded();
return self::_loginUserWithApi($email, $password);
self::bootSessionIfNeeded();
return self::loginUserWithApi($email, $password);
}
/**
@ -81,7 +82,7 @@ class WPCrm extends ApiV1
*/
public static function getUser()
{
self::_bootSessionIfNeeded();
self::bootSessionIfNeeded();
return self::$user;
}
@ -95,7 +96,7 @@ class WPCrm extends ApiV1
*/
public static function getUserFromApi($email)
{
self::_initLDCApi();
self::initLdcApi();
/**
* Object response
*
@ -103,6 +104,7 @@ class WPCrm extends ApiV1
*/
$response = self::$api->getCustomerByEmail($email);
if ($response->status === 'KO') {
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Found
error_log(
sprintf(
'LDC CRM: returned KO from CRM => %s',
@ -111,7 +113,7 @@ class WPCrm extends ApiV1
);
return $response->code;
}
self::_setUser(
self::setUser(
$response->user->crm_id,
$response->user->hashcode,
$response->user->email,
@ -129,9 +131,9 @@ class WPCrm extends ApiV1
*
* @return \LDCCRM\WPCrmUser|int
*/
private static function _loginUserWithApi($email, $password)
private static function loginUserWithApi($email, $password)
{
self::_initLDCApi();
self::initLdcApi();
/**
* Object response
*
@ -139,6 +141,7 @@ class WPCrm extends ApiV1
*/
$response = self::$api->loginCustomer($email, $password);
if ($response->status === 'KO') {
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Found
error_log(
sprintf(
'LDC CRM: returned KO from CRM => %s',
@ -147,13 +150,13 @@ class WPCrm extends ApiV1
);
return $response->code;
}
self::_setUser(
self::setUser(
$response->user->crm_id,
$response->user->hashcode,
$response->user->email,
$response
);
self::_saveOrUpdateUserToTable();
self::saveOrUpdateUserToTable();
return self::$user;
}
@ -165,10 +168,10 @@ class WPCrm extends ApiV1
*/
public static function logout()
{
self::_bootSessionIfNeeded();
unset($_SESSION[self::$_sessionKey]);
self::bootSessionIfNeeded();
unset($_SESSION[self::$sessionKey]);
session_destroy();
self::$_sessionDestroyed = true;
self::$sessionDestroyed = true;
}
/**
@ -178,8 +181,8 @@ class WPCrm extends ApiV1
*/
public static function check()
{
self::_bootSessionIfNeeded();
return (bool) self::getUser();
self::bootSessionIfNeeded();
return (bool)self::getUser();
}
/**
@ -194,14 +197,14 @@ class WPCrm extends ApiV1
*
* @return void
*/
private static function _setUser($crm_id, $hashcode, $email, $response)
private static function setUser($crm_id, $hashcode, $email, $response)
{
self::_bootSessionIfNeeded();
self::bootSessionIfNeeded();
if (!self::$user) {
self::$user = new WPCrmUser($crm_id, $hashcode, $email);
}
self::$user->setInfo($response->user);
$_SESSION[self::$_sessionKey] = json_encode(self::$user);
$_SESSION[self::$sessionKey] = json_encode(self::$user);
}
/**
@ -209,7 +212,7 @@ class WPCrm extends ApiV1
*
* @return \LDCCRM\ApiV1
*/
private static function _initLDCApi()
private static function initLdcApi()
{
if (!self::$api) {
if (!extension_loaded('curl')) {
@ -218,7 +221,7 @@ class WPCrm extends ApiV1
);
}
$endpoint = getenv('CRM_ENDPOINT');
$api_key = getenv('CRM_API_TOKEN');
$api_key = getenv('CRM_API_TOKEN');
if ($endpoint === false or $api_key === false) {
throw new Exception(
'LDC CRM require .env to have CRM_ENDPOINT and CRM_API_TOKEN'
@ -234,19 +237,19 @@ class WPCrm extends ApiV1
*
* @return void
*/
private static function _bootSessionIfNeeded()
private static function bootSessionIfNeeded()
{
if (!session_id() && !self::$_sessionDestroyed) {
if (!session_id() && !self::$sessionDestroyed) {
session_start();
}
if (!self::$user and array_key_exists(self::$_sessionKey, $_SESSION)) {
if (!self::$user and array_key_exists(self::$sessionKey, $_SESSION)) {
/**
* The user object
*
* @var \LDCCRM\WPCrmUser
*/
self::$user = WPCrmUser::parseFromJson(
$_SESSION[self::$_sessionKey] ?? null
$_SESSION[self::$sessionKey] ?? null
);
}
}
@ -260,7 +263,7 @@ class WPCrm extends ApiV1
*/
public static function getUserFromTable($crm_id)
{
return self::_getWpDB()->get_row(
return self::getWpDb()->get_row(
'SELECT * FROM `ldc_wp_customers` WHERE `crm_id` = ' . $crm_id
);
}
@ -274,7 +277,7 @@ class WPCrm extends ApiV1
*/
public static function getUserFromTableByEmail($email)
{
return self::_getWpDB()->get_row(
return self::getWpDb()->get_row(
'SELECT * FROM `ldc_wp_customers` WHERE `email` = "' . $email . '"'
);
}
@ -282,13 +285,11 @@ class WPCrm extends ApiV1
/**
* Get a customer from table
*
* @param int $crm_id The customer crm id
*
* @return object|null
*/
public static function getUserToTable($crm_id)
public static function getUserToTable()
{
return self::_getWpDB()->get_row(
return self::getWpDb()->get_row(
'SELECT * FROM `ldc_wp_customers` WHERE `crm_id` = ' .
self::$user->crm_id
);
@ -299,12 +300,11 @@ class WPCrm extends ApiV1
*
* @return void
*/
private static function _saveOrUpdateUserToTable()
private static function saveOrUpdateUserToTable()
{
if (self::getUserToTable(self::$user->crm_id)) {
self::_query(
'UPDATE `ldc_wp_customers` SET `hashcode` = %d, `email` = %d' .
' WHERE `crm_id` = %d',
if (self::getUserToTable()) {
self::query(
'UPDATE `ldc_wp_customers` SET `hashcode` = %d, `email` = %d WHERE `crm_id` = %d',
[
'crm_id' => self::$user->crm_id,
'hashcode' => self::$user->hashcode,
@ -312,7 +312,7 @@ class WPCrm extends ApiV1
]
);
} else {
self::_query(
self::query(
'INSERT INTO `ldc_wp_customers` VALUES (%d, %s, %s)',
[
'crm_id' => self::$user->crm_id,
@ -331,10 +331,10 @@ class WPCrm extends ApiV1
*
* @return object Containing the row columns
*/
private static function _query($query, array $params = [])
private static function query($query, array $params = [])
{
return self::_getWpDB()->query(
self::_getWpDB()->prepare($query, $params)
return self::getWpDb()->query(
self::getWpDb()->prepare($query, $params)
);
}
@ -343,8 +343,9 @@ class WPCrm extends ApiV1
*
* @return object The $wpdb variable
*/
private static function _getWpDB()
private static function getWpDb()
{
// phpcs:ignore Squiz.PHP.GlobalKeyword.NotAllowed
global $wpdb;
return $wpdb;
}

View file

@ -29,6 +29,7 @@ use JsonSerializable;
*/
class WPCrmUser implements JsonSerializable
{
/**
* The CRM ID
*
@ -66,9 +67,9 @@ class WPCrmUser implements JsonSerializable
*/
public function __construct($crm_id, $hashcode, $email)
{
$this->crm_id = $crm_id;
$this->crm_id = $crm_id;
$this->hashcode = $hashcode;
$this->email = $email;
$this->email = $email;
}
/**
@ -107,9 +108,9 @@ class WPCrmUser implements JsonSerializable
if (is_null($info) or is_null($info->email)) {
throw new Exception('Info cannot be null');
}
$this->info = $info;
$this->crm_id = $info->crm_id;
$this->email = $info->email;
$this->info = $info;
$this->crm_id = $info->crm_id;
$this->email = $info->email;
$this->hashcode = $info->hashcode;
}