linkmobility4oxid/src/Application/Model/OrderRecipients.php

95 lines
2.7 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\ValueObject\Recipient;
use OxidEsales\Eshop\Application\Model\Country;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\Registry;
2022-07-04 13:28:58 +02:00
class OrderRecipients
{
/**
* @var Order
*/
private $order;
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) {
$content = trim($this->order->getFieldData($phoneFieldName));
2022-07-04 13:28:58 +02:00
if (strlen($content)) {
2022-07-04 13:28:58 +02:00
$country = oxNew(Country::class);
$country->load($this->order->getFieldData($countryIdFieldName));
2022-07-04 13:28:58 +02:00
return oxNew(Recipient::class, $content, $country->getFieldData('oxisoalpha2'));
}
}
throw oxNew(noRecipientFoundException::class);
}
/**
* @return string[]
*/
public function getSmsRecipientFields(): array
{
$customFields = $this->getSanitizedCustomFields();
return count($customFields) ?
$customFields :
[
'oxdelfon' => 'oxdelcountryid',
'oxbillfon' => 'oxbillcountryid'
];
}
/**
* @return array
*/
2022-07-13 13:23:48 +02:00
public function getSanitizedCustomFields(): array
{
$customFields = (array) Registry::getConfig()->getConfigParam('d3linkmobility_smsOrderRecipientsFields');
array_walk($customFields, [$this, 'checkFieldExists']);
return array_filter($customFields);
}
public function checkFieldExists(&$checkPhoneFieldName, $checkCountryFieldName)
{
$checkCountryFieldName = trim($checkCountryFieldName);
$checkPhoneFieldName = trim($checkPhoneFieldName);
$allFieldNames = oxNew(Order::class)->getFieldNames();
2022-07-13 13:23:48 +02:00
array_walk($allFieldNames, function (&$value) {
$value = strtolower($value);
});
$checkPhoneFieldName = in_array(strtolower($checkPhoneFieldName), $allFieldNames) &&
in_array(strtolower($checkCountryFieldName), $allFieldNames) ? $checkPhoneFieldName : null;
2022-07-04 13:28:58 +02:00
}
2022-07-13 13:23:48 +02:00
}