linkmobility4oxid/src/Application/Model/Configuration.php

208 lines
5.8 KiB
PHP
Raw Normal View History

<?php
/**
2022-07-13 13:21:52 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-07-13 13:21:52 +02:00
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
2022-07-13 13:21:52 +02:00
declare(strict_types=1);
namespace D3\Linkmobility4OXID\Application\Model;
use Assert\Assert;
2022-07-18 00:27:49 +02:00
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\User;
2023-01-06 23:42:51 +01:00
use OxidEsales\Eshop\Core\Config;
class Configuration
{
2022-07-27 08:47:20 +02:00
public const GENERAL_APITOKEN = "d3linkmobility_apitoken";
public const GENERAL_DEBUG = "d3linkmobility_debug";
2022-07-18 00:27:49 +02:00
2022-07-27 08:47:20 +02:00
public const ORDER_RECFIELDS = "d3linkmobility_smsOrderRecipientsFields";
public const USER_RECFIELDS = "d3linkmobility_smsUserRecipientsFields";
2022-07-18 00:27:49 +02:00
2022-07-27 08:47:20 +02:00
public const SMS_SENDERNR = "d3linkmobility_smsSenderNumber";
public const SMS_SENDERCOUNTRY = "d3linkmobility_smsSenderCountry";
2022-07-18 00:27:49 +02:00
2022-07-27 08:47:20 +02:00
public const SENDBY_ORDERED = "d3linkmobility_orderActive";
public const SENDBY_SENDEDNOW = "d3linkmobility_sendedNowActive";
public const SENDBY_CANCELED = "d3linkmobility_cancelOrderActive";
2022-07-18 00:27:49 +02:00
2022-07-27 08:47:20 +02:00
public const ARGS_CHECKKEYS = "checkKeys";
public const ARGS_CHECKCLASS = "checkClassName";
2022-07-18 00:27:49 +02:00
/**
* @return string
*/
public function getApiToken(): string
{
2022-07-27 09:27:59 +02:00
/** @var string $token */
2023-01-06 23:42:51 +01:00
$token = $this->getConfig()->getConfigParam(self::GENERAL_APITOKEN);
Assert::that($token)->string();
2022-07-27 09:27:59 +02:00
$token = trim($token);
2023-01-06 23:42:51 +01:00
Assert::that($token)->notEmpty();
return $token;
}
/**
* @return bool
*/
public function getTestMode(): bool
{
2023-01-06 23:42:51 +01:00
return (bool) $this->getConfig()->getConfigParam(self::GENERAL_DEBUG);
}
/**
* @return string|null
*/
2022-12-28 00:11:00 +01:00
public function getSmsSenderNumber(): ?string
{
2022-07-27 09:27:59 +02:00
/** @var string $number */
2023-01-06 23:42:51 +01:00
$number = $this->getConfig()->getConfigParam(self::SMS_SENDERNR);
Assert::that($number)->string();
2022-07-27 09:27:59 +02:00
$number = trim($number);
return strlen($number) ? $number : null;
}
/**
* @return string|null
*/
2022-07-27 09:27:59 +02:00
public function getSmsSenderCountry(): ?string
{
2022-07-27 09:27:59 +02:00
/** @var string $country */
2023-01-06 23:42:51 +01:00
$country = $this->getConfig()->getConfigParam(self::SMS_SENDERCOUNTRY);
Assert::that($country)->string();
2022-07-27 09:27:59 +02:00
$country = trim($country);
$country = strlen($country) ? strtoupper($country) : null;
2022-07-14 16:04:26 +02:00
Assert::that($country)->nullOr()->string()->length(2);
return $country;
}
2022-07-18 00:27:49 +02:00
/**
2022-07-27 09:27:59 +02:00
* @return string[]
2022-07-18 00:27:49 +02:00
*/
public function getOrderRecipientFields(): array
{
2022-07-27 09:27:59 +02:00
/** @var string[] $customFields */
2023-01-06 23:42:51 +01:00
$customFields = $this->getConfig()->getConfigParam(self::ORDER_RECFIELDS) ?: [];
Assert::that($customFields)->isArray();
2022-07-18 00:27:49 +02:00
array_walk(
$customFields,
[$this, 'checkFieldExists'],
[self::ARGS_CHECKKEYS => true, self::ARGS_CHECKCLASS => Order::class]
);
2023-01-06 23:42:51 +01:00
// remove all false values and trim keys
return array_filter($this->sanitizeKeys($customFields));
2022-07-18 00:27:49 +02:00
}
/**
2022-07-27 09:27:59 +02:00
* @return string[]
2022-07-18 00:27:49 +02:00
*/
public function getUserRecipientFields(): array
{
2022-07-27 09:27:59 +02:00
/** @var string[] $customFields */
2023-01-06 23:42:51 +01:00
$customFields = $this->getConfig()->getConfigParam(self::USER_RECFIELDS) ?: [];
Assert::that($customFields)->isArray();
2022-07-18 00:27:49 +02:00
array_walk(
$customFields,
[$this, 'checkFieldExists'],
[self::ARGS_CHECKKEYS => false, self::ARGS_CHECKCLASS => User::class]
);
2023-01-06 23:42:51 +01:00
// remove all false values
return array_filter($customFields);
}
2022-07-18 00:27:49 +02:00
2023-01-06 23:42:51 +01:00
/**
2023-01-10 23:33:48 +01:00
* @param string[] $customFields
* @return string[]
2023-01-06 23:42:51 +01:00
*/
public function sanitizeKeys(array $customFields): array
{
foreach ($customFields as $key => $value) {
$sanitizedKey = trim($key);
if ($key !== $sanitizedKey) {
$customFields[$sanitizedKey] = $value;
unset($customFields[$key]);
}
}
2022-07-18 00:27:49 +02:00
return $customFields;
}
/**
2022-07-27 09:27:59 +02:00
* @template T
* @param string $checkPhoneFieldName
* @param string $checkCountryFieldName
* @param array{checkKeys: bool, checkClassName: class-string<T>} $args
2022-07-18 00:27:49 +02:00
* @return void
*/
2022-07-27 09:27:59 +02:00
protected function checkFieldExists(string &$checkPhoneFieldName, string $checkCountryFieldName, array $args): void
2022-07-18 00:27:49 +02:00
{
$checkCountryFieldName = $args[self::ARGS_CHECKKEYS] ? trim($checkCountryFieldName) : $checkCountryFieldName;
$checkPhoneFieldName = trim($checkPhoneFieldName);
2023-01-06 23:42:51 +01:00
2022-12-15 08:38:52 +01:00
$allFieldNames = oxNew($args[self::ARGS_CHECKCLASS])->getFieldNames() ?: [];
2022-07-18 00:27:49 +02:00
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
{
2023-01-06 23:42:51 +01:00
return (bool) $this->getConfig()->getConfigParam(self::SENDBY_ORDERED);
2022-07-18 00:27:49 +02:00
}
/**
* @return bool
*/
public function sendOrderSendedNowMessage(): bool
{
2023-01-06 23:42:51 +01:00
return (bool) $this->getConfig()->getConfigParam(self::SENDBY_SENDEDNOW);
2022-07-18 00:27:49 +02:00
}
/**
* @return bool
*/
public function sendOrderCanceledMessage(): bool
{
2023-01-06 23:42:51 +01:00
return (bool) $this->getConfig()->getConfigParam(self::SENDBY_CANCELED);
}
/**
* @return Config
*/
protected function getConfig(): Config
{
/** @var Config $config */
$config = d3GetOxidDIC()->get('d3ox.linkmobility.'.Config::class);
return $config;
2022-07-18 00:27:49 +02:00
}
2022-07-13 13:23:48 +02:00
}