2022-07-01 11:48:44 +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-01 11:48:44 +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-01 11:48:44 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-13 13:21:52 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-07-01 11:48:44 +02:00
|
|
|
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-01 16:11:40 +02:00
|
|
|
use D3\LinkmobilityClient\Exceptions\ApiException;
|
2022-07-01 11:48:44 +02:00
|
|
|
use D3\LinkmobilityClient\Request\RequestInterface;
|
2022-07-04 09:13:36 +02:00
|
|
|
use D3\LinkmobilityClient\Response\ResponseInterface;
|
2022-07-12 13:44:05 +02:00
|
|
|
use D3\LinkmobilityClient\SMS\SmsRequestInterface;
|
2022-07-08 10:17:59 +02:00
|
|
|
use D3\LinkmobilityClient\ValueObject\Recipient;
|
2022-07-01 11:48:44 +02:00
|
|
|
use D3\LinkmobilityClient\ValueObject\Sender;
|
2022-07-01 16:11:40 +02:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2022-07-12 16:06:19 +02:00
|
|
|
use InvalidArgumentException;
|
2022-07-04 13:28:58 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\Order;
|
2022-07-01 11:48:44 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
2022-07-01 16:11:40 +02:00
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2022-07-01 11:48:44 +02:00
|
|
|
|
|
|
|
class Sms
|
|
|
|
{
|
2022-07-04 09:13:36 +02:00
|
|
|
private $response;
|
2022-07-08 10:17:59 +02:00
|
|
|
private $recipients = [];
|
2022-07-08 15:13:05 +02:00
|
|
|
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
|
|
|
|
*/
|
2022-07-08 15:13:05 +02:00
|
|
|
public function sendUserAccountMessage(User $user): bool
|
2022-07-04 09:13:36 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->sendCustomRecipientMessage(
|
2022-07-13 13:23:48 +02:00
|
|
|
[ 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
|
|
|
*/
|
2022-07-08 15:13:05 +02:00
|
|
|
public function sendOrderMessage(Order $order): bool
|
2022-07-04 13:28:58 +02:00
|
|
|
{
|
2022-07-08 09:49:39 +02:00
|
|
|
try {
|
|
|
|
Registry::getLogger()->debug('startRequest', ['orderId' => $order->getId()]);
|
|
|
|
$return = $this->sendCustomRecipientMessage(
|
2022-07-13 13:23:48 +02:00
|
|
|
[ oxNew(OrderRecipients::class, $order)->getSmsRecipient() ]
|
2022-07-04 13:28:58 +02:00
|
|
|
);
|
2022-07-08 09:49:39 +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
|
|
|
|
*/
|
2022-07-08 15:13:05 +02:00
|
|
|
public function sendCustomRecipientMessage(array $recipientsArray): bool
|
2022-07-01 11:48:44 +02:00
|
|
|
{
|
2022-07-01 16:11:40 +02:00
|
|
|
try {
|
2022-07-08 10:17:59 +02:00
|
|
|
$this->setRecipients($recipientsArray);
|
2022-07-13 13:23:48 +02:00
|
|
|
$configuration = oxNew(Configuration::class);
|
|
|
|
$client = oxNew(MessageClient::class)->getClient();
|
2022-07-01 16:11:40 +02:00
|
|
|
|
2022-07-12 13:44:05 +02:00
|
|
|
/** @var SmsRequestInterface $request */
|
2022-07-13 13:23:48 +02:00
|
|
|
$request = oxNew(RequestFactory::class, $this->getMessage(), $client)->getSmsRequest();
|
|
|
|
$request->setTestMode($configuration->getTestMode())->setMethod(RequestInterface::METHOD_POST)
|
2022-07-12 13:44:05 +02:00
|
|
|
->setSenderAddress(
|
|
|
|
oxNew(
|
|
|
|
Sender::class,
|
|
|
|
$configuration->getSmsSenderNumber(),
|
|
|
|
$configuration->getSmsSenderCountry()
|
|
|
|
)
|
|
|
|
)
|
2022-07-13 13:23:48 +02:00
|
|
|
->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) {
|
2022-07-13 13:23:48 +02:00
|
|
|
$recipientsList->add($recipient);
|
2022-07-04 09:13:36 +02:00
|
|
|
}
|
2022-07-08 09:49:39 +02:00
|
|
|
|
2022-07-13 13:23:48 +02:00
|
|
|
$response = $client->request($request);
|
2022-07-01 16:11:40 +02:00
|
|
|
|
2022-07-04 09:13:36 +02:00
|
|
|
$this->response = $response;
|
|
|
|
|
2022-07-12 13:44:05 +02:00
|
|
|
if (false === $response->isSuccessful()) {
|
2022-07-13 13:23:48 +02:00
|
|
|
Registry::getLogger()->warning($response->getErrorMessage(), [$request->getBody()]);
|
2022-07-12 13:44:05 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2022-07-13 13:23:48 +02:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2022-07-01 16:11:40 +02:00
|
|
|
Registry::getLogger()->warning($e->getMessage());
|
|
|
|
Registry::getUtilsView()->addErrorToDisplay($e);
|
2022-07-01 11:48:44 +02:00
|
|
|
}
|
2022-07-01 16:11:40 +02:00
|
|
|
|
|
|
|
return false;
|
2022-07-01 11:48:44 +02:00
|
|
|
}
|
2022-07-04 09:13:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ResponseInterface|null
|
|
|
|
*/
|
|
|
|
public function getResponse()
|
|
|
|
{
|
|
|
|
return $this->response;
|
|
|
|
}
|
2022-07-08 10:17:59 +02:00
|
|
|
|
|
|
|
protected function setRecipients(array $recipients)
|
|
|
|
{
|
|
|
|
$this->recipients = $recipients;
|
|
|
|
}
|
|
|
|
|
2022-07-13 13:23:48 +02:00
|
|
|
public function getRecipientsList(): string
|
2022-07-08 10:17:59 +02:00
|
|
|
{
|
|
|
|
$list = [];
|
|
|
|
/** @var Recipient $recipient */
|
|
|
|
foreach ($this->recipients as $recipient) {
|
|
|
|
$list[] = $recipient->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $list);
|
|
|
|
}
|
2022-07-08 15:13:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $message
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-07-13 13:23:48 +02:00
|
|
|
protected function sanitizeMessage($message): string
|
2022-07-08 15:13:05 +02:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2022-07-13 13:23:48 +02:00
|
|
|
public function getMessage(): string
|
2022-07-08 15:13:05 +02:00
|
|
|
{
|
|
|
|
return $this->message;
|
|
|
|
}
|
2022-07-13 13:23:48 +02:00
|
|
|
}
|