cleanup code

This commit is contained in:
Daniel Seifert 2022-06-24 16:00:56 +02:00
parent 50cf733101
commit fae19607c2
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
16 changed files with 213 additions and 649 deletions

View File

@ -19,12 +19,13 @@
], ],
"require": { "require": {
"php": "^7.0", "php": "^7.0",
"beberlei/assert": "^2.9", "beberlei/assert": "^2.9.9",
"guzzlehttp/guzzle": "~6.2", "guzzlehttp/guzzle": "~6.2",
"psr/http-message": "~1.0", "psr/http-message": "~1.0",
"symfony/options-resolver": "^3.0|^4.0", "symfony/options-resolver": "^4.4.37",
"phlib/sms-length": "^1.1.0 || ^2.0.0", "phlib/sms-length": "^1.1.0 || ^2.0.0",
"giggsey/libphonenumber-for-php": "^8.12" "giggsey/libphonenumber-for-php": "^8.12.50",
"ext-json": "*"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit" : "~5.7||~6.0", "phpunit/phpunit" : "~5.7||~6.0",

View File

@ -13,12 +13,16 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare( strict_types = 1 );
namespace D3\LinkmobilityClient; namespace D3\LinkmobilityClient;
use D3\LinkmobilityClient\Exceptions\ApiException; use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Request\RequestInterface; use D3\LinkmobilityClient\Request\RequestInterface;
use GuzzleHttp\RequestOptions; use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use RuntimeException;
class Client class Client
{ {
@ -29,7 +33,7 @@ class Client
public function __construct(string $accessToken, $apiUrl = false, $client = false) public function __construct(string $accessToken, $apiUrl = false, $client = false)
{ {
if ($apiUrl !== false && false === $apiUrl instanceof UrlInterface) { if ($apiUrl !== false && false === $apiUrl instanceof UrlInterface) {
throw new \RuntimeException('ApiUrl instance must implement UrlInterface'); throw new RuntimeException(ExceptionMessages::WRONG_APIURL_INTERFACE);
} }
$this->accessToken = $accessToken; $this->accessToken = $accessToken;
@ -37,10 +41,16 @@ class Client
$this->requestClient = $client ?: new \GuzzleHttp\Client( [ 'base_uri' => $this->apiUrl->getBaseUri() ] ); $this->requestClient = $client ?: new \GuzzleHttp\Client( [ 'base_uri' => $this->apiUrl->getBaseUri() ] );
} }
public function request(RequestInterface $request) : \D3\LinkmobilityClient\Response\ResponseInterface /**
* @param RequestInterface $request
*
* @return Response\ResponseInterface
* @throws ApiException
* @throws GuzzleException
*/
public function request(RequestInterface $request) : Response\ResponseInterface
{ {
$request->validate(); $request->validate();
$responseClass = $request->getResponseClass();
return $request->getResponseInstance( return $request->getResponseInstance(
$this->rawRequest($request->getUri(), $request->getMethod(), $request->getOptions()) $this->rawRequest($request->getUri(), $request->getMethod(), $request->getOptions())
@ -50,7 +60,7 @@ class Client
/** /**
* @param $url * @param $url
* @param string $method * @param string $method
* @param array $postArgs * @param array $options
* *
* @return ResponseInterface * @return ResponseInterface
* @throws ApiException * @throws ApiException
@ -67,7 +77,9 @@ class Client
); );
if ($response->getStatusCode() != 200) { if ($response->getStatusCode() != 200) {
throw new ApiException('request '.$url.' returns status code '.$response->getStatusCode()); throw new ApiException(
sprintf(ExceptionMessages::NOK_REQUEST_RETURN, [$url, $response->getStatusCode()])
);
} }
return $response; return $response;

View File

@ -13,6 +13,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare(strict_types=1);
namespace D3\LinkmobilityClient\Exceptions; namespace D3\LinkmobilityClient\Exceptions;
class ApiException extends LinkmobilityException class ApiException extends LinkmobilityException

View File

@ -0,0 +1,29 @@
<?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\LinkmobilityClient\Exceptions;
class ExceptionMessages
{
const INVALID_SENDER = 'invalid sender phone number';
const WRONG_APIURL_INTERFACE = 'ApiUrl instance must implement UrlInterface';
const NOK_REQUEST_RETURN = 'request %1$s returns status code %2$s';
const INVALID_RECIPIENT_PHONE = 'invalid recipient phone number';
const NOT_A_MOBILE_NUMBER = 'not a mobile number';
}

View File

@ -17,7 +17,9 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\Exceptions; namespace D3\LinkmobilityClient\Exceptions;
class LinkmobilityException extends \Exception use Exception;
class LinkmobilityException extends Exception
{ {
} }

View File

@ -17,10 +17,15 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\RecipientsList; namespace D3\LinkmobilityClient\RecipientsList;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use D3\LinkmobilityClient\ValueObject\Recipient; use D3\LinkmobilityClient\ValueObject\Recipient;
use Iterator;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberType; use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
class RecipientsList implements RecipientsListInterface, \Iterator class RecipientsList implements RecipientsListInterface, Iterator
{ {
/** /**
* @var array * @var array
@ -29,20 +34,28 @@ class RecipientsList implements RecipientsListInterface, \Iterator
public function add(Recipient $recipient) : RecipientsListInterface public function add(Recipient $recipient) : RecipientsListInterface
{ {
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $phoneUtil = PhoneNumberUtil::getInstance();
try { try {
$phoneNumber = $phoneUtil->parse($recipient->get(), $recipient->getCountryCode()); $phoneNumber = $phoneUtil->parse( $recipient->get(), $recipient->getCountryCode() );
if (false === $phoneUtil->isValidNumber($phoneNumber)) { if ( false === $phoneUtil->isValidNumber( $phoneNumber ) ) {
throw new \D3\LinkmobilityClient\Exceptions\RecipientException('invalid recipient phone number'); throw new RecipientException( ExceptionMessages::INVALID_RECIPIENT_PHONE );
} elseif (false === in_array($phoneUtil->getNumberType($phoneNumber), [PhoneNumberType::MOBILE, PhoneNumberType::FIXED_LINE_OR_MOBILE])) { } elseif (
throw new \D3\LinkmobilityClient\Exceptions\RecipientException('not a mobile number'); false === in_array(
$phoneUtil->getNumberType( $phoneNumber ),
[
PhoneNumberType::MOBILE,
PhoneNumberType::FIXED_LINE_OR_MOBILE
]
)
) {
throw new RecipientException( ExceptionMessages::NOT_A_MOBILE_NUMBER );
} }
$this->recipients[md5(serialize($recipient))] = $recipient; $this->recipients[ md5( serialize( $recipient ) ) ] = $recipient;
} catch (\libphonenumber\NumberParseException $e) { } catch (NumberParseException $e) {
// var_dump($e); // var_dump($e);
} catch (\D3\LinkmobilityClient\Exceptions\RecipientException $e) { } catch (RecipientException $e) {
// var_dump($e); // var_dump($e);
} }
@ -68,16 +81,25 @@ class RecipientsList implements RecipientsListInterface, \Iterator
); );
} }
/**
* @return array
*/
public function getRecipientsList() : array public function getRecipientsList() : array
{ {
return $this->recipients; return $this->recipients;
} }
/**
* @return false|mixed
*/
public function current() public function current()
{ {
return current($this->recipients); return current($this->recipients);
} }
/**
* @return false|mixed|void
*/
public function next() public function next()
{ {
return next($this->recipients); return next($this->recipients);
@ -93,10 +115,8 @@ class RecipientsList implements RecipientsListInterface, \Iterator
return reset($this->recipients); return reset($this->recipients);
} }
public function valid() public function valid(): bool
{ {
return (false !== current($this->recipients) && current($this->recipients) instanceof Recipient); return current($this->recipients) instanceof Recipient;
} }
//stract methods and must therefore be declared abstract or implement the remaining methods (Iterator::current, Iterator::next, Iterator::key, ...) in
} }

View File

@ -23,10 +23,9 @@ use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface;
use D3\LinkmobilityClient\Response\ResponseInterface; use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\ValueObject\Recipient; use D3\LinkmobilityClient\ValueObject\Recipient;
use D3\LinkmobilityClient\ValueObject\Sender; use D3\LinkmobilityClient\ValueObject\Sender;
use D3\LinkmobilityClient\ValueObject\SmsMessage;
use D3\LinkmobilityClient\ValueObject\StringValueObject; use D3\LinkmobilityClient\ValueObject\StringValueObject;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use OxidEsales\Eshop\Core\Registry; use InvalidArgumentException;
abstract class Request implements RequestInterface abstract class Request implements RequestInterface
{ {
@ -38,7 +37,7 @@ abstract class Request implements RequestInterface
/** /**
* @var string * @var string
*/ */
private $method = RequestInterface::METHOD_GET; private $method = RequestInterface::METHOD_POST;
/** /**
* @var string * @var string
@ -81,7 +80,7 @@ abstract class Request implements RequestInterface
private $sendAsFlashSms = false; private $sendAsFlashSms = false;
/** /**
* @var string|null * @var Sender|null
*/ */
private $senderAddress = null; private $senderAddress = null;
@ -105,6 +104,9 @@ abstract class Request implements RequestInterface
*/ */
private $maxSmsPerMessage = 0; private $maxSmsPerMessage = 0;
/**
* @param StringValueObject $message
*/
public function __construct(StringValueObject $message) public function __construct(StringValueObject $message)
{ {
$this->recipientsList = new RecipientsList(); $this->recipientsList = new RecipientsList();
@ -116,7 +118,7 @@ abstract class Request implements RequestInterface
/** /**
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function validate(): void public function validate()
{ {
Assert::that( $this->getMethod() )->choice( self::getMethods() ); Assert::that( $this->getMethod() )->choice( self::getMethods() );
Assert::that( $this->getUri() )->string()->startsWith( '/' ); Assert::that( $this->getUri() )->string()->startsWith( '/' );
@ -134,10 +136,10 @@ abstract class Request implements RequestInterface
Assert::thatNullOr( $this->getContentCategory() )->choice(self::getContentCategories()); Assert::thatNullOr( $this->getContentCategory() )->choice(self::getContentCategories());
Assert::thatNullOr( $this->getNotificationCallbackUrl() )->url(); Assert::thatNullOr( $this->getNotificationCallbackUrl() )->url();
Assert::thatNullOr( $this->getPriority() )->integer(); Assert::thatNullOr( $this->getPriority() )->integer();
Assert::thatNullOr( $this->getSendAsFlashSms() )->boolean(); Assert::thatNullOr( $this->doSendAsFlashSms() )->boolean();
Assert::thatNullOr( $this->getSenderAddress() )->isInstanceOf(Sender::class); Assert::thatNullOr( $this->getSenderAddress() )->isInstanceOf(Sender::class);
Assert::thatNullOr( $this->getSenderAddressType() )->choice(self::getSenderAddressTypes()); Assert::thatNullOr( $this->getSenderAddressType() )->choice(self::getSenderAddressTypes());
Assert::thatNullOr( $this->isTest() )->boolean(); Assert::thatNullOr( $this->getTestMode() )->boolean();
Assert::thatNullOr( $this->getValidityPeriode() )->integer(); Assert::thatNullOr( $this->getValidityPeriode() )->integer();
} }
@ -152,10 +154,10 @@ abstract class Request implements RequestInterface
'notificationCallbackUrl' => $this->getNotificationCallbackUrl(), 'notificationCallbackUrl' => $this->getNotificationCallbackUrl(),
'priority' => $this->getPriority(), 'priority' => $this->getPriority(),
'recipientAddressList' => $this->getRecipientsList()->getRecipients(), 'recipientAddressList' => $this->getRecipientsList()->getRecipients(),
'sendAsFlashSms' => $this->getSendAsFlashSms(), 'sendAsFlashSms' => $this->doSendAsFlashSms(),
'senderAddress' => $this->getSenderAddress() ? $this->getSenderAddress()->get() : null, 'senderAddress' => $this->getSenderAddress() ? $this->getSenderAddress()->get() : null,
'senderAddressType' => $this->getSenderAddressType(), 'senderAddressType' => $this->getSenderAddressType(),
'test' => $this->isTest(), 'test' => $this->getTestMode(),
'validityPeriode' => $this->getValidityPeriode() 'validityPeriode' => $this->getValidityPeriode()
]; ];
} }
@ -187,7 +189,12 @@ abstract class Request implements RequestInterface
); );
} }
public function setMessage(StringValueObject $message) /**
* @param StringValueObject $message
*
* @return $this
*/
public function setMessage(StringValueObject $message): Request
{ {
$this->message = $message; $this->message = $message;
@ -199,7 +206,12 @@ abstract class Request implements RequestInterface
return $this->message; return $this->message;
} }
public function setMethod(string $method) /**
* @param string $method
*
* @return $this
*/
public function setMethod(string $method): Request
{ {
$this->method = $method; $this->method = $method;
@ -222,7 +234,12 @@ abstract class Request implements RequestInterface
]; ];
} }
public function setContentType(string $contentType) /**
* @param string $contentType
*
* @return $this
*/
public function setContentType(string $contentType): Request
{ {
$this->contentType = $contentType; $this->contentType = $contentType;
@ -234,7 +251,12 @@ abstract class Request implements RequestInterface
return $this->contentType; return $this->contentType;
} }
public function setClientMessageId($clientMessageId) /**
* @param $clientMessageId
*
* @return $this
*/
public function setClientMessageId($clientMessageId): Request
{ {
$this->clientMessageId = $clientMessageId; $this->clientMessageId = $clientMessageId;
@ -246,7 +268,12 @@ abstract class Request implements RequestInterface
return $this->clientMessageId; return $this->clientMessageId;
} }
public function setContentCategory(string $contentCategory) /**
* @param string $contentCategory
*
* @return $this
*/
public function setContentCategory(string $contentCategory): Request
{ {
$this->contentCategory = $contentCategory; $this->contentCategory = $contentCategory;
@ -266,19 +293,29 @@ abstract class Request implements RequestInterface
]; ];
} }
public function setTest(bool $test) /**
* @param bool $test
*
* @return $this
*/
public function setTestMode(bool $test): Request
{ {
$this->test = $test; $this->test = $test;
return $this; return $this;
} }
public function isTest() : bool public function getTestMode() : bool
{ {
return $this->test; return $this->test;
} }
public function setMaxSmsPerMessage(int $maxSmsPerMessage) /**
* @param int $maxSmsPerMessage
*
* @return $this
*/
public function setMaxSmsPerMessage(int $maxSmsPerMessage): Request
{ {
$this->maxSmsPerMessage = $maxSmsPerMessage; $this->maxSmsPerMessage = $maxSmsPerMessage;
@ -290,7 +327,12 @@ abstract class Request implements RequestInterface
return $this->maxSmsPerMessage; return $this->maxSmsPerMessage;
} }
public function setMessageType(string $messageType) /**
* @param string $messageType
*
* @return $this
*/
public function setMessageType(string $messageType): Request
{ {
$this->messageType = $messageType; $this->messageType = $messageType;
@ -303,11 +345,11 @@ abstract class Request implements RequestInterface
} }
/** /**
* @param string|null $notificationCallbackUrl * @param string $notificationCallbackUrl
* *
* @return $this * @return $this
*/ */
public function setNotificationCallbackUrl($notificationCallbackUrl) public function setNotificationCallbackUrl(string $notificationCallbackUrl): Request
{ {
$this->notificationCallbackUrl = $notificationCallbackUrl; $this->notificationCallbackUrl = $notificationCallbackUrl;
@ -323,11 +365,11 @@ abstract class Request implements RequestInterface
} }
/** /**
* @param string|null $priority * @param int $priority
* *
* @return $this * @return $this
*/ */
public function setPriority($priority) public function setPriority(int $priority): Request
{ {
$this->priority = $priority; $this->priority = $priority;
@ -335,7 +377,7 @@ abstract class Request implements RequestInterface
} }
/** /**
* @return string|null * @return int|null
*/ */
public function getPriority() public function getPriority()
{ {
@ -351,11 +393,11 @@ abstract class Request implements RequestInterface
} }
/** /**
* @param array $recipientList * @param bool $flashSms
* *
* @return $this * @return $this
*/ */
public function setSendAsFlashSms(bool $flashSms) public function sendAsFlashSms(bool $flashSms): Request
{ {
$this->sendAsFlashSms = $flashSms; $this->sendAsFlashSms = $flashSms;
@ -363,9 +405,9 @@ abstract class Request implements RequestInterface
} }
/** /**
* @return string|null * @return bool|null
*/ */
public function getSendAsFlashSms() : bool public function doSendAsFlashSms() : bool
{ {
return $this->sendAsFlashSms; return $this->sendAsFlashSms;
} }
@ -375,7 +417,7 @@ abstract class Request implements RequestInterface
* *
* @return $this * @return $this
*/ */
public function setSenderAddress(Sender $senderAddress) public function setSenderAddress(Sender $senderAddress): Request
{ {
$this->senderAddress = $senderAddress; $this->senderAddress = $senderAddress;
@ -391,11 +433,11 @@ abstract class Request implements RequestInterface
} }
/** /**
* @param $senderAddressType * @param string $senderAddressType
* *
* @return $this * @return $this
*/ */
public function setSenderAddressType($senderAddressType) public function setSenderAddressType(string $senderAddressType): Request
{ {
$this->senderAddressType = $senderAddressType; $this->senderAddressType = $senderAddressType;
@ -410,22 +452,25 @@ abstract class Request implements RequestInterface
return $this->senderAddressType; return $this->senderAddressType;
} }
/**
* @return array
*/
public static function getSenderAddressTypes(): array public static function getSenderAddressTypes(): array
{ {
return [ return [
RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC => RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC, RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC => RequestInterface::SENDERADDRESSTYPE_ALPHANUMERIC,
RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL => RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL, RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL => RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL,
RequestInterface::SENDERADDRESSTYPE_NATIONAL => RequestInterface::SENDERADDRESSTYPE_NATIONAL, RequestInterface::SENDERADDRESSTYPE_NATIONAL => RequestInterface::SENDERADDRESSTYPE_NATIONAL,
RequestInterface::SENDERADDRESSTYPE_SHORTCODE => RequestInterface::SENDERADDRESSTYPE_SHORTCODE RequestInterface::SENDERADDRESSTYPE_SHORTCODE => RequestInterface::SENDERADDRESSTYPE_SHORTCODE
]; ];
} }
/** /**
* @param $validityPeriode * @param int $validityPeriode
* *
* @return $this * @return $this
*/ */
public function setValidityPeriode($validityPeriode) public function setValidityPeriode( int $validityPeriode): Request
{ {
$this->validityPeriode = $validityPeriode; $this->validityPeriode = $validityPeriode;
@ -441,6 +486,8 @@ abstract class Request implements RequestInterface
} }
/** /**
* @param \Psr\Http\Message\ResponseInterface $rawResponse
*
* @return ResponseInterface * @return ResponseInterface
*/ */
public function getResponseInstance(\Psr\Http\Message\ResponseInterface $rawResponse): ResponseInterface public function getResponseInstance(\Psr\Http\Message\ResponseInterface $rawResponse): ResponseInterface

View File

@ -17,7 +17,8 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\Request; namespace D3\LinkmobilityClient\Request;
use D3\LinkmobilityClient\Response\ResponseInterface; use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use D3\LinkmobilityClient\Response\ResponseInterface as LMResponseInterface;
interface RequestInterface interface RequestInterface
{ {
@ -71,11 +72,11 @@ interface RequestInterface
public function getResponseClass() : string; public function getResponseClass() : string;
/** /**
* @param \Psr\Http\Message\ResponseInterface $rawResponse * @param PsrResponseInterface $rawResponse
* *
* @return ResponseInterface * @return LMResponseInterface
*/ */
public function getResponseInstance(\Psr\Http\Message\ResponseInterface $rawResponse): ResponseInterface; public function getResponseInstance(PsrResponseInterface $rawResponse): LMResponseInterface;
/** /**
* Must return the options for this request. If there are none, return [] (empty array) * Must return the options for this request. If there are none, return [] (empty array)
@ -89,5 +90,5 @@ interface RequestInterface
* This is called before sending the request * This is called before sending the request
* Must throw an exception if the validation fails * Must throw an exception if the validation fails
*/ */
public function validate() : void; public function validate();
} }

View File

@ -4,8 +4,6 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\Response; namespace D3\LinkmobilityClient\Response;
use Assert\Assert;
abstract class Response implements ResponseInterface abstract class Response implements ResponseInterface
{ {
const STATUSCODE = 'statusCode'; const STATUSCODE = 'statusCode';

View File

@ -22,7 +22,9 @@ class Request
{ {
public function __construct( Sender $sender, SmsMessage $message) public function __construct( Sender $sender, SmsMessage $message)
{ {
$this = new TextRequest($sender, $message); // use factory pattern
//$this = new TextRequest($sender, $message);
/* /*
if ($message->isGsm7()) { if ($message->isGsm7()) {
return new TextRequest($sender, $message); return new TextRequest($sender, $message);

View File

@ -4,46 +4,6 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\SMS; namespace D3\LinkmobilityClient\SMS;
use Assert\Assert;
use Loevgaard\Linkmobility\Response\BatchStatusResponse\Details;
use Loevgaard\Linkmobility\Response\BatchStatusResponse\Stat;
class Response extends \D3\LinkmobilityClient\Response\Response class Response extends \D3\LinkmobilityClient\Response\Response
{ {
/**
* @var Stat
*/
protected $stat;
/**
* @var Details
*/
protected $details;
public function init() : void
{
Assert::that($this->data)
->keyExists('stat')
->keyExists('details')
;
$this->stat = new Stat($this->data['stat']);
$this->details = new Details($this->data['details']);
}
/**
* @return Stat
*/
public function getStat(): Stat
{
return $this->stat;
}
/**
* @return Details
*/
public function getDetails(): Details
{
return $this->details;
}
} }

View File

@ -18,485 +18,30 @@ declare( strict_types = 1 );
namespace D3\LinkmobilityClient\SMS; namespace D3\LinkmobilityClient\SMS;
use Assert\Assert; use Assert\Assert;
use D3\LinkmobilityClient\Request\RequestInterface;
use D3\LinkmobilityClient\Url;
use D3\LinkmobilityClient\ValueObject\Message;
use D3\LinkmobilityClient\ValueObject\Sender;
use D3\LinkmobilityClient\ValueObject\SmsMessage; use D3\LinkmobilityClient\ValueObject\SmsMessage;
class TextRequest extends \D3\LinkmobilityClient\Request\Request class TextRequest extends \D3\LinkmobilityClient\Request\Request
{ {
/** /**
* @var Sender * @return string
*/ */
protected $sender;
/**
* @var boolean
*/
protected $status;
/**
* @var string
*/
protected $statusUrl;
/**
* @var string
*/
protected $returnData;
/**
* @var int
*/
protected $class;
/**
* @var \DateTimeInterface
*/
protected $sendTime;
/**
* @var int
*/
protected $price;
/**
* @var boolean
*/
protected $charity;
/**
* @var string
*/
protected $invoiceText;
/**
* @var int
*/
protected $validity;
/**
* @var string
*/
protected $format;
/**
* @var string
*/
protected $udh;
/**
* @var array
*/
protected $attachment;
/**
* @var string
*/
protected $pushUrl;
/**
* @var string
*/
protected $pushExpire;
/**
* @var array
*/
protected $filter;
/**
* @var array
*/
protected $segmentation;
/**
* @var int
*/
protected $pid;
/**
* @var string
*/
protected $advanced;
/**
* @var string
*/
protected $protocol;
/**
* @var string
*/
protected $revenueText;
public function getUri(): string public function getUri(): string
{ {
return '/rest/smsmessaging/text'; return '/rest/smsmessaging/text';
} }
public function validate(): void public function validate()
{ {
parent::validate(); parent::validate();
Assert::thatNullOr( $this->getMessage() )->isInstanceOf(SmsMessage::class); Assert::thatNullOr( $this->getMessage() )->isInstanceOf(SmsMessage::class);
} }
/**
* @return string
*/
public function getResponseClass(): string public function getResponseClass(): string
{ {
return Response::class; return Response::class;
} }
/**
* @param Message $message
*
* @return PostMessageRequest
*/
/*
public function setMessage( SmsMessage $message )
{
$this->message = $message;
if ($message->isGsm7()) {
$this->setFormat(SmsMessage::GSM_7BIT);
} else {
$this->setFormat(SmsMessage::GSM_UCS2);
}
return $this;
}
*/
/**
* @return bool
*/
public function isStatus(): bool
{
return $this->status;
}
/**
* @param bool $status
*
* @return PostMessageRequest
*/
public function setStatus( bool $status )
{
$this->status = $status;
return $this;
}
/**
* @return string
*/
public function getReturnData(): string
{
return $this->returnData;
}
/**
* @param string $returnData
*
* @return PostMessageRequest
*/
public function setReturnData( string $returnData )
{
$this->returnData = $returnData;
return $this;
}
/**
* @return int
*/
public function getClass(): int
{
return $this->class;
}
/**
* @param int $class
*
* @return PostMessageRequest
*/
public function setClass( int $class )
{
$this->class = $class;
return $this;
}
/**
* @return \DateTimeInterface
*/
public function getSendTime(): \DateTimeInterface
{
return $this->sendTime;
}
/**
* @param \DateTimeInterface $sendTime
*
* @return PostMessageRequest
*/
public function setSendTime( \DateTimeInterface $sendTime )
{
$this->sendTime = $sendTime;
return $this;
}
/**
* @return int
*/
public function getValidity(): int
{
return $this->validity;
}
/**
* @param int|\DateInterval $validity In minutes
*
* @return PostMessageRequest
*/
public function setValidity( $validity )
{
if ( $validity instanceof \DateInterval ) {
$now = new \DateTimeImmutable();
$seconds = $now->add( $validity )->getTimestamp() - $now->getTimestamp();
$validity = ceil( $seconds / 60 );
}
$validity = (int) $validity;
$this->validity = $validity;
return $this;
}
/**
* @return string
*/
public function getFormat(): string
{
return $this->format;
}
/**
* @param string $format
*
* @return PostMessageRequest
*/
public function setFormat( string $format )
{
$this->format = $format;
return $this;
}
/**
* @return string
*/
public function getUdh(): string
{
return $this->udh;
}
/**
* @param string $udh
*
* @return PostMessageRequest
*/
public function setUdh( string $udh )
{
$this->udh = $udh;
return $this;
}
/**
* @return array
*/
public function getAttachment(): array
{
return $this->attachment;
}
/**
* @param array $attachment
*
* @return PostMessageRequest
*/
public function setAttachment( array $attachment )
{
$this->attachment = $attachment;
return $this;
}
/**
* @return string
*/
public function getPushUrl(): string
{
return $this->pushUrl;
}
/**
* @param string $pushUrl
*
* @return PostMessageRequest
*/
public function setPushUrl( string $pushUrl )
{
$this->pushUrl = $pushUrl;
return $this;
}
/**
* @return string
*/
public function getPushExpire(): string
{
return $this->pushExpire;
}
/**
* @param string|\DateTimeInterface $pushExpire
*
* @return PostMessageRequest
*/
public function setPushExpire( $pushExpire )
{
if ( $pushExpire instanceof \DateTimeInterface ) {
$pushExpire = (string) $pushExpire->getTimestamp();
}
$this->pushExpire = $pushExpire;
return $this;
}
/**
* @return array
*/
public function getFilter(): array
{
return $this->filter;
}
/**
* @param array $filter
*
* @return PostMessageRequest
*/
public function setFilter( array $filter )
{
$this->filter = $filter;
return $this;
}
/**
* @return array
*/
public function getSegmentation(): array
{
return $this->segmentation;
}
/**
* @param array $segmentation
*
* @return PostMessageRequest
*/
public function setSegmentation( array $segmentation )
{
$this->segmentation = $segmentation;
return $this;
}
/**
* @return int
*/
public function getPid(): int
{
return $this->pid;
}
/**
* @param int $pid
*
* @return PostMessageRequest
*/
public function setPid( int $pid )
{
$this->pid = $pid;
return $this;
}
/**
* @return string
*/
public function getAdvanced(): string
{
return $this->advanced;
}
/**
* @param string $advanced
*
* @return PostMessageRequest
*/
public function setAdvanced( string $advanced )
{
$this->advanced = $advanced;
return $this;
}
/**
* @return string
*/
public function getProtocol(): string
{
return $this->protocol;
}
/**
* @param string $protocol
*
* @return PostMessageRequest
*/
public function setProtocol( string $protocol )
{
$this->protocol = $protocol;
return $this;
}
/**
* @return string
*/
public function getRevenueText(): string
{
return $this->revenueText;
}
/**
* @param string $revenueText
*
* @return PostMessageRequest
*/
public function setRevenueText( string $revenueText )
{
$this->revenueText = $revenueText;
return $this;
}
} }

View File

@ -13,6 +13,8 @@
* @link http://www.oxidmodule.com * @link http://www.oxidmodule.com
*/ */
declare( strict_types = 1 );
namespace D3\LinkmobilityClient; namespace D3\LinkmobilityClient;
class Url implements UrlInterface class Url implements UrlInterface
@ -26,76 +28,4 @@ class Url implements UrlInterface
{ {
return $this->baseUri; return $this->baseUri;
} }
/**
* @param float $lat
* @param float $lng
* @param float $radius
* @param string $sort
* @param string $type
*
* @return string
*/
public function getListUrl(float $lat, float $lng, float $radius, string $sort, string $type): string
{
$query = http_build_query(
[
'lat' => $lat,
'lng' => $lng,
'rad' => $radius,
'sort' => $sort,
'type' => $type,
'apikey'=> $this->apiKey
]
);
return "list.php?$query";
}
/**
* @param $stationId
*
* @return string
*/
public function getStationDetailUrl($stationId): string
{
$query = http_build_query(
[
'id' => $stationId,
'apikey'=> $this->apiKey
]
);
return "detail.php?$query";
}
/**
* @param array $stationList
*
* @return string
* @throws ApiException
*/
public function getPricesUrl(array $stationList): string
{
if (count($stationList) < 1 || count($stationList) > 10) {
throw new ApiException('Preisabfrage darf nur zwischen 1 und 10 Stationen beinhalten');
}
$query = http_build_query(
[
'ids' => implode( ',', $stationList),
'apikey'=> $this->apiKey
]
);
return "prices.php?$query";
}
/**
* @return string
*/
public function getComplaintUrl(): string
{
$query = http_build_query(['apikey'=> $this->apiKey]);
return $this->baseUri . "complaint.php?$query";
}
} }

View File

@ -5,7 +5,9 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject; namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert; use Assert\Assert;
use libphonenumber\PhoneNumberType; use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
class Recipient extends StringValueObject class Recipient extends StringValueObject
{ {
@ -18,11 +20,11 @@ class Recipient extends StringValueObject
{ {
Assert::that($iso2CountryCode)->string()->length(2); Assert::that($iso2CountryCode)->string()->length(2);
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $phoneUtil = PhoneNumberUtil::getInstance();
try { try {
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode)); $phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = ltrim($phoneUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::E164), '+'); $number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
} catch (\libphonenumber\NumberParseException $e) { } catch ( NumberParseException $e) {
var_dump($e); var_dump($e);
} }

View File

@ -5,22 +5,33 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject; namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert; use Assert\Assert;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
class Sender extends StringValueObject class Sender extends StringValueObject
{ {
/**
* @param string $number
* @param string $iso2CountryCode
*
* @throws RecipientException
*/
public function __construct(string $number, string $iso2CountryCode) public function __construct(string $number, string $iso2CountryCode)
{ {
Assert::that($iso2CountryCode)->string()->length(2); Assert::that($iso2CountryCode)->string()->length(2);
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $phoneUtil = PhoneNumberUtil::getInstance();
try { try {
$phoneNumber = $phoneUtil->parse( $number, strtoupper($iso2CountryCode) ); $phoneNumber = $phoneUtil->parse( $number, strtoupper($iso2CountryCode) );
$number = $phoneUtil->format( $phoneNumber, \libphonenumber\PhoneNumberFormat::E164 ); $number = $phoneUtil->format( $phoneNumber, PhoneNumberFormat::E164 );
if (false === $phoneUtil->isValidNumber($phoneNumber)) { if (false === $phoneUtil->isValidNumber($phoneNumber)) {
throw new \D3\LinkmobilityClient\Exceptions\RecipientException( 'invalid sender phone number' ); throw new RecipientException( ExceptionMessages::INVALID_SENDER );
} }
} catch (\libphonenumber\NumberParseException $e) { } catch ( NumberParseException $e) {
var_dump($e); var_dump($e);
} }

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject; namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert;
use Phlib\SmsLength\SmsLength; use Phlib\SmsLength\SmsLength;
class SmsMessage extends StringValueObject class SmsMessage extends StringValueObject
@ -31,10 +30,13 @@ class SmsMessage extends StringValueObject
return $smsLength->getMessageCount(); return $smsLength->getMessageCount();
} }
/**
* @return int
*/
public function length() : int public function length() : int
{ {
$smsLength = new SmsLength($this->value); $smsLength = new SmsLength($this->value);
$smsLength->getSize(); return $smsLength->getSize();
} }
public function isGsm7() : bool public function isGsm7() : bool