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
bovenliggende 05132f82ee
commit 05ff18d537
Getekend door: DanielS
GPG sleutel-ID: 8A7C4C6ED1915C6F
2 gewijzigde bestanden met toevoegingen van 19 en 23 verwijderingen

Bestand weergeven

@ -57,29 +57,7 @@ class RecipientsList implements RecipientsListInterface, Iterator
public function add(Recipient $recipient): RecipientsListInterface public function add(Recipient $recipient): RecipientsListInterface
{ {
$phoneUtil = $this->getPhoneNumberUtil(); $phoneUtil = $this->getPhoneNumberUtil();
try { $this->recipients[ md5(serialize($recipient)) ] = $recipient;
$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]);
}
return $this; return $this;
} }

Bestand weergeven

@ -16,8 +16,11 @@ 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\NumberParseException;
use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil; use libphonenumber\PhoneNumberUtil;
class Recipient extends StringValueObject class Recipient extends StringValueObject
@ -32,6 +35,7 @@ class Recipient extends StringValueObject
* @param string $iso2CountryCode * @param string $iso2CountryCode
* *
* @throws NumberParseException * @throws NumberParseException
* @throws RecipientException
*/ */
public function __construct(string $number, string $iso2CountryCode) public function __construct(string $number, string $iso2CountryCode)
{ {
@ -42,6 +46,20 @@ class Recipient extends StringValueObject
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode)); $phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164); $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); parent::__construct($number);
$this->countryCode = $iso2CountryCode; $this->countryCode = $iso2CountryCode;
} }