linkmobility4oxid/src/Application/Model/UserRecipients.php

117 lines
3.3 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;
2022-07-01 16:11:40 +02:00
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use D3\LinkmobilityClient\LoggerHandler;
2022-07-01 16:11:40 +02:00
use D3\LinkmobilityClient\ValueObject\Recipient;
use libphonenumber\NumberParseException;
2022-07-01 16:11:40 +02:00
use OxidEsales\Eshop\Application\Model\Country;
use OxidEsales\Eshop\Application\Model\User;
class UserRecipients
{
/**
* @var User
*/
2023-01-04 01:08:35 +01:00
protected $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-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);
2023-01-15 22:24:49 +01:00
return $this->getRecipient($content, (string) $country->getFieldData('oxisoalpha2'));
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;
}
/**
* @param string $content
* @param string $countryCode
* @throws NumberParseException
* @throws RecipientException
* @return Recipient
*/
protected function getRecipient(string $content, string $countryCode): Recipient
{
return oxNew(Recipient::class, $content, $countryCode);
}
/**
* @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();
return count($customFields) ?
$customFields :
[
'oxmobfon',
'oxfon',
'oxprivfon'
];
}
2022-07-13 13:23:48 +02:00
}