get sender address type only if sender is set

This commit is contained in:
Daniel Seifert 2022-07-14 11:58:31 +02:00
parent bd9c3e6c03
commit f1eae3bd87
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
5 changed files with 96 additions and 24 deletions

View File

@ -465,10 +465,55 @@ abstract class AbstractRequest extends ApiTestCase
/**
* @test
* @throws ReflectionException
* @dataProvider setGetSenderAddressTypeDataProvider
*/
public function setGetSenderAddressTypeTest()
public function testSetGetSenderAddressType($hasSender, $addressType, $expected)
{
$this->checkGetterSetter('fixture', 'setSenderAddressType', 'getSenderAddressType');
/** @var Request|MockObject $request */
$request = $this->getMockBuilder($this->testClassName)
->setConstructorArgs([$this->request->getMessage(), $this->request->getClient()])
->onlyMethods(['getSenderAddress'])
->getMock();
if ($hasSender) {
/** @var Sender|MockObject $senderMock */
$senderMock = $this->getMockBuilder( Sender::class )
->disableOriginalConstructor()
->onlyMethods(['get'])
->getMock();
$senderMock->method('get')->willReturn('fixture');
$request->method('getSenderAddress')->willReturn($senderMock);
} else {
$request->method('getSenderAddress')->willReturn(null);
}
$this->assertInstanceOf(
Request::class,
$this->callMethod(
$request,
'setSenderAddressType',
[$addressType]
)
);
$this->assertSame(
$expected,
$this->callMethod(
$request,
'getSenderAddressType'
)
);
}
/**
* @return array[]
*/
public function setGetSenderAddressTypeDataProvider(): array
{
return [
'has no sender' => [false, 'fixture', null],
'has sender and address type' => [true, 'fixture', 'fixture'],
];
}
/**

View File

@ -17,11 +17,13 @@ namespace D3\LinkmobilityClient\Exceptions;
class ExceptionMessages
{
public const INVALID_SENDER = 'invalid sender phone number';
public const INVALID_SENDER = 'invalid sender phone number';
public const NOK_REQUEST_RETURN = 'request %1$s returns status code %2$s';
public const NOK_REQUEST_RETURN = 'request %1$s returns status code %2$s';
public const INVALID_RECIPIENT_PHONE = 'invalid recipient phone number';
public const INVALID_RECIPIENT_PHONE = 'invalid recipient phone number';
public const NOT_A_MOBILE_NUMBER = 'not a mobile number';
public const NOT_A_MOBILE_NUMBER = 'not a mobile number';
public const DEBUG_NOSENDERORCOUNTRYCODE= 'no sender number or sender country code defined, use fallback to account default';
}

View File

@ -0,0 +1,21 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\LinkmobilityClient\Exceptions;
class NoSenderDefinedException extends LinkmobilityException
{
}

View File

@ -465,7 +465,7 @@ abstract class Request implements RequestInterface
*/
public function getSenderAddressType()
{
return $this->senderAddressType;
return $this->getSenderAddress() && $this->getSenderAddress()->get() ? $this->senderAddressType : null;
}
/**

View File

@ -17,7 +17,9 @@ namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Exceptions\NoSenderDefinedException;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use D3\LinkmobilityClient\LoggerHandler;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
@ -31,20 +33,30 @@ class Sender extends StringValueObject
* @throws RecipientException
* @throws NumberParseException
*/
public function __construct(string $number, string $iso2CountryCode)
public function __construct(string $number = null, string $iso2CountryCode = null)
{
Assert::that($iso2CountryCode)->string()->length(2);
try {
if ( is_null( $number ) || is_null( $iso2CountryCode ) ) {
throw new NoSenderDefinedException();
}
$phoneUtil = $this->getPhoneNumberUtil();
Assert::that( $iso2CountryCode )->string()->length( 2 );
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
$phoneUtil = $this->getPhoneNumberUtil();
if (false === $phoneUtil->isValidNumber($phoneNumber)) {
throw new RecipientException(ExceptionMessages::INVALID_SENDER);
$phoneNumber = $phoneUtil->parse( $number, strtoupper( $iso2CountryCode ) );
$number = ltrim( $phoneUtil->format( $phoneNumber, PhoneNumberFormat::E164 ), '+' );
if ( false === $phoneUtil->isValidNumber( $phoneNumber ) ) {
throw new RecipientException( ExceptionMessages::INVALID_SENDER );
}
parent::__construct( $number );
} catch (NoSenderDefinedException $e) {
LoggerHandler::getInstance()->getLogger()->debug(
ExceptionMessages::DEBUG_NOSENDERORCOUNTRYCODE
);
}
parent::__construct($number);
}
/**
@ -54,12 +66,4 @@ class Sender extends StringValueObject
{
return PhoneNumberUtil::getInstance();
}
/**
* @return int
*/
public function getFormatted()
{
return parent::getFormatted();
}
}