From c502926daff4864a5513ba05256146df8862162e Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 1 Jul 2022 09:24:16 +0200 Subject: [PATCH] inject logger, do not catch exceptions that should lead to termination --- README.md | 5 +- src/Client.php | 47 +++++++++++++++++-- src/RecipientsList/RecipientsList.php | 35 +++++++++++++- .../RecipientsListInterface.php | 3 ++ src/Request/Request.php | 27 ++++++++++- src/Request/RequestInterface.php | 7 +++ src/Response/Response.php | 2 + src/SMS/BinaryRequest.php | 5 +- src/SMS/RequestFactory.php | 12 +++-- src/SMS/RequestFactoryInterface.php | 4 +- src/SMS/SmsRequestInterface.php | 7 +-- src/SMS/TextRequest.php | 5 +- src/ValueObject/Recipient.php | 15 +++--- src/ValueObject/Sender.php | 14 +++--- src/ValueObject/SmsBinaryMessage.php | 3 ++ src/ValueObject/SmsMessageAbstract.php | 2 +- src/ValueObject/SmsMessageInterface.php | 2 +- src/ValueObject/SmsTextMessage.php | 2 + 18 files changed, 157 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index ac89790..5141ddd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ ``` $client = new Client('accesstoken'); -$request = (new \D3\LinkmobilityClient\SMS\Request('me', 'message')) - ->addRecipient(new Recipient('recipient')); +$client->setLogger($logger); // optional +$request = new D3\LinkmobilityClient\SMS\RequestFactory($message, $client)->getSmsRequest()) + ->addRecipient(new D3\LinkmobilityClient\ValueObject\Recipient('recipient', 'DE')); $client->request($request) ``` \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index 53c85bb..eb01996 100644 --- a/src/Client.php +++ b/src/Client.php @@ -21,7 +21,9 @@ use D3\LinkmobilityClient\Exceptions\ApiException; use D3\LinkmobilityClient\Exceptions\ExceptionMessages; use D3\LinkmobilityClient\Request\RequestInterface; use GuzzleHttp\Exception\GuzzleException; +use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; +use Psr\Log\LoggerInterface; use RuntimeException; class Client @@ -30,6 +32,8 @@ class Client public $apiUrl; public $requestClient; + private $logger; + public function __construct(string $accessToken, $apiUrl = false, $client = false) { if ($apiUrl !== false && false === $apiUrl instanceof UrlInterface) { @@ -47,6 +51,7 @@ class Client * @return Response\ResponseInterface * @throws ApiException * @throws GuzzleException + * @throws InvalidArgumentException */ public function request(RequestInterface $request) : Response\ResponseInterface { @@ -70,6 +75,8 @@ class Client { $options['headers']['Authorization'] = 'Bearer '.$this->accessToken; + if ($this->hasLogger()) $this->getLogger()->debug('request '.$url, $options); + $response = $this->requestClient->request( $method, $url, @@ -77,11 +84,45 @@ class Client ); if ($response->getStatusCode() != 200) { - throw new ApiException( - sprintf(ExceptionMessages::NOK_REQUEST_RETURN, [$url, $response->getStatusCode()]) - ); + $message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, [$url, $response->getStatusCode()]); + if ($this->hasLogger()) $this->getLogger()->error($message); + throw new ApiException($message); + } + + if ($this->hasLogger()) { + $response->getBody()->rewind(); + $this->getLogger()->debug('response', [$response->getBody()->getContents()]); } return $response; } + + /** + * @param mixed $logger + * + * @return Client + */ + public function setLogger(LoggerInterface $logger ) : Client + { + $this->logger = $logger; + + return $this; + } + + /** + * @return bool + */ + public function hasLogger() : bool + { + return $this->logger instanceof LoggerInterface; + } + + /** + * @return LoggerInterface + */ + public function getLogger(): LoggerInterface + { + return $this->logger; + } + } \ No newline at end of file diff --git a/src/RecipientsList/RecipientsList.php b/src/RecipientsList/RecipientsList.php index 1a772ed..c831181 100644 --- a/src/RecipientsList/RecipientsList.php +++ b/src/RecipientsList/RecipientsList.php @@ -17,6 +17,7 @@ declare(strict_types=1); namespace D3\LinkmobilityClient\RecipientsList; +use D3\LinkmobilityClient\Client; use D3\LinkmobilityClient\Exceptions\ExceptionMessages; use D3\LinkmobilityClient\Exceptions\RecipientException; use D3\LinkmobilityClient\ValueObject\Recipient; @@ -27,11 +28,21 @@ use libphonenumber\PhoneNumberUtil; class RecipientsList implements RecipientsListInterface, Iterator { + /** + * @var Client + */ + private $client; + /** * @var array */ private $recipients = []; + public function __construct( Client $client ) + { + $this->setClient($client); + } + public function add(Recipient $recipient) : RecipientsListInterface { $phoneUtil = PhoneNumberUtil::getInstance(); @@ -54,9 +65,13 @@ class RecipientsList implements RecipientsListInterface, Iterator $this->recipients[ md5( serialize( $recipient ) ) ] = $recipient; } catch (NumberParseException $e) { -// var_dump($e); + if ($this->getClient()->hasLogger()) { + $this->getClient()->getLogger()->info($e->getMessage()); + } } catch (RecipientException $e) { -// var_dump($e); + if ($this->getClient()->hasLogger()) { + $this->getClient()->getLogger()->info($e->getMessage()); + } } return $this; @@ -119,4 +134,20 @@ class RecipientsList implements RecipientsListInterface, Iterator { return current($this->recipients) instanceof Recipient; } + + /** + * @return Client + */ + public function getClient(): Client + { + return $this->client; + } + + /** + * @param Client $client + */ + public function setClient( Client $client ) + { + $this->client = $client; + } } \ No newline at end of file diff --git a/src/RecipientsList/RecipientsListInterface.php b/src/RecipientsList/RecipientsListInterface.php index 5e10673..4489d3d 100644 --- a/src/RecipientsList/RecipientsListInterface.php +++ b/src/RecipientsList/RecipientsListInterface.php @@ -17,10 +17,13 @@ declare(strict_types=1); namespace D3\LinkmobilityClient\RecipientsList; +use D3\LinkmobilityClient\Client; use D3\LinkmobilityClient\ValueObject\Recipient; interface RecipientsListInterface { + public function __construct(Client $client); + public function add(Recipient $recipient) : RecipientsListInterface; public function clearRecipents() : RecipientsListInterface; diff --git a/src/Request/Request.php b/src/Request/Request.php index 3f7d928..f623709 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -18,6 +18,7 @@ declare( strict_types = 1 ); namespace D3\LinkmobilityClient\Request; use Assert\Assert; +use D3\LinkmobilityClient\Client; use D3\LinkmobilityClient\RecipientsList\RecipientsList; use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface; use D3\LinkmobilityClient\Response\ResponseInterface; @@ -36,6 +37,11 @@ abstract class Request implements RequestInterface */ private $message; + /** + * @var Client + */ + private $client; + /** * @var string */ @@ -109,10 +115,11 @@ abstract class Request implements RequestInterface /** * @param SmsMessageAbstract $message */ - public function __construct(SmsMessageInterface $message) + public function __construct(SmsMessageInterface $message, Client $client) { - $this->recipientsList = new RecipientsList(); + $this->recipientsList = new RecipientsList($client); $this->setMessage( $message ); + $this->setClient($client); return $this; } @@ -495,4 +502,20 @@ abstract class Request implements RequestInterface $FQClassName = $this->getResponseClass(); return new $FQClassName($rawResponse); } + + /** + * @return Client + */ + public function getClient(): Client + { + return $this->client; + } + + /** + * @param Client $client + */ + public function setClient( Client $client ) + { + $this->client = $client; + } } \ No newline at end of file diff --git a/src/Request/RequestInterface.php b/src/Request/RequestInterface.php index 31558d8..d19c6da 100644 --- a/src/Request/RequestInterface.php +++ b/src/Request/RequestInterface.php @@ -17,6 +17,9 @@ declare(strict_types=1); namespace D3\LinkmobilityClient\Request; +use D3\LinkmobilityClient\Client; +use D3\LinkmobilityClient\ValueObject\SmsMessageInterface; +use InvalidArgumentException; use Psr\Http\Message\ResponseInterface as PsrResponseInterface; use D3\LinkmobilityClient\Response\ResponseInterface as LMResponseInterface; @@ -41,6 +44,8 @@ interface RequestInterface const SENDERADDRESSTYPE_ALPHANUMERIC = 'alphanumeric'; const SENDERADDRESSTYPE_SHORTCODE = 'shortcode'; + public function __construct(SmsMessageInterface $message, Client $client); + public function setMethod(string $method); /** @@ -89,6 +94,8 @@ interface RequestInterface * Must validate the input of the request * This is called before sending the request * Must throw an exception if the validation fails + * + * @throws InvalidArgumentException */ public function validate(); } \ No newline at end of file diff --git a/src/Response/Response.php b/src/Response/Response.php index 61546f0..2ceff07 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -27,6 +27,8 @@ abstract class Response implements ResponseInterface public function __construct(\Psr\Http\Message\ResponseInterface $rawResponse) { $this->rawResponse = $rawResponse; + + $this->rawResponse->getBody()->rewind(); $this->content = json_decode($this->rawResponse->getBody()->getContents(), true); } diff --git a/src/SMS/BinaryRequest.php b/src/SMS/BinaryRequest.php index d7fc105..b10ee9e 100644 --- a/src/SMS/BinaryRequest.php +++ b/src/SMS/BinaryRequest.php @@ -20,7 +20,7 @@ namespace D3\LinkmobilityClient\SMS; use Assert\Assert; use D3\LinkmobilityClient\Request\Request; use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage; -use D3\LinkmobilityClient\ValueObject\SmsMessage; +use InvalidArgumentException; class BinaryRequest extends Request implements SmsRequestInterface { @@ -42,6 +42,9 @@ class BinaryRequest extends Request implements SmsRequestInterface ); } + /** + * @throws InvalidArgumentException + */ public function validate() { parent::validate(); diff --git a/src/SMS/RequestFactory.php b/src/SMS/RequestFactory.php index 2b3d872..c604e6f 100644 --- a/src/SMS/RequestFactory.php +++ b/src/SMS/RequestFactory.php @@ -17,7 +17,9 @@ declare( strict_types = 1 ); namespace D3\LinkmobilityClient\SMS; +use D3\LinkmobilityClient\Client; use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage; +use D3\LinkmobilityClient\ValueObject\SmsMessageInterface; use D3\LinkmobilityClient\ValueObject\SmsTextMessage; use Phlib\SmsLength\SmsLength; @@ -34,24 +36,26 @@ class RequestFactory const GSM_UCS2 = 'ucs-2'; private $message; + private $client; - public function __construct($message) + public function __construct($message, Client $client) { $this->message = $message; + $this->client = $client; } /** * @return SmsRequestInterface */ - public function getRequest() : SmsRequestInterface + public function getSmsRequest() : SmsRequestInterface { $smsLength = new SmsLength($this->message); if ($smsLength->getEncoding() === self::GSM_7BIT) { $message = new SmsTextMessage($this->message); - return new TextRequest($message); + return new TextRequest($message, $this->client); } $message = new SmsBinaryMessage($this->message); - return new BinaryRequest($message); + return new BinaryRequest($message, $this->client); } } \ No newline at end of file diff --git a/src/SMS/RequestFactoryInterface.php b/src/SMS/RequestFactoryInterface.php index 326e9cd..9fbdee2 100644 --- a/src/SMS/RequestFactoryInterface.php +++ b/src/SMS/RequestFactoryInterface.php @@ -17,11 +17,9 @@ declare( strict_types = 1 ); namespace D3\LinkmobilityClient\SMS; -use D3\LinkmobilityClient\ValueObject\SmsMessage; - interface RequestFactoryInterface { - public function __construct(SmsMessage $message); + public function __construct($message); public function getRequest() : SmsRequestInterface; } \ No newline at end of file diff --git a/src/SMS/SmsRequestInterface.php b/src/SMS/SmsRequestInterface.php index 5c1a55b..02bce86 100644 --- a/src/SMS/SmsRequestInterface.php +++ b/src/SMS/SmsRequestInterface.php @@ -15,9 +15,4 @@ namespace D3\LinkmobilityClient\SMS; -use D3\LinkmobilityClient\ValueObject\SmsMessageInterface; - -interface SmsRequestInterface -{ - public function __construct(SmsMessageInterface $message); -} \ No newline at end of file +interface SmsRequestInterface {} \ No newline at end of file diff --git a/src/SMS/TextRequest.php b/src/SMS/TextRequest.php index 0e50283..83da1dc 100644 --- a/src/SMS/TextRequest.php +++ b/src/SMS/TextRequest.php @@ -19,8 +19,8 @@ namespace D3\LinkmobilityClient\SMS; use Assert\Assert; use D3\LinkmobilityClient\Request\Request; -use D3\LinkmobilityClient\ValueObject\SmsMessageInterface; use D3\LinkmobilityClient\ValueObject\SmsTextMessage; +use InvalidArgumentException; class TextRequest extends Request implements SmsRequestInterface { @@ -32,6 +32,9 @@ class TextRequest extends Request implements SmsRequestInterface return '/rest/smsmessaging/text'; } + /** + * @throws InvalidArgumentException + */ public function validate() { parent::validate(); diff --git a/src/ValueObject/Recipient.php b/src/ValueObject/Recipient.php index bcf8897..d6cc086 100644 --- a/src/ValueObject/Recipient.php +++ b/src/ValueObject/Recipient.php @@ -16,17 +16,20 @@ class Recipient extends StringValueObject */ private $countryCode; + /** + * @param string $number + * @param string $iso2CountryCode + * + * @throws NumberParseException + */ public function __construct(string $number, string $iso2CountryCode) { Assert::that($iso2CountryCode)->string()->length(2); $phoneUtil = PhoneNumberUtil::getInstance(); - try { - $phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode)); - $number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+'); - } catch ( NumberParseException $e) { - var_dump($e); - } + + $phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode)); + $number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+'); parent::__construct($number); $this->countryCode = $iso2CountryCode; diff --git a/src/ValueObject/Sender.php b/src/ValueObject/Sender.php index 1e89c21..c835e9d 100644 --- a/src/ValueObject/Sender.php +++ b/src/ValueObject/Sender.php @@ -18,21 +18,19 @@ class Sender extends StringValueObject * @param string $iso2CountryCode * * @throws RecipientException + * @throws NumberParseException */ public function __construct(string $number, string $iso2CountryCode) { Assert::that($iso2CountryCode)->string()->length(2); $phoneUtil = PhoneNumberUtil::getInstance(); - try { - $phoneNumber = $phoneUtil->parse( $number, strtoupper($iso2CountryCode) ); - $number = $phoneUtil->format( $phoneNumber, PhoneNumberFormat::E164 ); - if (false === $phoneUtil->isValidNumber($phoneNumber)) { - throw new RecipientException( ExceptionMessages::INVALID_SENDER ); - } - } catch ( NumberParseException $e) { - var_dump($e); + $phoneNumber = $phoneUtil->parse( $number, strtoupper($iso2CountryCode) ); + $number = $phoneUtil->format( $phoneNumber, PhoneNumberFormat::E164 ); + + if (false === $phoneUtil->isValidNumber($phoneNumber)) { + throw new RecipientException( ExceptionMessages::INVALID_SENDER ); } parent::__construct( $number); diff --git a/src/ValueObject/SmsBinaryMessage.php b/src/ValueObject/SmsBinaryMessage.php index 6749f50..84e502c 100644 --- a/src/ValueObject/SmsBinaryMessage.php +++ b/src/ValueObject/SmsBinaryMessage.php @@ -19,6 +19,9 @@ class SmsBinaryMessage extends SmsMessageAbstract $smsLength->validate(); } + /** + * @return array|false|string + */ public function getMessageContent() { return str_split( diff --git a/src/ValueObject/SmsMessageAbstract.php b/src/ValueObject/SmsMessageAbstract.php index 6e65208..baea32a 100644 --- a/src/ValueObject/SmsMessageAbstract.php +++ b/src/ValueObject/SmsMessageAbstract.php @@ -26,7 +26,7 @@ abstract class SmsMessageAbstract extends StringValueObject implements SmsMessag return $smsLength->getSize(); } - public function getMessageContent() + public function getMessageContent() : string { return (string) $this->value; } diff --git a/src/ValueObject/SmsMessageInterface.php b/src/ValueObject/SmsMessageInterface.php index 34e927a..f5e4a23 100644 --- a/src/ValueObject/SmsMessageInterface.php +++ b/src/ValueObject/SmsMessageInterface.php @@ -21,5 +21,5 @@ interface SmsMessageInterface public function length() : int; - public function getMessageContent(); + public function getMessageContent() : string; } \ No newline at end of file diff --git a/src/ValueObject/SmsTextMessage.php b/src/ValueObject/SmsTextMessage.php index 19d917f..ca86b8b 100644 --- a/src/ValueObject/SmsTextMessage.php +++ b/src/ValueObject/SmsTextMessage.php @@ -4,12 +4,14 @@ declare(strict_types=1); namespace D3\LinkmobilityClient\ValueObject; +use InvalidArgumentException; use Phlib\SmsLength\SmsLength; class SmsTextMessage extends SmsMessageAbstract { /** * @param string $message + * @throws InvalidArgumentException */ public function __construct(string $message) {