move recipient number check from recipient list to recipient itself

This commit is contained in:
Daniel Seifert 2022-07-18 13:29:23 +02:00
parent 05132f82ee
commit 05ff18d537
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
2 changed files with 19 additions and 23 deletions

View File

@ -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;
}

View File

@ -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;
}