linkmobility4oxid/src/Application/Model/MessageTypes/Sms.php

266 lines
9.1 KiB
PHP
Raw Normal View History

<?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
*
* @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-13 13:21:52 +02:00
declare(strict_types=1);
namespace D3\Linkmobility4OXID\Application\Model\MessageTypes;
use D3\Linkmobility4OXID\Application\Model\Configuration;
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;
use D3\Linkmobility4OXID\Application\Model\MessageClient;
use D3\Linkmobility4OXID\Application\Model\OrderRecipients;
use D3\Linkmobility4OXID\Application\Model\RequestFactory;
use D3\Linkmobility4OXID\Application\Model\UserRecipients;
2023-01-07 22:45:24 +01:00
use D3\LinkmobilityClient\Client;
2022-07-01 16:11:40 +02:00
use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use D3\LinkmobilityClient\RecipientsList\RecipientsList;
2023-01-07 22:45:24 +01:00
use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface;
use D3\LinkmobilityClient\Request\RequestInterface;
2023-01-07 22:45:24 +01:00
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\SMS\SmsRequestInterface;
2022-07-15 23:44:05 +02:00
use D3\LinkmobilityClient\ValueObject\Recipient;
use D3\LinkmobilityClient\ValueObject\Sender;
use Exception;
2022-07-01 16:11:40 +02:00
use GuzzleHttp\Exception\GuzzleException;
2022-07-12 16:06:19 +02:00
use InvalidArgumentException;
use libphonenumber\NumberParseException;
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;
2023-01-07 22:45:24 +01:00
use OxidEsales\Eshop\Core\UtilsView;
use Psr\Log\LoggerInterface;
class Sms extends AbstractMessage
{
2022-07-01 16:11:40 +02:00
/**
* @param User $user
*
* @return bool
* @throws Exception
2022-07-01 16:11:40 +02:00
*/
public function sendUserAccountMessage(User $user): bool
2022-07-04 09:13:36 +02:00
{
try {
2023-01-07 22:45:24 +01:00
$this->getLogger()->debug('startRequest', ['userId' => $user->getId()]);
$return = $this->sendCustomRecipientMessage($this->getUserRecipientsList($user));
if ($return) {
$this->setRemark($user->getId(), $this->getRecipientsList(), $this->getMessage());
}
2023-01-07 22:45:24 +01:00
$this->getLogger()->debug('finishRequest', ['userId' => $user->getId()]);
return $return;
2022-07-04 09:13:36 +02:00
} catch (noRecipientFoundException $e) {
2023-01-07 22:45:24 +01:00
$this->getLogger()->warning($e->getMessage());
2023-01-10 23:33:48 +01:00
/** @var UtilsView $utilsView */
$utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class);
$utilsView->addErrorToDisplay($e);
2022-07-04 13:28:58 +02:00
}
return false;
}
2023-01-07 22:45:24 +01:00
/**
* @param User $user
* @return RecipientsListInterface
* @throws noRecipientFoundException
*/
protected function getUserRecipientsList(User $user): RecipientsListInterface
{
/** @var MessageClient $messageClient */
$messageClient = d3GetOxidDIC()->get(MessageClient::class);
d3GetOxidDIC()->set(RecipientsList::class.'.args.client', $messageClient->getClient());
/** @var RecipientsList $recipientsList */
$recipientsList = d3GetOxidDIC()->get(RecipientsList::class);
d3GetOxidDIC()->set(UserRecipients::class.'.args.user', $user);
/** @var UserRecipients $userRecipients */
$userRecipients = d3GetOxidDIC()->get(UserRecipients::class);
return $recipientsList->add(
$userRecipients->getSmsRecipient()
);
}
2022-07-04 13:28:58 +02:00
/**
* @param Order $order
*
* @return bool
2022-07-04 16:33:49 +02:00
* @throws noRecipientFoundException
* @throws Exception
2022-07-04 13:28:58 +02:00
*/
public function sendOrderMessage(Order $order): bool
2022-07-04 13:28:58 +02:00
{
try {
2023-01-07 22:45:24 +01:00
$this->getLogger()->debug('startRequest', ['orderId' => $order->getId()]);
$return = $this->sendCustomRecipientMessage($this->getOrderRecipientsList($order));
if ($return) {
$this->setRemark($order->getOrderUser()->getId(), $this->getRecipientsList(), $this->getMessage());
}
2023-01-07 22:45:24 +01:00
$this->getLogger()->debug('finishRequest', ['orderId' => $order->getId()]);
return $return;
2022-07-04 13:28:58 +02:00
} catch (noRecipientFoundException $e) {
2023-01-07 22:45:24 +01:00
$this->getLogger()->warning($e->getMessage());
2023-01-10 23:33:48 +01:00
/** @var UtilsView $utilsView */
$utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class);
$utilsView->addErrorToDisplay($e);
2022-07-04 09:13:36 +02:00
}
2023-01-07 22:45:24 +01:00
return false;
}
/**
* @param Order $order
* @return RecipientsListInterface
* @throws noRecipientFoundException
*/
protected function getOrderRecipientsList(Order $order): RecipientsListInterface
{
/** @var MessageClient $messageClient */
$messageClient = d3GetOxidDIC()->get(MessageClient::class);
d3GetOxidDIC()->set(RecipientsList::class.'.args.client', $messageClient->getClient());
/** @var RecipientsList $recipientsList */
$recipientsList = d3GetOxidDIC()->get(RecipientsList::class);
return $recipientsList->add(
$this->getOrderRecipient(($order))
);
2022-07-04 09:13:36 +02:00
}
2022-07-15 00:19:48 +02:00
/**
* @param Order $order
* @return Recipient
* @throws noRecipientFoundException
*/
protected function getOrderRecipient(Order $order): Recipient
{
2023-01-07 22:45:24 +01:00
d3GetOxidDIC()->set(OrderRecipients::class.'.args.order', $order);
/** @var OrderRecipients $orderRecipients */
$orderRecipients = d3GetOxidDIC()->get(OrderRecipients::class);
return $orderRecipients->getSmsRecipient();
2022-07-15 00:19:48 +02:00
}
2022-07-04 09:13:36 +02:00
/**
2023-01-10 23:33:48 +01:00
* @param RecipientsListInterface $recipientsList
2022-07-04 09:13:36 +02:00
*
* @return bool
*/
2023-01-10 23:33:48 +01:00
public function sendCustomRecipientMessage(RecipientsListInterface $recipientsList): bool
{
2022-07-01 16:11:40 +02:00
try {
2023-01-07 22:45:24 +01:00
$this->response = $response = $this->submitMessage($recipientsList);
2022-07-01 16:11:40 +02:00
return $response->isSuccessful();
2023-01-07 22:45:24 +01:00
} catch (abortSendingExceptionInterface|GuzzleException|ApiException|InvalidArgumentException $e) {
$this->getLogger()->warning($e->getMessage());
2022-12-28 00:11:00 +01:00
// Oxid does not accept throwable interface only exceptions according to definition
2023-01-10 23:33:48 +01:00
/** @var UtilsView $utilsView */
$utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class);
$utilsView->addErrorToDisplay($e->getMessage());
}
2022-07-01 16:11:40 +02:00
return false;
}
2022-07-04 09:13:36 +02:00
2023-01-07 22:45:24 +01:00
/**
* @param Configuration $configuration
* @param Client $client
* @return SmsRequestInterface
* @throws NumberParseException
* @throws RecipientException
2023-01-07 22:45:24 +01:00
*/
protected function getRequest(Configuration $configuration, Client $client): SmsRequestInterface
{
$requestFactory = $this->getRequestFactory($this->getMessage(), $client);
2023-01-15 22:24:49 +01:00
$sender = $this->getSender((string) $configuration->getSmsSenderNumber(), (string) $configuration->getSmsSenderCountry());
2023-01-07 22:45:24 +01:00
$request = $requestFactory->getSmsRequest();
$request->setTestMode($configuration->getTestMode())->setMethod(RequestInterface::METHOD_POST)
->setSenderAddress($sender)
->setSenderAddressType(RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL);
return $request;
}
/**
* @param string $message
* @param Client $client
* @return RequestFactory
*/
protected function getRequestFactory(string $message, Client $client): RequestFactory
{
return oxNew(RequestFactory::class, $message, $client);
}
/**
* @param string $number
* @param string $countryCode
* @throws NumberParseException
* @throws RecipientException
* @return Sender
*/
protected function getSender(string $number, string $countryCode): Sender
{
return oxNew(Sender::class, $number, $countryCode);
}
2023-01-07 22:45:24 +01:00
/**
2023-01-10 23:33:48 +01:00
* @param RecipientsListInterface $recipientsList
2023-01-07 22:45:24 +01:00
* @return ResponseInterface
* @throws ApiException
* @throws GuzzleException
2023-01-15 22:24:49 +01:00
* @throws NumberParseException
* @throws RecipientException
2023-01-07 22:45:24 +01:00
*/
2023-01-10 23:33:48 +01:00
protected function submitMessage(RecipientsListInterface $recipientsList): ResponseInterface
2023-01-07 22:45:24 +01:00
{
$this->setRecipients($recipientsList);
/** @var Configuration $configuration */
$configuration = d3GetOxidDIC()->get(Configuration::class);
/** @var MessageClient $messageClient */
$messageClient = d3GetOxidDIC()->get(MessageClient::class);
$client = $messageClient->getClient();
$request = $this->getRequest($configuration, $client);
$requestRecipientsList = $request->getRecipientsList();
2023-01-12 22:49:27 +01:00
foreach ($recipientsList->getRecipientsList() as $recipient) {
2023-01-07 22:45:24 +01:00
$requestRecipientsList->add($recipient);
}
$response = $client->request($request);
if (false === $response->isSuccessful()) {
$this->getLogger()->warning($response->getErrorMessage(), [$request->getBody()]);
}
return $response;
}
2022-07-04 09:13:36 +02:00
/**
* @return string
*/
public function getTypeName(): string
{
return 'SMS';
}
2023-01-07 22:45:24 +01:00
/**
* @return LoggerInterface
*/
2023-01-15 22:24:49 +01:00
public function getLogger(): LoggerInterface
2023-01-07 22:45:24 +01:00
{
return Registry::getLogger();
}
2022-07-13 13:23:48 +02:00
}