linkmobility4Ordermanager/src/Application/Model/d3linkmobility_ordermanager_sender.php

174 lines
5.9 KiB
PHP
Raw Normal View History

2022-08-05 00:43:08 +02:00
<?php
/**
* 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)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\Linkmobility4Ordermanager\Application\Model;
2022-08-07 00:45:00 +02:00
use D3\Linkmobility4Ordermanager\Application\Model\Exceptions\emptyMesageException;
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
2022-08-05 00:43:08 +02:00
use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms;
use D3\Linkmobility4OXID\Application\Model\OrderRecipients;
2022-08-07 01:14:43 +02:00
use D3\LinkmobilityClient\Exceptions\RecipientException;
2022-08-05 00:43:08 +02:00
use D3\LinkmobilityClient\ValueObject\Recipient;
2022-08-07 00:45:00 +02:00
use D3\ModCfg\Application\Model\Exception\d3ParameterNotFoundException;
2022-08-05 00:43:08 +02:00
use D3\Ordermanager\Application\Model\d3ordermanager;
2022-08-07 00:45:00 +02:00
use D3\Ordermanager\Application\Model\d3ordermanager as Manager;
2022-08-05 00:43:08 +02:00
use OxidEsales\Eshop\Application\Model\Order;
2022-08-07 00:45:00 +02:00
use OxidEsales\Eshop\Core\Exception\ArticleException;
use OxidEsales\Eshop\Core\Exception\ArticleInputException;
use OxidEsales\Eshop\Core\Exception\StandardException;
2022-08-07 00:45:00 +02:00
use OxidEsales\Eshop\Core\Registry;
2022-08-05 00:43:08 +02:00
class d3linkmobility_ordermanager_sender
{
/** @var d3ordermanager */
protected $manager;
/** @var Order */
protected $item;
2022-08-07 00:45:00 +02:00
/**
* @param Manager $manager
* @param Order $item
* @return void
* @throws ArticleException
* @throws ArticleInputException
* @throws StandardException
2022-08-07 00:45:00 +02:00
* @throws d3ParameterNotFoundException
* @throws emptyMesageException
*/
2022-08-05 00:43:08 +02:00
public function sendOrderManagerSms(d3ordermanager $manager, Order $item)
{
$this->setManager($manager);
$this->setItem($item);
2022-08-05 00:43:08 +02:00
$sms = oxNew(Sms::class, $this->getMessageBody());
$sms->sendCustomRecipientMessage($this->getRecipients());
}
/**
* @return string
2022-08-07 00:45:00 +02:00
* @throws d3ParameterNotFoundException
* @throws emptyMesageException
2022-08-05 00:43:08 +02:00
*/
protected function getMessageBody(): string
{
$generator = oxNew(MessageContentGenerator::class, $this->getManager(), $this->getManager()->getCurrentItem());
2022-08-07 00:45:00 +02:00
if ($this->getManager()->getValue('sLinkMobilityMessageFromSource') == 'cms') {
return $generator->generateFromCms($this->getManager()->getValue('sLinkMobilityMessageFromContentname'));
2022-08-07 00:45:00 +02:00
} elseif ($this->getManager()->getValue('sLinkMobilityMessageFromSource') == 'template') {
$fromTheme = $this->getManager()->getValue('sLinkMobilityMessageFromTheme');
$generator->setTemplateFrom(
$fromTheme === 'admin' ? MessageContentGenerator::TEMPLATE_FROM_ADMIN :
($fromTheme === 'module' ? MessageContentGenerator::TEMPLATE_FROM_MODULE :
MessageContentGenerator::TEMPLATE_FROM_FRONTEND),
$this->getManager()->getValue('sLinkMobilityMessageFromModulePath')
2022-08-07 00:45:00 +02:00
);
return $generator->generateFromTpl($this->getManager()->getValue('sLinkMobilityMessageFromTemplatename'));
2022-08-07 00:45:00 +02:00
}
throw oxNew(emptyMesageException::class);
2022-08-05 00:43:08 +02:00
}
/**
* @return array
* @throws StandardException
* @throws d3ParameterNotFoundException
2022-08-05 00:43:08 +02:00
*/
protected function getRecipients(): array
{
$recipients = [];
2022-08-07 00:45:00 +02:00
if ((bool) $this->getManager()->getValue('blLinkMobilityMessageToCustomer')) {
2022-08-07 01:14:43 +02:00
try {
$recipients[] = (oxNew(OrderRecipients::class, $this->getItem()))->getSmsRecipient();
} catch (noRecipientFoundException $e) {
2022-08-07 22:52:00 +02:00
/** @var string $note */
$note = Registry::getLang()->translateString('D3_ORDERMANAGER_JOBDESC_SENDLMMESSAGE_NORECIPIENT', null, true);
$this->getManager()->getRemarkHandler()->addNote($note);
}
2022-08-05 00:43:08 +02:00
}
2022-08-07 00:45:00 +02:00
if ((bool) $this->getManager()->getValue('blLinkMobilityMessageToCustom') &&
strlen(trim($this->getManager()->getValue('sLinkMobilityMessageToCustomAddress')))
) {
foreach ($this->extractCustomAddresses() as $phoneNumber => $countryId) {
2022-08-07 01:14:43 +02:00
try {
$recipients[] = oxNew(Recipient::class, $phoneNumber, $countryId);
} catch (RecipientException $e) {
Registry::getLogger()->info($e->getMessage(), [$phoneNumber, $countryId]);
2022-08-07 22:52:00 +02:00
/** @var string $format */
$format = Registry::getLang()->translateString(
'D3_ORDERMANAGER_JOBDESC_SENDLMMESSAGE_RECIPIENTERROR',
null,
true
);
2022-08-07 22:52:00 +02:00
$this->getManager()->getRemarkHandler()->addNote(sprintf($format, $phoneNumber, $countryId));
2022-08-07 01:14:43 +02:00
}
2022-08-07 00:45:00 +02:00
}
2022-08-05 00:43:08 +02:00
}
return $recipients;
}
2022-08-07 00:45:00 +02:00
/**
* @return array
*/
protected function extractCustomAddresses(): array
{
$addresses = [];
$customAddresses = trim($this->getManager()->getValue('sLinkMobilityMessageToCustomAddress'));
if (strlen($customAddresses)) {
foreach (explode(';', $customAddresses) as $addressGroups) {
2022-08-07 22:18:23 +02:00
[$phoneNumber, $countryId] = explode('@', trim($addressGroups));
2022-08-07 00:45:00 +02:00
$addresses[trim($phoneNumber)] = trim($countryId);
}
}
return $addresses;
}
2022-08-05 00:43:08 +02:00
/**
* @return Order
*/
public function getItem(): Order
{
return $this->item;
}
/**
* @param Order $item
*/
public function setItem(Order $item): void
{
$this->item = $item;
}
/**
* @param d3ordermanager $manager
*/
public function setManager(d3ordermanager $manager): void
{
$this->manager = $manager;
}
/**
* @return d3ordermanager
*/
public function getManager(): d3ordermanager
{
return $this->manager;
}
2022-08-07 22:18:23 +02:00
}