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;
|
2022-07-08 12:19:08 +02:00
|
|
|
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) {
|
2022-07-08 09:49:39 +02:00
|
|
|
$content = trim($this->order->getFieldData($phoneFieldName));
|
2022-07-04 13:28:58 +02:00
|
|
|
|
2022-07-08 09:49:39 +02:00
|
|
|
if (strlen($content)) {
|
2022-07-04 13:28:58 +02:00
|
|
|
$country = oxNew(Country::class);
|
2022-07-08 09:49:39 +02:00
|
|
|
$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
|
|
|
|
{
|
2022-07-08 12:19:08 +02:00
|
|
|
$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
|
2022-07-08 12:19:08 +02:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
});
|
2022-07-08 12:19:08 +02:00
|
|
|
|
|
|
|
$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
|
|
|
}
|