move configurations to conf class

This commit is contained in:
Daniel Seifert 2022-07-18 00:27:49 +02:00 committed by Daniel Seifert
parent 95249a247a
commit 1a896e26c2
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
4 changed files with 119 additions and 64 deletions

View File

@ -16,16 +16,34 @@ declare(strict_types=1);
namespace D3\Linkmobility4OXID\Application\Model; namespace D3\Linkmobility4OXID\Application\Model;
use Assert\Assert; use Assert\Assert;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
class Configuration 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 * @return string
*/ */
public function getApiToken(): 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(); Assert::that($token)->string()->notEmpty();
@ -37,7 +55,7 @@ class Configuration
*/ */
public function getTestMode(): bool 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() public function getSmsSenderNumber()
{ {
$number = trim(Registry::getConfig()->getConfigParam('d3linkmobility_smsSenderNumber')); $number = trim(Registry::getConfig()->getConfigParam(self::SMS_SENDERNR));
return strlen($number) ? $number : null; return strlen($number) ? $number : null;
} }
@ -55,11 +73,95 @@ class Configuration
*/ */
public function getSmsSenderCountry(): string 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; $country = strlen($country) ? strtoupper($country) : null;
Assert::that($country)->nullOr()->string()->length(2); Assert::that($country)->nullOr()->string()->length(2);
return $country; 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);
}
} }

View File

@ -19,7 +19,6 @@ use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms;
use Exception; use Exception;
use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\Registry;
class MessageSender class MessageSender
{ {
@ -30,7 +29,9 @@ class MessageSender
*/ */
public function sendOrderFinishedMessage(Order $order, $messageBody) 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) 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) 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 Order $order
* @param $messageBody * @param $messageBody
* @throws Exception * @throws Exception
*/ */
public function sendMessageByOrder($configParam, Order $order, $messageBody) public function sendMessageByOrder(Order $order, $messageBody)
{ {
if (false === (bool) Registry::getConfig()->getConfigParam($configParam) if ((bool) strlen(trim($messageBody)) === false) {
|| (bool) strlen(trim($messageBody)) === false
) {
return; return;
} }

View File

@ -58,7 +58,7 @@ class OrderRecipients
*/ */
public function getSmsRecipientFields(): array public function getSmsRecipientFields(): array
{ {
$customFields = $this->getSanitizedCustomFields(); $customFields = (oxNew(Configuration::class))->getOrderRecipientFields();
return count($customFields) ? return count($customFields) ?
$customFields : $customFields :
@ -67,28 +67,4 @@ class OrderRecipients
'oxbillfon' => 'oxbillcountryid' '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;
}
} }

View File

@ -57,7 +57,7 @@ class UserRecipients
*/ */
public function getSmsRecipientFields(): array public function getSmsRecipientFields(): array
{ {
$customFields = $this->getSanitizedCustomFields(); $customFields = (oxNew(Configuration::class))->getUserRecipientFields();
return count($customFields) ? return count($customFields) ?
$customFields : $customFields :
@ -67,29 +67,4 @@ class UserRecipients
'oxprivfon' '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;
}
} }