diff --git a/src/Application/Model/Configuration.php b/src/Application/Model/Configuration.php index 9da0a7c..68c5143 100644 --- a/src/Application/Model/Configuration.php +++ b/src/Application/Model/Configuration.php @@ -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); + } } diff --git a/src/Application/Model/MessageSender.php b/src/Application/Model/MessageSender.php index 8aa7c18..43bd2ee 100644 --- a/src/Application/Model/MessageSender.php +++ b/src/Application/Model/MessageSender.php @@ -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; } diff --git a/src/Application/Model/OrderRecipients.php b/src/Application/Model/OrderRecipients.php index 41e833f..4dc951e 100644 --- a/src/Application/Model/OrderRecipients.php +++ b/src/Application/Model/OrderRecipients.php @@ -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; - } } diff --git a/src/Application/Model/UserRecipients.php b/src/Application/Model/UserRecipients.php index 3d72c0f..5901868 100644 --- a/src/Application/Model/UserRecipients.php +++ b/src/Application/Model/UserRecipients.php @@ -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; - } }