move writing remarks to a more centralized class

This commit is contained in:
Daniel Seifert 2022-07-15 22:46:52 +02:00
bovenliggende 0e6f66554e
commit 95249a247a
Getekend door: DanielS
GPG sleutel-ID: 6A513E13AEE66170
6 gewijzigde bestanden met toevoegingen van 145 en 118 verwijderingen

Bestand weergeven

@ -17,14 +17,13 @@ namespace D3\Linkmobility4OXID\Application\Controller\Admin;
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException;
use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms;
use D3\Linkmobility4OXID\Application\Model\OrderRecipients;
use D3\Linkmobility4OXID\Application\Model\Sms;
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\ValueObject\Recipient;
use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\Remark;
use OxidEsales\Eshop\Core\Registry;
class AdminOrder extends AdminController
@ -86,7 +85,6 @@ class AdminOrder extends AdminController
try {
$sms = oxNew(Sms::class, $messageBody);
if ($sms->sendOrderMessage($order)) {
$this->setRemark($sms->getRecipientsList(), $sms->getMessage());
Registry::getUtilsView()->addErrorToDisplay(
oxNew(successfullySentException::class, $sms->getResponse()->getSmsCount())
);
@ -103,21 +101,4 @@ class AdminOrder extends AdminController
Registry::getUtilsView()->addErrorToDisplay($e);
}
}
/**
* @param $recipients
* @param $messageBody
*
* @throws Exception
*/
protected function setRemark($recipients, $messageBody)
{
$remark = oxNew(Remark::class);
$remark->assign([
'oxtype' => AdminUser::REMARK_IDENT,
'oxparentid' => $this->order->getOrderUser()->getId(),
'oxtext' => $recipients.PHP_EOL.$messageBody
]);
$remark->save();
}
}

Bestand weergeven

@ -16,20 +16,17 @@ declare(strict_types=1);
namespace D3\Linkmobility4OXID\Application\Controller\Admin;
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\Linkmobility4OXID\Application\Model\Sms;
use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms;
use D3\Linkmobility4OXID\Application\Model\UserRecipients;
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\ValueObject\Recipient;
use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
use OxidEsales\Eshop\Application\Model\Remark;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
class AdminUser extends AdminController
{
public const REMARK_IDENT = 'LMSMS';
protected $_sThisTemplate = 'd3adminuser.tpl';
/**
@ -86,7 +83,6 @@ class AdminUser extends AdminController
$sms = oxNew(Sms::class, $messageBody);
if ($sms->sendUserAccountMessage($user)) {
$this->setRemark($sms->getRecipientsList(), $sms->getMessage());
Registry::getUtilsView()->addErrorToDisplay(
sprintf(
Registry::getLang()->translateString('D3LM_EXC_SMS_SUCC_SENT'),
@ -103,21 +99,4 @@ class AdminUser extends AdminController
);
}
}
/**
* @param $recipients
* @param $messageBody
*
* @throws Exception
*/
protected function setRemark($recipients, $messageBody)
{
$remark = oxNew(Remark::class);
$remark->assign([
'oxtype' => AdminUser::REMARK_IDENT,
'oxparentid' => $this->getEditObjectId(),
'oxtext' => $recipients.PHP_EOL.$messageBody
]);
$remark->save();
}
}

