linkmobility4oxid/src/Application/Model/OrderRecipients.php

116 lines
3.5 KiB
PHP
Raw Normal View History

2022-07-04 13:28:58 +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-04 13:28:58 +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-04 13:28:58 +02:00
*/
2022-07-13 13:21:52 +02:00
declare(strict_types=1);
2022-07-04 13:28:58 +02:00
namespace D3\Linkmobility4OXID\Application\Model;
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use D3\LinkmobilityClient\LoggerHandler;
2022-07-04 13:28:58 +02:00
use D3\LinkmobilityClient\ValueObject\Recipient;
use libphonenumber\NumberParseException;
2022-07-04 13:28:58 +02:00
use OxidEsales\Eshop\Application\Model\Country;
use OxidEsales\Eshop\Application\Model\Order;
class OrderRecipients
{
/**
* @var Order
*/
2023-01-05 22:43:58 +01:00
protected $order;
2022-07-04 13:28:58 +02:00
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* @return Recipient
* @throws noRecipientFoundException
*/
public function getSmsRecipient(): Recipient
{
2022-07-13 13:23:48 +02:00
foreach ($this->getSmsRecipientFields() as $phoneFieldName => $countryIdFieldName) {
2023-01-05 22:43:58 +01:00
if ($recipient = $this->getSmsRecipientByField($phoneFieldName, $countryIdFieldName)) {
return $recipient;
}
}
/** @var noRecipientFoundException $exc */
$exc = oxNew(noRecipientFoundException::class);
throw $exc;
}
/**
2023-01-10 23:33:48 +01:00
* @param string $phoneFieldName
* @param string $countryIdFieldName
2023-01-05 22:43:58 +01:00
* @return Recipient|null
*/
2023-01-10 23:33:48 +01:00
protected function getSmsRecipientByField(string $phoneFieldName, string $countryIdFieldName): ?Recipient
2023-01-05 22:43:58 +01:00
{
2023-01-10 23:33:48 +01:00
/** @var Country $country */
$country = d3GetOxidDIC()->get('d3ox.linkmobility.'.Country::class);
2023-01-05 22:43:58 +01:00
try {
2022-07-27 09:27:59 +02:00
/** @var string $content */
$content = $this->order->getFieldData($phoneFieldName) ?: '';
$content = trim($content);
2022-07-04 13:28:58 +02:00
2023-01-05 22:43:58 +01:00
if (strlen($content)) {
/** @var string $countryId */
$countryId = $this->order->getFieldData(trim($countryIdFieldName));
$country->load($countryId);
2023-01-15 22:24:49 +01:00
return $this->getRecipient($content, (string) $country->getFieldData('oxisoalpha2'));
2022-07-04 13:28:58 +02:00
}
2023-01-05 22:43:58 +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-05 22:43:58 +01:00
$e->getMessage(),
[$content, $country->getFieldData('oxisoalpha2')]
);
2022-07-04 13:28:58 +02:00
}
2023-01-05 22:43:58 +01:00
return null;
2022-07-04 13:28:58 +02:00
}
/**
* @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);
}
2022-07-04 13:28:58 +02:00
/**
* @return string[]
*/
public function getSmsRecipientFields(): array
{
2023-01-05 22:43:58 +01:00
/** @var Configuration $configuration */
$configuration = d3GetOxidDIC()->get(Configuration::class);
$customFields = $configuration->getOrderRecipientFields();
return count($customFields) ?
$customFields :
[
'oxdelfon' => 'oxdelcountryid',
'oxbillfon' => 'oxbillcountryid'
];
}
2022-07-13 13:23:48 +02:00
}