linkmobility4oxid/src/Application/Model/Sms.php

187 lines
6.0 KiB
PHP
Raw Normal View History

<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
*/
namespace D3\Linkmobility4OXID\Application\Model;
2022-07-01 16:11:40 +02:00
use D3\Linkmobility4OXID\Application\Model\Exceptions\abortSendingExceptionInterface;
2022-07-04 09:13:36 +02:00
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
2022-07-04 16:33:49 +02:00
use D3\Linkmobility4OXID\Application\Model\RequestFactory;
2022-07-01 16:11:40 +02:00
use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\Request\RequestInterface;
2022-07-04 09:13:36 +02:00
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\SMS\SmsRequestInterface;
use D3\LinkmobilityClient\ValueObject\Recipient;
use D3\LinkmobilityClient\ValueObject\Sender;
2022-07-01 16:11:40 +02:00
use GuzzleHttp\Exception\GuzzleException;
2022-07-04 13:28:58 +02:00
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Application\Model\User;
2022-07-01 16:11:40 +02:00
use OxidEsales\Eshop\Core\Registry;
class Sms
{
2022-07-04 09:13:36 +02:00
private $response;
private $recipients = [];
private $message;
protected $removeLineBreaks = true;
protected $removeMultipleSpaces = true;
public function __construct(string $message)
{
$this->message = $this->sanitizeMessage($message);
}
2022-07-04 09:13:36 +02:00
2022-07-01 16:11:40 +02:00
/**
* @param User $user
*
* @return bool
*/
public function sendUserAccountMessage(User $user): bool
2022-07-04 09:13:36 +02:00
{
try {
return $this->sendCustomRecipientMessage(
[ oxNew( UserRecipients::class, $user )->getSmsRecipient() ]
2022-07-04 09:13:36 +02:00
);
} catch (noRecipientFoundException $e) {
Registry::getLogger()->warning($e->getMessage());
2022-07-04 13:28:58 +02:00
Registry::getUtilsView()->addErrorToDisplay($e);
}
return false;
}
/**
* @param Order $order
*
* @return bool
2022-07-04 16:33:49 +02:00
* @throws noRecipientFoundException
2022-07-04 13:28:58 +02:00
*/
public function sendOrderMessage(Order $order): bool
2022-07-04 13:28:58 +02:00
{
try {
oxNew( OrderRecipients::class, $order )->getSmsRecipient();
} catch (Exception $e) {
dumpvar($e->getMessage());
}
try {
Registry::getLogger()->debug('startRequest', ['orderId' => $order->getId()]);
$return = $this->sendCustomRecipientMessage(
[ oxNew( OrderRecipients::class, $order )->getSmsRecipient() ]
2022-07-04 13:28:58 +02:00
);
Registry::getLogger()->debug('finishRequest', ['orderId' => $order->getId()]);
return $return;
2022-07-04 13:28:58 +02:00
} catch (noRecipientFoundException $e) {
Registry::getLogger()->warning($e->getMessage());
2022-07-04 16:33:49 +02:00
throw $e;
2022-07-04 09:13:36 +02:00
}
}
/**
* @param array $recipientsArray
*
* @return bool
*/
public function sendCustomRecipientMessage(array $recipientsArray): bool
{
2022-07-01 16:11:40 +02:00
try {
$this->setRecipients($recipientsArray);
2022-07-01 16:11:40 +02:00
$configuration = oxNew( Configuration::class );
$client = oxNew( MessageClient::class )->getClient();
/** @var SmsRequestInterface $request */
$request = oxNew( RequestFactory::class, $this->getMessage(), $client )->getSmsRequest();
$request->setTestMode( $configuration->getTestMode() )->setMethod( RequestInterface::METHOD_POST )
->setSenderAddress(
oxNew(
Sender::class,
$configuration->getSmsSenderNumber(),
$configuration->getSmsSenderCountry()
)
)
->setSenderAddressType( RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL );
2022-07-01 16:11:40 +02:00
$recipientsList = $request->getRecipientsList();
2022-07-04 09:13:36 +02:00
foreach ($recipientsArray as $recipient) {
$recipientsList->add( $recipient );
}
2022-07-01 16:11:40 +02:00
$response = $client->request( $request );
2022-07-04 09:13:36 +02:00
$this->response = $response;
if (false === $response->isSuccessful()) {
Registry::getLogger()->warning( $response->getErrorMessage(), [$request->getBody()] );
}
2022-07-01 16:11:40 +02:00
return $response->isSuccessful();
} catch (abortSendingExceptionInterface $e) {
Registry::getLogger()->warning($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
} catch (GuzzleException $e) {
Registry::getLogger()->warning($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
} catch (ApiException $e) {
Registry::getLogger()->warning($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
} catch (\InvalidArgumentException $e) {
Registry::getLogger()->warning($e->getMessage());
Registry::getUtilsView()->addErrorToDisplay($e);
}
2022-07-01 16:11:40 +02:00
return false;
}
2022-07-04 09:13:36 +02:00
/**
* @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
{
$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;
}
}