move configurations to conf class

Cette révision appartient à :
Daniel Seifert 2022-07-18 00:27:49 +02:00 révisé par Daniel Seifert
Parent 95249a247a
révision 1a896e26c2
Signé par: DanielS
ID de la clé GPG: 8A7C4C6ED1915C6F
4 fichiers modifiés avec 119 ajouts et 64 suppressions

Voir le fichier

@ -16,16 +16,34 @@ declare(strict_types=1);
namespace D3\Linkmobility4OXID\Application\Model;
use Assert\Assert;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
class Configuration
{
const GENERAL_APITOKEN = "d3linkmobility_apitoken";
const GENERAL_DEBUG = "d3linkmobility_debug";
const ORDER_RECFIELDS = "d3linkmobility_smsOrderRecipientsFields";
const USER_RECFIELDS = "d3linkmobility_smsUserRecipientsFields";
const SMS_SENDERNR = "d3linkmobility_smsSenderNumber";
const SMS_SENDERCOUNTRY = "d3linkmobility_smsSenderCountry";
const SENDBY_ORDERED = "d3linkmobility_orderActive";
const SENDBY_SENDEDNOW = "d3linkmobility_sendedNowActive";
const SENDBY_CANCELED = "d3linkmobility_cancelOrderActive";
const ARGS_CHECKKEYS = "checkKeys";
const ARGS_CHECKCLASS = "checkClassName";
/**
* @return string
*/
public function getApiToken(): string
{
$token = trim(Registry::getConfig()->getConfigParam('d3linkmobility_apitoken'));
$token = trim(Registry::getConfig()->getConfigParam(self::GENERAL_APITOKEN));
Assert::that($token)->string()->notEmpty();
@ -37,7 +55,7 @@ class Configuration
*/
public function getTestMode(): bool
{
return (bool) Registry::getConfig()->getConfigParam('d3linkmobility_debug');
return (bool) Registry::getConfig()->getConfigParam(self::GENERAL_DEBUG);
}
/**
@ -45,7 +63,7 @@ class Configuration
*/
public function getSmsSenderNumber()
{
$number = trim(Registry::getConfig()->getConfigParam('d3linkmobility_smsSenderNumber'));
$number = trim(Registry::getConfig()->getConfigParam(self::SMS_SENDERNR));
return strlen($number) ? $number : null;
}
@ -55,11 +73,95 @@ class Configuration
*/
public function getSmsSenderCountry(): string
{
$country = trim(Registry::getConfig()->getConfigParam('d3linkmobility_smsSenderCountry'));
$country = trim(Registry::getConfig()->getConfigParam(self::SMS_SENDERCOUNTRY));
$country = strlen($country) ? strtoupper($country) : null;
Assert::that($country)->nullOr()->string()->length(2);
return $country;
}
/**
* @return array
*/
public function getOrderRecipientFields(): array
{
$customFields = Registry::getConfig()->getConfigParam(self::ORDER_RECFIELDS);
array_walk(
$customFields,
[$this, 'checkFieldExists'],
[self::ARGS_CHECKKEYS => true, self::ARGS_CHECKCLASS => Order::class]
);
$customFields = array_filter($customFields);
Assert::that($customFields)->isArray();
return $customFields;
}
/**
* @return array
*/
public function getUserRecipientFields(): array
{
$customFields = Registry::getConfig()->getConfigParam(self::USER_RECFIELDS);
array_walk(
$customFields,
[$this, 'checkFieldExists'],
[self::ARGS_CHECKKEYS => false, self::ARGS_CHECKCLASS => User::class]
);
$customFields = array_filter($customFields);
Assert::that($customFields)->isArray();
return $customFields;
}
/**
* @param $checkPhoneFieldName
* @param $checkCountryFieldName
* @param $args
* @return void
*/
protected function checkFieldExists(&$checkPhoneFieldName, $checkCountryFieldName, $args)
{
$checkCountryFieldName = $args[self::ARGS_CHECKKEYS] ? trim($checkCountryFieldName) : $checkCountryFieldName;
$checkPhoneFieldName = trim($checkPhoneFieldName);
$allFieldNames = oxNew($args[self::ARGS_CHECKCLASS])->getFieldNames();
array_walk($allFieldNames, function (&$value) {
$value = strtolower($value);
});
$checkPhoneFieldName = in_array(strtolower($checkPhoneFieldName), $allFieldNames) && (
false === $args[self::ARGS_CHECKKEYS] ||
in_array(strtolower($checkCountryFieldName), $allFieldNames)
) ? $checkPhoneFieldName : null;
}
/**
* @return bool
*/
public function sendOrderFinishedMessage(): bool
{
return (bool) Registry::getConfig()->getConfigParam(self::SENDBY_ORDERED);
}
/**
* @return bool
*/
public function sendOrderSendedNowMessage(): bool
{
return (bool) Registry::getConfig()->getConfigParam(self::SENDBY_SENDEDNOW);
}
/**
* @return bool
*/
public function sendOrderCanceledMessage(): bool
{
return (bool) Registry::getConfig()->getConfigParam(self::SENDBY_CANCELED);
}
}

Voir le fichier

@ -19,7 +19,6 @@ use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms;
use Exception;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\Registry;
class MessageSender
{
@ -30,7 +29,9 @@ class MessageSender
*/
public function sendOrderFinishedMessage(Order $order, $messageBody)
{
$this->sendMessageByOrder('d3linkmobility_orderActive', $order, $messageBody);
if ((oxNew(Configuration::class))->sendOrderFinishedMessage()) {
$this->sendMessageByOrder( $order, $messageBody);
}
}
/**
@ -40,7 +41,9 @@ class MessageSender
*/
public function sendSendedNowMessage(Order $order, $messageBody)
{
$this->sendMessageByOrder('d3linkmobility_sendedNowActive', $order, $messageBody);
if ((oxNew(Configuration::class))->sendOrderSendedNowMessage()) {
$this->sendMessageByOrder($order, $messageBody);
}
}
/**
@ -50,20 +53,19 @@ class MessageSender
*/
public function sendCancelOrderMessage(Order $order, $messageBody)
{
$this->sendMessageByOrder('d3linkmobility_cancelOrderActive', $order, $messageBody);
if ((oxNew(Configuration::class))->sendOrderCanceledMessage()) {
$this->sendMessageByOrder($order, $messageBody);
}
}
/**
* @param $configParam
* @param Order $order
* @param $messageBody
* @throws Exception
*/
public function sendMessageByOrder($configParam, Order $order, $messageBody)
public function sendMessageByOrder(Order $order, $messageBody)
{
if (false === (bool) Registry::getConfig()->getConfigParam($configParam)
|| (bool) strlen(trim($messageBody)) === false
) {
if ((bool) strlen(trim($messageBody)) === false) {
return;
}

Voir le fichier

@ -58,7 +58,7 @@ class OrderRecipients
*/
public function getSmsRecipientFields(): array
{
$customFields = $this->getSanitizedCustomFields();
$customFields = (oxNew(Configuration::class))->getOrderRecipientFields();
return count($customFields) ?
$customFields :
@ -67,28 +67,4 @@ class OrderRecipients
'oxbillfon' => 'oxbillcountryid'
];
}
/**
* @return array
*/
public function getSanitizedCustomFields(): array
{
$customFields = (array) Registry::getConfig()->getConfigParam('d3linkmobility_smsOrderRecipientsFields');
array_walk($customFields, [$this, 'checkFieldExists']);
return array_filter($customFields);
}
public function checkFieldExists(&$checkPhoneFieldName, $checkCountryFieldName)
{
$checkCountryFieldName = trim($checkCountryFieldName);
$checkPhoneFieldName = trim($checkPhoneFieldName);
$allFieldNames = oxNew(Order::class)->getFieldNames();
array_walk($allFieldNames, function (&$value) {
$value = strtolower($value);
});
$checkPhoneFieldName = in_array(strtolower($checkPhoneFieldName), $allFieldNames) &&
in_array(strtolower($checkCountryFieldName), $allFieldNames) ? $checkPhoneFieldName : null;
}
}

Voir le fichier

@ -57,7 +57,7 @@ class UserRecipients
*/
public function getSmsRecipientFields(): array
{
$customFields = $this->getSanitizedCustomFields();
$customFields = (oxNew(Configuration::class))->getUserRecipientFields();
return count($customFields) ?
$customFields :
@ -67,29 +67,4 @@ class UserRecipients
'oxprivfon'
];
}
/**
* @return array
*/
public function getSanitizedCustomFields(): array
{
$customFields = (array) Registry::getConfig()->getConfigParam('d3linkmobility_smsUserRecipientsFields');
array_walk($customFields, [$this, 'checkFieldExists']);
return array_filter($customFields);
}
/**
* @param $checkFieldName
*/
public function checkFieldExists(&$checkFieldName)
{
$checkFieldName = trim($checkFieldName);
$allFieldNames = oxNew(User::class)->getFieldNames();
array_walk($allFieldNames, function (&$value) {
$value = strtolower($value);
});
$checkFieldName = in_array(strtolower($checkFieldName), $allFieldNames) ? $checkFieldName : null;
}
}