Skip to content

Commit

Permalink
Update card and contact addition functionality for mobile phones
Browse files Browse the repository at this point in the history
This commit integrates mobile phone handling in the card and contact addition process. Both additions of contacts with and without mobile phones are now supported, which are also covered in the new test cases. The mobile phone is also now accounted for in the card verification process. Additionally, HTTP client timeout has been reduced from 60 to 30.

Signed-off-by: B24io <[email protected]>
  • Loading branch information
b24io-sdk committed Jul 9, 2024
1 parent 861b0b2 commit 9492802
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/Common/Result/Cards/CardItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace B24io\Loyalty\SDK\Common\Result\Cards;

use B24io\Loyalty\SDK\Common\Result\Contacts\ContactItemResult;
use B24io\Loyalty\SDK\Common\Result\Contacts\MobilePhoneItemResult;
use B24io\Loyalty\SDK\Core\Exceptions\InvalidArgumentException;
use B24io\Loyalty\SDK\Core\Result\AbstractItem;
use DateTimeImmutable;
use Exception;
use Money\Currency;
use Money\Money;
use MoneyPHP\Percentage\Percentage;
Expand All @@ -25,13 +27,14 @@
* @property-read string $externalId
* @property-read ?CardLevelItemResult $level
* @property-read ContactItemResult $contact
* @property-read ?MobilePhoneItemResult $mobilePhone
*/
class CardItemResult extends AbstractItem
{
/**
* @param int|string $offset
* @throws InvalidArgumentException
* @throws \Exception
* @throws Exception
*/
public function __get($offset)
{
Expand All @@ -57,6 +60,11 @@ public function __get($offset)
return null;
}
return new ContactItemResult($this->data[$offset]);
case 'mobilePhone':
if ($this->data['mobile_phone'] === null) {
return null;
}
return new MobilePhoneItemResult($this->data['mobile_phone']);
default:
return parent::__get($offset);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Common/Result/Contacts/ContactItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
* @property-read DateTimeImmutable $created
* @property-read DateTimeImmutable $modified
* @property-read ?CardItemResult $card
* @property-read ?MobilePhoneItemResult $mobilePhone
*/
class ContactItemResult extends AbstractItem
{
/**
* @param int|string $offset
* @throws InvalidArgumentException
* @throws \Exception
*/
public function __get($offset)
{
Expand Down Expand Up @@ -56,6 +58,11 @@ public function __get($offset)
return null;
}
return new CardItemResult($this->data['card']);
case 'mobilePhone':
if ($this->data['mobile_phone'] === null) {
return null;
}
return new MobilePhoneItemResult($this->data['mobile_phone']);
default:
return parent::__get($offset);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Common/Result/Contacts/MobilePhoneItemResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace B24io\Loyalty\SDK\Common\Result\Contacts;

use B24io\Loyalty\SDK\Common\VerificationStatus;
use B24io\Loyalty\SDK\Core\Result\AbstractItem;
use Exception;
use libphonenumber\PhoneNumber;


/**
* @property-read VerificationStatus $verificationStatus
* @property-read PhoneNumber $number
*/
class MobilePhoneItemResult extends AbstractItem
{
/**
* @param int|string $offset
* @throws Exception
*/
public function __get($offset)
{
switch ($offset) {
case 'number':
return $this->phoneNumberUtil->parse($this->data[$offset], null);
case 'verificationStatus':
return new VerificationStatus($this->data['verification_status']);
default:
return parent::__get($offset);
}
}
}
13 changes: 9 additions & 4 deletions src/Services/Admin/Contacts/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ class Contacts extends AbstractService
* @param FullName $fullName
* @param DateTimeZone $timezone
* @param Gender $gender
* @param PhoneNumber $mobilePhone
* @param PhoneNumber|null $mobilePhone
* @param DateTimeImmutable|null $birthdate
* @param array<string, string> $externalIds
*@return AddedContactResult
* @return AddedContactResult
*/
public function add(
FullName $fullName,
DateTimeZone $timezone,
Gender $gender,
PhoneNumber $mobilePhone,
?PhoneNumber $mobilePhone = null,
?DateTimeImmutable $birthdate = null,
array $externalIds = []
): AddedContactResult
{
$rawMobilePhone = null;
if ($mobilePhone !== null) {
$rawMobilePhone = $this->phoneNumberUtil->format($mobilePhone, PhoneNumberFormat::E164);
}

return new AddedContactResult($this->core->call(
new Command(
Context::admin(),
Expand All @@ -53,7 +58,7 @@ public function add(
'timezone' => $timezone->getName(),
'gender' => (string)$gender,
'birthday' => ($nullsafeBirthdate = $birthdate) ? $nullsafeBirthdate->format('Y.m.d') : null,
'mobile_phone' => $this->phoneNumberUtil->format($mobilePhone, PhoneNumberFormat::E164),
'mobile_phone' => $rawMobilePhone,
'external_ids' => $externalIds
],
null,
Expand Down
9 changes: 7 additions & 2 deletions src/Services/ServiceBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

class ServiceBuilderFactory
{
public static function createAdminRoleServiceBuilder(string $apiEndpointUrl, string $apiClientId, string $apiAdminKey, LoggerInterface $logger, ?HttpClientInterface $httpClient = null): AdminServiceBuilder
public static function createAdminRoleServiceBuilder(
string $apiEndpointUrl,
string $apiClientId,
string $apiAdminKey,
LoggerInterface $logger,
?HttpClientInterface $httpClient = null): AdminServiceBuilder
{
return new AdminServiceBuilder(
self::getCore(
Expand Down Expand Up @@ -76,7 +81,7 @@ public static function getDefaultHttpClient(): HttpClientInterface
return new CurlHttpClient(
[
'http_version' => '2.0',
'timeout' => 60,
'timeout' => 30,
],
);
}
Expand Down
14 changes: 9 additions & 5 deletions tests/Integration/Services/Admin/Cards/CardsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use MoneyPHP\Percentage\Percentage;
use PHPUnit\Framework\TestCase;
use Fig\Http\Message\StatusCodeInterface;
use Random\RandomException;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Faker;
use Throwable;
Expand Down Expand Up @@ -52,20 +51,24 @@ public function testGetCardsListWithoutArguments(): void
/**
* @throws BaseException
* @throws NumberParseException
* @testdox Test add card and contact with mobile phone
* @covers \B24io\Loyalty\SDK\Services\Admin\Contacts\Contacts::add
* @covers \B24io\Loyalty\SDK\Services\Admin\Cards\Cards::add
*/
public function testAddCard(): void
{
$phone = $this->phoneNumberUtil->parse(
$this->faker->phoneNumber,
'RU'
);
$addedContact = $this->sb->contactsScope()->contacts()->add(
new FullName(
$this->faker->firstName(),
$this->faker->lastName(),
),
new DateTimeZone('Europe/Moscow'),
Gender::male(),
$this->phoneNumberUtil->parse(
$this->faker->phoneNumber,
'RU'
)
$phone
);

$contactId = $addedContact->getContact()->id;
Expand Down Expand Up @@ -104,6 +107,7 @@ public function testAddCard(): void
$cardStatus,
$addedCard->getCard()->status
);
$this->assertTrue($phone->equals($addedCard->getCard()->mobilePhone->number));
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/Integration/Services/Admin/Contacts/ContactsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,63 @@ public function testAdd(): void
);
}

/**
* @throws TransportExceptionInterface|BaseException|NumberParseException
* @testdox Test add contact with mobile phone
* @covers \B24io\Loyalty\SDK\Services\Admin\Contacts\Contacts::add
* @covers \B24io\Loyalty\SDK\Services\Admin\Contacts\Contacts::getById
*/
public function testAddWithMobilePhone(): void
{
$phoneNumber = $this->phoneNumberUtil->parse(
$this->faker->phoneNumber,
'RU'
);
$addedContact = $this->sb->contactsScope()->contacts()->add(
new FullName(
$this->faker->firstName(),
$this->faker->lastName(),
),
new DateTimeZone('Europe/Moscow'),
Gender::male(),
$phoneNumber
);
$this->assertEquals(
StatusCodeInterface::STATUS_OK,
$addedContact->getCoreResponse()->httpResponse->getStatusCode()
);

$contact = $this->sb->contactsScope()->contacts()->getById($addedContact->getContact()->id);
$this->assertTrue($phoneNumber->equals($contact->mobilePhone->number));
}

/**
* @throws TransportExceptionInterface|BaseException|NumberParseException
* @testdox Test add contact without mobile phone
* @covers \B24io\Loyalty\SDK\Services\Admin\Contacts\Contacts::add
* @covers \B24io\Loyalty\SDK\Services\Admin\Contacts\Contacts::getById
*/
public function testAddWithoutMobilePhone(): void
{

$addedContact = $this->sb->contactsScope()->contacts()->add(
new FullName(
$this->faker->firstName(),
$this->faker->lastName(),
),
new DateTimeZone('Europe/Moscow'),
Gender::male(),
null
);
$this->assertEquals(
StatusCodeInterface::STATUS_OK,
$addedContact->getCoreResponse()->httpResponse->getStatusCode()
);

$contact = $this->sb->contactsScope()->contacts()->getById($addedContact->getContact()->id);
$this->assertNull($contact->mobilePhone);
}

/**
* @throws TransportExceptionInterface|BaseException
* @testdox Get contacts list with null arguments - test default options on server side
Expand Down

0 comments on commit 9492802

Please sign in to comment.