linkmobility-php-client/src/Request/Request.php

521 lines
12 KiB
PHP
Raw Normal View History

2022-06-20 14:47:33 +02:00
<?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
*/
declare( strict_types = 1 );
namespace D3\LinkmobilityClient\Request;
use Assert\Assert;
use D3\LinkmobilityClient\Client;
2022-06-20 14:47:33 +02:00
use D3\LinkmobilityClient\RecipientsList\RecipientsList;
use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface;
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\ValueObject\Recipient;
use D3\LinkmobilityClient\ValueObject\Sender;
use D3\LinkmobilityClient\ValueObject\SmsMessageAbstract;
use D3\LinkmobilityClient\ValueObject\SmsMessageInterface;
2022-06-20 14:47:33 +02:00
use D3\LinkmobilityClient\ValueObject\StringValueObject;
use GuzzleHttp\RequestOptions;
2022-06-24 16:00:56 +02:00
use InvalidArgumentException;
2022-06-20 14:47:33 +02:00
abstract class Request implements RequestInterface
{
/**
* @var SmsMessageInterface
2022-06-20 14:47:33 +02:00
*/
private $message;
/**
* @var Client
*/
private $client;
2022-06-20 14:47:33 +02:00
/**
* @var string
*/
2022-06-24 16:00:56 +02:00
private $method = RequestInterface::METHOD_POST;
2022-06-20 14:47:33 +02:00
/**
* @var string
*/
private $contentType = RequestInterface::CONTENTTYPE_JSON;
/**
* @var null|string
*/
private $clientMessageId;
/**
* @var string
*/
private $contentCategory = RequestInterface::CONTENTCATEGORY_INFORMATIONAL;
/**
* @var string
*/
private $messageType = RequestInterface::MESSAGETYPE_DEFAULT;
/**
* @var string|null
*/
private $notificationCallbackUrl = null;
/**
* @var null|int
*/
private $priority = null;
/**
* @var RecipientsListInterface
*/
private $recipientsList;
/**
* @var bool
*/
private $sendAsFlashSms = false;
/**
2022-06-24 16:00:56 +02:00
* @var Sender|null
2022-06-20 14:47:33 +02:00
*/
private $senderAddress = null;
/**
* @var string|null
*/
private $senderAddressType = null;
/**
* @var int|null
*/
private $validityPeriode = null;
/**
* @var bool
*/
private $test = false;
/**
* @var bool
*/
private $maxSmsPerMessage = 0;
2022-06-24 16:00:56 +02:00
/**
* @param SmsMessageAbstract $message
2022-06-24 16:00:56 +02:00
*/
public function __construct(SmsMessageInterface $message, Client $client)
2022-06-20 14:47:33 +02:00
{
$this->recipientsList = new RecipientsList($client);
2022-06-20 14:47:33 +02:00
$this->setMessage( $message );
$this->setClient($client);
2022-06-20 14:47:33 +02:00
return $this;
}
/**
* @throws InvalidArgumentException
*/
2022-06-24 16:00:56 +02:00
public function validate()
2022-06-20 14:47:33 +02:00
{
Assert::that( $this->getMethod() )->choice( self::getMethods() );
Assert::that( $this->getUri() )->string()->startsWith( '/' );
Assert::that($this->getBody())->isArray();
Assert::that($this->getResponseClass())->implementsInterface(ResponseInterface::class);
Assert::that($this->getOptions())->isArray();
Assert::that( $this->getRecipientsList() )->isInstanceOf(RecipientsList::class)->notEmpty();
Assert::that( $this->getRecipientsList()->getRecipients())->notEmpty();
Assert::thatAll( $this->getRecipientsList() )->isInstanceOf( Recipient::class )->notEmpty();
2022-06-20 14:47:33 +02:00
// optional properties
Assert::thatNullOr( $this->getClientMessageId() )->string();
Assert::thatNullOr( $this->getContentCategory() )->choice(self::getContentCategories());
Assert::thatNullOr( $this->getNotificationCallbackUrl() )->url();
Assert::thatNullOr( $this->getPriority() )->integer();
2022-06-24 16:00:56 +02:00
Assert::thatNullOr( $this->doSendAsFlashSms() )->boolean();
2022-06-20 14:47:33 +02:00
Assert::thatNullOr( $this->getSenderAddress() )->isInstanceOf(Sender::class);
Assert::thatNullOr( $this->getSenderAddressType() )->choice(self::getSenderAddressTypes());
2022-06-24 16:00:56 +02:00
Assert::thatNullOr( $this->getTestMode() )->boolean();
2022-06-20 14:47:33 +02:00
Assert::thatNullOr( $this->getValidityPeriode() )->integer();
}
public function getRawBody() : array
{
return [
'clientMessageId' => $this->getClientMessageId(),
'contentCategory' => $this->getContentCategory(),
'messageContent' => $this->getMessage()->getMessageContent(),
2022-06-20 14:47:33 +02:00
'notificationCallbackUrl' => $this->getNotificationCallbackUrl(),
'priority' => $this->getPriority(),
'recipientAddressList' => $this->getRecipientsList()->getRecipients(),
2022-06-24 16:00:56 +02:00
'sendAsFlashSms' => $this->doSendAsFlashSms(),
'senderAddress' => $this->getSenderAddress() ? $this->getSenderAddress()->get() : null,
2022-06-20 14:47:33 +02:00
'senderAddressType' => $this->getSenderAddressType(),
2022-06-24 16:00:56 +02:00
'test' => $this->getTestMode(),
'validityPeriode' => $this->getValidityPeriode()
2022-06-20 14:47:33 +02:00
];
}
public function getBody(): array
{
$body = array_filter( $this->getRawBody(), function( $elm ) {
return ! is_null( $elm );
} );
switch ($this->getContentType()) {
case RequestInterface::CONTENTTYPE_JSON:
return [RequestOptions::JSON => $body];
2022-06-20 14:47:33 +02:00
default:
return $body;
}
}
public function getOptions(): array
{
return array_merge(
[
'headers' => [
'Accept' => $this->contentType,
'Content-Type' => $this->contentType,
]
],
$this->getBody()
);
}
2022-06-24 16:00:56 +02:00
/**
* @param StringValueObject $message
*
* @return $this
*/
public function setMessage(StringValueObject $message): Request
2022-06-20 14:47:33 +02:00
{
$this->message = $message;
return $this;
}
public function getMessage(): SmsMessageInterface
2022-06-20 14:47:33 +02:00
{
return $this->message;
}
2022-06-24 16:00:56 +02:00
/**
* @param string $method
*
* @return $this
*/
public function setMethod(string $method): Request
2022-06-20 14:47:33 +02:00
{
$this->method = $method;
return $this;
}
public function getMethod(): string
{
return $this->method;
}
public static function getMethods(): array
{
return [
RequestInterface::METHOD_GET => RequestInterface::METHOD_GET,
RequestInterface::METHOD_POST => RequestInterface::METHOD_POST,
RequestInterface::METHOD_PUT => RequestInterface::METHOD_PUT,
RequestInterface::METHOD_PATCH => RequestInterface::METHOD_PATCH,
RequestInterface::METHOD_DELETE => RequestInterface::METHOD_DELETE,
];
}
2022-06-24 16:00:56 +02:00
/**
* @param string $contentType
*
* @return $this
*/
public function setContentType(string $contentType): Request
2022-06-20 14:47:33 +02:00
{
$this->contentType = $contentType;
return $this;
}
public function getContentType() : string
{
return $this->contentType;
}
2022-06-24 16:00:56 +02:00
/**
* @param $clientMessageId
*
* @return $this
*/
public function setClientMessageId($clientMessageId): Request
2022-06-20 14:47:33 +02:00
{
$this->clientMessageId = $clientMessageId;
return $this;
}
public function getClientMessageId()
{
return $this->clientMessageId;
}
2022-06-24 16:00:56 +02:00
/**
* @param string $contentCategory
*
* @return $this
*/
public function setContentCategory(string $contentCategory): Request
2022-06-20 14:47:33 +02:00
{
$this->contentCategory = $contentCategory;
return $this;
}
public function getContentCategory() : string
{
return $this->contentCategory;
}
public static function getContentCategories(): array
{
return [
RequestInterface::CONTENTCATEGORY_ADVERTISEMENT => RequestInterface::CONTENTCATEGORY_ADVERTISEMENT,
RequestInterface::CONTENTCATEGORY_INFORMATIONAL => RequestInterface::CONTENTCATEGORY_INFORMATIONAL
];
}
2022-06-24 16:00:56 +02:00
/**
* @param bool $test
*
* @return $this
*/
public function setTestMode(bool $test): Request
2022-06-20 14:47:33 +02:00
{
$this->test = $test;
return $this;
}
2022-06-24 16:00:56 +02:00
public function getTestMode() : bool
2022-06-20 14:47:33 +02:00
{
return $this->test;
}
2022-06-24 16:00:56 +02:00
/**
* @param int $maxSmsPerMessage
*
* @return $this
*/
public function setMaxSmsPerMessage(int $maxSmsPerMessage): Request
2022-06-20 14:47:33 +02:00
{
$this->maxSmsPerMessage = $maxSmsPerMessage;
return $this;
}
public function getMaxSmsPerMessage() : int
{
return $this->maxSmsPerMessage;
}
2022-06-24 16:00:56 +02:00
/**
* @param string $messageType
*
* @return $this
*/
public function setMessageType(string $messageType): Request
2022-06-20 14:47:33 +02:00
{
$this->messageType = $messageType;
return $this;
}
public function getMessageType() : string
{
return $this->messageType;
}
/**
2022-06-24 16:00:56 +02:00
* @param string $notificationCallbackUrl
2022-06-20 14:47:33 +02:00
*
* @return $this
*/
2022-06-24 16:00:56 +02:00
public function setNotificationCallbackUrl(string $notificationCallbackUrl): Request
2022-06-20 14:47:33 +02:00
{
$this->notificationCallbackUrl = $notificationCallbackUrl;
return $this;
}
/**
* @return string|null
*/
public function getNotificationCallbackUrl()
{
return $this->notificationCallbackUrl;
}
/**
2022-06-24 16:00:56 +02:00
* @param int $priority
2022-06-20 14:47:33 +02:00
*
* @return $this
*/
2022-06-24 16:00:56 +02:00
public function setPriority(int $priority): Request
2022-06-20 14:47:33 +02:00
{
$this->priority = $priority;
return $this;
}
/**
2022-06-24 16:00:56 +02:00
* @return int|null
2022-06-20 14:47:33 +02:00
*/
public function getPriority()
{
return $this->priority;
}
/**
* @return RecipientsListInterface
*/
public function getRecipientsList() : RecipientsListInterface
{
return $this->recipientsList;
}
/**
2022-06-24 16:00:56 +02:00
* @param bool $flashSms
2022-06-20 14:47:33 +02:00
*
* @return $this
*/
2022-06-24 16:00:56 +02:00
public function sendAsFlashSms(bool $flashSms): Request
2022-06-20 14:47:33 +02:00
{
$this->sendAsFlashSms = $flashSms;
return $this;
}
/**
2022-06-24 16:00:56 +02:00
* @return bool|null
2022-06-20 14:47:33 +02:00
*/
2022-06-24 16:00:56 +02:00
public function doSendAsFlashSms() : bool
2022-06-20 14:47:33 +02:00
{
return $this->sendAsFlashSms;
}
/**
* @param Sender $senderAddress
*
* @return $this
*/
2022-06-24 16:00:56 +02:00
public function setSenderAddress(Sender $senderAddress): Request
2022-06-20 14:47:33 +02:00
{
$this->senderAddress = $senderAddress;
return $this;
}
/**
* @return Sender|null
2022-06-20 14:47:33 +02:00
*/
public function getSenderAddress()
2022-06-20 14:47:33 +02:00
{
return $this->senderAddress;
}
/**
2022-06-24 16:00:56 +02:00
* @param string $senderAddressType
2022-06-20 14:47:33 +02:00
*
* @return $this
*/
2022-06-24 16:00:56 +02:00
public function setSenderAddressType(string $senderAddressType): Request
2022-06-20 14:47:33 +02:00
{
$this->senderAddressType = $senderAddressType;
return $this;
}
/**
* @return string|null
*/
public function getSenderAddressType()
{
return $this->senderAddressType;
}
2022-06-24 16:00:56 +02:00
/**
* @return array
*/
2022-06-20 14:47:33 +02:00
public static function getSenderAddressTypes(): array
{
return [
RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC => RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC,
2022-06-24 16:00:56 +02:00
RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL => RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL,
RequestInterface::SENDERADDRESSTYPE_NATIONAL => RequestInterface::SENDERADDRESSTYPE_NATIONAL,
RequestInterface::SENDERADDRESSTYPE_SHORTCODE => RequestInterface::SENDERADDRESSTYPE_SHORTCODE
2022-06-20 14:47:33 +02:00
];
}
/**
2022-06-24 16:00:56 +02:00
* @param int $validityPeriode
2022-06-20 14:47:33 +02:00
*
* @return $this
*/
2022-06-24 16:00:56 +02:00
public function setValidityPeriode( int $validityPeriode): Request
2022-06-20 14:47:33 +02:00
{
$this->validityPeriode = $validityPeriode;
return $this;
}
/**
* @return int|null
*/
public function getValidityPeriode()
{
return $this->validityPeriode;
}
/**
2022-06-24 16:00:56 +02:00
* @param \Psr\Http\Message\ResponseInterface $rawResponse
*
2022-06-20 14:47:33 +02:00
* @return ResponseInterface
*/
public function getResponseInstance(\Psr\Http\Message\ResponseInterface $rawResponse): ResponseInterface
{
$FQClassName = $this->getResponseClass();
return new $FQClassName($rawResponse);
2022-06-20 14:47:33 +02:00
}
/**
* @return Client
*/
public function getClient(): Client
{
return $this->client;
}
/**
* @param Client $client
*/
public function setClient( Client $client )
{
$this->client = $client;
}
2022-06-20 14:47:33 +02:00
}