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
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
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) {
|
2022-07-27 09:27:59 +02:00
|
|
|
/** @var string $content */
|
|
|
|
$content = $this->user->getFieldData($fieldName) ?: '';
|
|
|
|
$content = trim($content);
|
2022-07-27 08:47:20 +02:00
|
|
|
$country = oxNew(Country::class);
|
2022-07-01 16:11:40 +02:00
|
|
|
|
2022-07-18 13:25:26 +02:00
|
|
|
try {
|
2022-07-27 08:47:20 +02:00
|
|
|
if (strlen($content)) {
|
2022-07-27 09:27:59 +02:00
|
|
|
/** @var string $countryId */
|
|
|
|
$countryId = $this->user->getFieldData('oxcountryid');
|
|
|
|
$country->load($countryId);
|
2022-07-27 08:47:20 +02:00
|
|
|
return oxNew(Recipient::class, $content, $country->getFieldData('oxisoalpha2'));
|
2022-07-18 13:25:26 +02:00
|
|
|
}
|
|
|
|
} catch (NumberParseException $e) {
|
2022-07-27 08:47:20 +02:00
|
|
|
LoggerHandler::getInstance()->getLogger()->info($e->getMessage(), [$content, $country->getFieldData('oxisoalpha2')]);
|
2022-07-18 13:25:26 +02:00
|
|
|
} catch (RecipientException $e) {
|
2022-07-27 08:47:20 +02:00
|
|
|
LoggerHandler::getInstance()->getLogger()->info($e->getMessage(), [$content, $country->getFieldData('oxisoalpha2')]);
|
2022-07-01 16:11:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 11:57:58 +02:00
|
|
|
/** @var noRecipientFoundException $exc */
|
|
|
|
$exc = oxNew(noRecipientFoundException::class);
|
|
|
|
throw $exc;
|
2022-07-01 11:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getSmsRecipientFields(): array
|
|
|
|
{
|
2022-07-18 00:27:49 +02:00
|
|
|
$customFields = (oxNew(Configuration::class))->getUserRecipientFields();
|
2022-07-08 12:19:08 +02:00
|
|
|
|
|
|
|
return count($customFields) ?
|
|
|
|
$customFields :
|
|
|
|
[
|
|
|
|
'oxmobfon',
|
|
|
|
'oxfon',
|
|
|
|
'oxprivfon'
|
|
|
|
];
|
|
|
|
}
|
2022-07-13 13:23:48 +02:00
|
|
|
}
|