Bestand weergeven

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Linkmobility4OXID\Application\Model;
use D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser;
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms;
use Exception;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\Remark;
use OxidEsales\Eshop\Core\Registry;
class MessageSender
@ -26,6 +26,7 @@ class MessageSender
/**
* @param Order $order
* @param $messageBody
* @throws Exception
*/
public function sendOrderFinishedMessage(Order $order, $messageBody)
{
@ -35,6 +36,7 @@ class MessageSender
/**
* @param Order $order
* @param $messageBody
* @throws Exception
*/
public function sendSendedNowMessage(Order $order, $messageBody)
{
@ -44,6 +46,7 @@ class MessageSender
/**
* @param Order $order
* @param $messageBody
* @throws Exception
*/
public function sendCancelOrderMessage(Order $order, $messageBody)
{
@ -54,6 +57,7 @@ class MessageSender
* @param $configParam
* @param Order $order
* @param $messageBody
* @throws Exception
*/
public function sendMessageByOrder($configParam, Order $order, $messageBody)
{
@ -65,28 +69,8 @@ class MessageSender
try {
$sms = oxNew(Sms::class, $messageBody);
if ($sms->sendOrderMessage($order)) {
$this->setRemark($order->getId(), $sms->getRecipientsList(), $sms->getMessage());
}
$sms->sendOrderMessage($order);
} catch (noRecipientFoundException $e) {
}
}
/**
* @param $orderId
* @param $recipients
* @param $message
*
* @throws \Exception
*/
protected function setRemark($orderId, $recipients, $message)
{
$remark = oxNew(Remark::class);
$remark->assign([
'oxtype' => AdminUser::REMARK_IDENT,
'oxparentid' => $orderId,
'oxtext' => $recipients.PHP_EOL.$message
]);
$remark->save();
}
}

Bestand weergeven

@ -0,0 +1,113 @@
<?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\Linkmobility4OXID\Application\Model\MessageTypes;
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\ValueObject\Recipient;
use Exception;
use OxidEsales\Eshop\Application\Model\Remark;
abstract class AbstractMessage
{
const REMARK_IDENT = 'LINKMOB';
protected $message;
protected $removeLineBreaks = true;
protected $removeMultipleSpaces = true;
protected $response;
protected $recipients = [];
/**
* @param string $message
*/
public function __construct(string $message)
{
$this->message = $this->sanitizeMessage($message);
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
/**
* @param $userId
* @param $recipients
* @param $message
*
* @throws Exception
*/
protected function setRemark($userId, $recipients, $message)
{
$remark = oxNew(Remark::class);
$remark->assign([
'oxtype' => self::REMARK_IDENT,
'oxparentid' => $userId,
'oxtext' => $this->getTypeName().' -> '.$recipients.PHP_EOL.$message
]);
$remark->save();
}
/**
* @return ResponseInterface|null
*/
public function getResponse()
{
return $this->response;
}
/**
* @param array $recipients
* @return void
*/
protected function setRecipients(array $recipients)
{
$this->recipients = $recipients;
}
/**
* @return string
*/
public function getRecipientsList(): string
{
$list = [];
/** @var Recipient $recipient */
foreach ($this->recipients as $recipient) {
$list[] = $recipient->get();
}
return implode(', ', $list);
}
/**
* @param $message
*
* @return string
*/
protected function sanitizeMessage($message): string
{
$message = trim(strip_tags($message));
$message = $this->removeLineBreaks ? str_replace(["\r", "\n"], ' ', $message) : $message;
$regexp = '/\s{2,}/m';
return $this->removeMultipleSpaces ? preg_replace($regexp, ' ', $message) : $message;
}
abstract public function getTypeName() : string;
}

Bestand weergeven

@ -13,46 +13,46 @@
declare(strict_types=1);
namespace D3\Linkmobility4OXID\Application\Model;
namespace D3\Linkmobility4OXID\Application\Model\MessageTypes;
use D3\Linkmobility4OXID\Application\Model\Configuration;
use D3\Linkmobility4OXID\Application\Model\Exceptions\abortSendingExceptionInterface;
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\Linkmobility4OXID\Application\Model\MessageClient;
use D3\Linkmobility4OXID\Application\Model\OrderRecipients;
use D3\Linkmobility4OXID\Application\Model\RequestFactory;
use D3\Linkmobility4OXID\Application\Model\UserRecipients;
use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\Request\RequestInterface;
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\SMS\SmsRequestInterface;
use D3\LinkmobilityClient\ValueObject\Recipient;
use D3\LinkmobilityClient\ValueObject\Sender;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
class Sms
class Sms extends AbstractMessage
{
private $response;
private $recipients = [];
private $message;
protected $removeLineBreaks = true;
protected $removeMultipleSpaces = true;
public function __construct(string $message)
{
$this->message = $this->sanitizeMessage($message);
}
/**
* @param User $user
*
* @return bool
* @throws Exception
*/
public function sendUserAccountMessage(User $user): bool
{
try {
return $this->sendCustomRecipientMessage(
Registry::getLogger()->debug('startRequest', ['userId' => $user->getId()]);
$return = $this->sendCustomRecipientMessage(
[ oxNew(UserRecipients::class, $user)->getSmsRecipient() ]
);
if ($return) {
$this->setRemark($user->getId(), $this->getRecipientsList(), $this->getMessage());
}
Registry::getLogger()->debug('finishRequest', ['userId' => $user->getId()]);
return $return;
} catch (noRecipientFoundException $e) {
Registry::getLogger()->warning($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
@ -66,6 +66,7 @@ class Sms
*
* @return bool
* @throws noRecipientFoundException
* @throws Exception
*/
public function sendOrderMessage(Order $order): bool
{
@ -74,6 +75,9 @@ class Sms
$return = $this->sendCustomRecipientMessage(
[ oxNew(OrderRecipients::class, $order)->getSmsRecipient() ]
);
if ($return) {
$this->setRemark($order->getOrderUser()->getId(), $this->getRecipientsList(), $this->getMessage());
}
Registry::getLogger()->debug('finishRequest', ['orderId' => $order->getId()]);
return $return;
} catch (noRecipientFoundException $e) {
@ -138,44 +142,10 @@ class Sms
}
/**
* @return ResponseInterface|null
*/
public function getResponse()
{
return $this->response;
}
protected function setRecipients(array $recipients)
{
$this->recipients = $recipients;
}
public function getRecipientsList(): string
{
$list = [];
/** @var Recipient $recipient */
foreach ($this->recipients as $recipient) {
$list[] = $recipient->get();
}
return implode(', ', $list);
}
/**
* @param $message
*
* @return string
*/
protected function sanitizeMessage($message): string
public function getTypeName(): string
{
$message = trim(strip_tags($message));
$message = $this->removeLineBreaks ? str_replace(["\r", "\n"], ' ', $message) : $message;
$regexp = '/\s{2,}/m';
return $this->removeMultipleSpaces ? preg_replace($regexp, ' ', $message) : $message;
}
public function getMessage(): string
{
return $this->message;
return 'SMS';
}
}

Bestand weergeven

@ -1,4 +1,4 @@
[{if $allitem->oxremark__oxtype->value == "LMSMS"}]
[{if $allitem->oxremark__oxtype->value == "LINKMOB"}]
[{oxmultilang ident="D3LM_REMARK_SMS"}]
[{else}]
[{$smarty.block.parent}]