From 05ff18d537d2067c7f2d770ce63064af910d1b0c Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 18 Jul 2022 13:29:23 +0200 Subject: [PATCH] move recipient number check from recipient list to recipient itself --- src/RecipientsList/RecipientsList.php | 24 +----------------------- src/ValueObject/Recipient.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/RecipientsList/RecipientsList.php b/src/RecipientsList/RecipientsList.php index d7992b9..d3f1a4c 100644 --- a/src/RecipientsList/RecipientsList.php +++ b/src/RecipientsList/RecipientsList.php @@ -57,29 +57,7 @@ class RecipientsList implements RecipientsListInterface, Iterator public function add(Recipient $recipient): RecipientsListInterface { $phoneUtil = $this->getPhoneNumberUtil(); - try { - $phoneNumber = $phoneUtil->parse($recipient->get(), $recipient->getCountryCode()); - - if (false === $phoneUtil->isValidNumber($phoneNumber)) { - throw new RecipientException(ExceptionMessages::INVALID_RECIPIENT_PHONE); - } elseif ( - 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; - } catch (NumberParseException $e) { - $this->client->getLoggerHandler()->getLogger()->info($e->getMessage(), [$recipient]); - } catch (RecipientException $e) { - $this->client->getLoggerHandler()->getLogger()->info($e->getMessage(), [$recipient]); - } + $this->recipients[ md5(serialize($recipient)) ] = $recipient; return $this; } diff --git a/src/ValueObject/Recipient.php b/src/ValueObject/Recipient.php index 55d9eba..d978abc 100644 --- a/src/ValueObject/Recipient.php +++ b/src/ValueObject/Recipient.php @@ -16,8 +16,11 @@ declare(strict_types=1); namespace D3\LinkmobilityClient\ValueObject; use Assert\Assert; +use D3\LinkmobilityClient\Exceptions\ExceptionMessages; +use D3\LinkmobilityClient\Exceptions\RecipientException; use libphonenumber\NumberParseException; use libphonenumber\PhoneNumberFormat; +use libphonenumber\PhoneNumberType; use libphonenumber\PhoneNumberUtil; class Recipient extends StringValueObject @@ -32,6 +35,7 @@ class Recipient extends StringValueObject * @param string $iso2CountryCode * * @throws NumberParseException + * @throws RecipientException */ public function __construct(string $number, string $iso2CountryCode) { @@ -42,6 +46,20 @@ class Recipient extends StringValueObject $phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode)); $number = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164); + if (false === $phoneUtil->isValidNumber($phoneNumber)) { + throw new RecipientException(ExceptionMessages::INVALID_RECIPIENT_PHONE); + } elseif ( + false === in_array( + $phoneUtil->getNumberType($phoneNumber), + [ + PhoneNumberType::MOBILE, + PhoneNumberType::FIXED_LINE_OR_MOBILE + ] + ) + ) { + throw new RecipientException( ExceptionMessages::NOT_A_MOBILE_NUMBER); + } + parent::__construct($number); $this->countryCode = $iso2CountryCode; }