Skip to content

Commit

Permalink
Update method signatures and improve data handling
Browse files Browse the repository at this point in the history
Method signatures for `Loyalty\SDK\Services\Admin\Contacts::add` are updated to make `$mobilePhone` nullable. The return types of `getById` methods have been changed. Also, data handling in the `MobilePhoneItemResult` class has been improved to better handle instances where the `PhoneNumberUtil` instance is not created. Tests are updated to reflect these changes.

Signed-off-by: B24io <[email protected]>
  • Loading branch information
b24io-sdk committed Jul 9, 2024
1 parent 9492802 commit bbe2eac
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
### Changed

* change signature for method `Loyalty\SDK\Services\Admin\Contacts::add`, argument `$mobilePhone` can be nullable.
* change signature for method `Loyalty\SDK\Services\Admin\Contacts::getById` from `ContactItemResult` to `ContactResult`
* WIP change signature for method `Loyalty\SDK\Services\Admin\Cards::getById` from `` to ``

## 3.2.0 (2024.05.23)

Expand Down
15 changes: 15 additions & 0 deletions src/Common/Result/Contacts/ContactResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

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

use B24io\Loyalty\SDK\Core\Result\AbstractResult;

class ContactResult extends AbstractResult
{
public function getContact(): ContactItemResult
{
return new ContactItemResult($this->getCoreResponse()->getResponseData()->result);
}
}
8 changes: 7 additions & 1 deletion src/Common/Result/Contacts/MobilePhoneItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace B24io\Loyalty\SDK\Common\Result\Contacts;

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


/**
Expand All @@ -24,7 +26,11 @@ public function __get($offset)
{
switch ($offset) {
case 'number':
return $this->phoneNumberUtil->parse($this->data[$offset], null);
$phoneNumberUtil = PhoneNumberUtil::getInstance();
if ($phoneNumberUtil === null) {
throw new InvalidArgumentException('cannot create libphonenumber util instance');
}
return $phoneNumberUtil->parse($this->data[$offset], null);
case 'verificationStatus':
return new VerificationStatus($this->data['verification_status']);
default:
Expand Down
3 changes: 0 additions & 3 deletions src/Core/Result/AbstractItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ abstract class AbstractItem implements IteratorAggregate
*/
protected array $data;
protected DecimalMoneyParser $decimalMoneyParser;
protected PhoneNumberUtil $phoneNumberUtil;

/**
* @param array<string, mixed> $data
*/
public function __construct(array $data)
{
$this->data = $data;
$this->decimalMoneyParser = new DecimalMoneyParser(new ISOCurrencies());
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Services/Admin/Contacts/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use B24io\Loyalty\SDK\Common\FullName;
use B24io\Loyalty\SDK\Common\Gender;
use B24io\Loyalty\SDK\Common\Requests\ItemsOrder;
use B24io\Loyalty\SDK\Common\Result\Cards\CardsResult;
use B24io\Loyalty\SDK\Common\Result\Contacts\AddedContactResult;
use B24io\Loyalty\SDK\Common\Result\Contacts\ContactItemResult;
use B24io\Loyalty\SDK\Common\Result\Contacts\ContactResult;
use B24io\Loyalty\SDK\Common\Result\Contacts\ContactsResult;
use B24io\Loyalty\SDK\Core\Command;
use B24io\Loyalty\SDK\Core\Credentials\Context;
Expand Down Expand Up @@ -67,16 +66,16 @@ public function add(
)));
}

public function getById(Uuid $id): ContactItemResult
public function getById(Uuid $id): ContactResult
{
return new ContactItemResult(
return new ContactResult(
$this->core->call(
new Command(
Context::admin(),
RequestMethodInterface::METHOD_GET,
sprintf('contacts/%s', $id->toRfc4122()),
)
)->getResponseData()->result
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Services/Admin/Contacts/ContactsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testAddWithMobilePhone(): void
);

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

/**
Expand All @@ -112,7 +112,7 @@ public function testAddWithoutMobilePhone(): void
);

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

/**
Expand Down

0 comments on commit bbe2eac

Please sign in to comment.