2022-07-01 11:48:44 +02:00
|
|
|
<?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
|
2022-07-01 11:48:44 +02:00
|
|
|
*
|
|
|
|
* @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-01 11:48:44 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-13 13:21:52 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-07-01 11:48:44 +02:00
|
|
|
namespace D3\Linkmobility4OXID\Application\Model;
|
|
|
|
|
2022-07-01 16:11:40 +02:00
|
|
|
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
|
2022-07-18 13:25:26 +02:00
|
|
|
use D3\LinkmobilityClient\Exceptions\RecipientException;
|
|
|
|
use D3\LinkmobilityClient\LoggerHandler;
|
2022-07-01 16:11:40 +02:00
|
|
|
use D3\LinkmobilityClient\ValueObject\Recipient;
|
2022-07-18 13:25:26 +02:00
|
|
|
use libphonenumber\NumberParseException;
|
2022-07-01 16:11:40 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\Country;
|
2022-07-01 11:48:44 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
|
|
|
|
class UserRecipients
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
2023-01-04 01:08:35 +01:00
|
|
|
protected $user;
|
2022-07-01 11:48:44 +02:00
|
|
|
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:11:40 +02:00
|
|
|
/**
|
|
|
|
* @return Recipient
|
|
|
|
* @throws noRecipientFoundException
|
|
|
|
*/
|
|
|
|
public function getSmsRecipient(): Recipient
|
2022-07-01 11:48:44 +02:00
|
|
|
{
|
2022-07-13 13:23:48 +02:00
|
|
|
foreach ($this->getSmsRecipientFields() as $fieldName) {
|
2023-01-04 01:08:35 +01:00
|
|
|
if ($recipient = $this->getSmsRecipientByField($fieldName)) {
|
|
|
|
return $recipient;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var noRecipientFoundException $exc */
|
|
|
|
$exc = d3GetOxidDIC()->get(noRecipientFoundException::class);
|
|
|
|
throw $exc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-10 23:33:48 +01:00
|
|
|
* @param string $fieldName
|
2023-01-04 01:08:35 +01:00
|
|
|
* @return Recipient|null
|
|
|
|
*/
|
2023-01-10 23:33:48 +01:00
|
|
|
protected function getSmsRecipientByField(string $fieldName): ?Recipient
|
2023-01-04 01:08:35 +01:00
|
|
|
{
|
2023-01-10 23:33:48 +01:00
|
|
|
/** @var Country $country */
|
|
|
|
$country = d3GetOxidDIC()->get('d3ox.linkmobility.'.Country::class);
|
|
|
|
|
2023-01-04 01:08:35 +01:00
|
|
|
try {
|
2022-07-27 09:27:59 +02:00
|
|
|
/** @var string $content */
|
|
|
|
$content = $this->user->getFieldData($fieldName) ?: '';
|
|
|
|
$content = trim($content);
|
2022-07-01 16:11:40 +02:00
|
|
|
|
2023-01-04 01:08:35 +01:00
|
|
|
if (strlen($content)) {
|
|
|
|
/** @var string $countryId */
|
|
|
|
$countryId = $this->user->getFieldData('oxcountryid');
|
|
|
|
$country->load($countryId);
|
|
|
|
d3GetOxidDIC()->setParameter(Recipient::class.'.args.number', $content);
|
|
|
|
d3GetOxidDIC()->setParameter(Recipient::class.'.args.iso2countrycode', $country->getFieldData('oxisoalpha2'));
|
|
|
|
/** @var Recipient $recipient */
|
|
|
|
$recipient = d3GetOxidDIC()->get(Recipient::class);
|
|
|
|
return $recipient;
|
2022-07-01 16:11:40 +02:00
|
|
|
}
|
2023-01-04 01:08:35 +01:00
|
|
|
} catch (NumberParseException|RecipientException $e) {
|
2023-01-10 23:33:48 +01:00
|
|
|
/** @var LoggerHandler $loggerHandler */
|
|
|
|
$loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class);
|
|
|
|
$loggerHandler->getLogger()->info(
|
2023-01-04 01:08:35 +01:00
|
|
|
$e->getMessage(),
|
|
|
|
[$content, $country->getFieldData('oxisoalpha2')]
|
|
|
|
);
|
2022-07-01 16:11:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 01:08:35 +01:00
|
|
|
return null;
|
2022-07-01 11:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getSmsRecipientFields(): array
|
|
|
|
{
|
2023-01-04 01:08:35 +01:00
|
|
|
/** @var Configuration $configuration */
|
|
|
|
$configuration = d3GetOxidDIC()->get(Configuration::class);
|
|
|
|
$customFields = $configuration->getUserRecipientFields();
|
2022-07-08 12:19:08 +02:00
|
|
|
|
|
|
|
return count($customFields) ?
|
|
|
|
$customFields :
|
|
|
|
[
|
|
|
|
'oxmobfon',
|
|
|
|
'oxfon',
|
|
|
|
'oxprivfon'
|
|
|
|
];
|
|
|
|
}
|
2022-07-13 13:23:48 +02:00
|
|
|
}
|