Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable save() method for User and enable SMS manipulation #17

Merged
merged 2 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/Users/UserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,28 @@ public function it_has_requests()
{
$this->requests->shouldHaveType(Requests::class);
}

public function it_has_sms()
{
$this->getSmsNumber()->shouldBe('87654321');
}

public function it_can_change_sms()
{
$this->setSmsNumber('12345678')->shouldBeCalled();
$this->getSmsNumber()->shouldBe('12345678');
}

public function it_can_add_sms()
{
$this->setSmsNumber('9999999')->shouldBeCalled();
$this->getSmsNumber()->shouldBe('9999999');
}

public function it_can_remove_sms()
{
$this->unsetSmsNumber()->shouldBeCalled();
$this->getSmsNumber()->shouldBe(null);
}

}
12 changes: 12 additions & 0 deletions spec/data/user_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
"preferred": true,
"preferred_sms": false,
"segment_type": "Internal"
},
{
"phone_number": "87654321",
"phone_type": [
{
"desc": "Mobile",
"value": "mobile"
}
],
"preferred": false,
"preferred_sms": true,
"segment_type": "Internal"
}
]
},
Expand Down
83 changes: 83 additions & 0 deletions src/Users/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,89 @@ public function getIdentifiers()
return $this->init()->_identifiers;
}

/**
* Get the user's preferred SMS number.
*
* @return string|null
*/
public function getSmsNumber()
{
$this->init();
if ($this->data->contact_info->phone) {
foreach ($this->data->contact_info->phone as $phone) {
if ($phone->preferred_sms) {
return $phone->phone_number;
}
}
}
return null;
}

/**
* Remove the preferred SMS flag from any number.
*/
public function unsetSmsNumber()
{
$this->init();
if ($this->data->contact_info->phone) {
foreach ($this->data->contact_info->phone as $phone) {
if ($phone->preferred_sms) {
$phone->preferred_sms = false;
}
}
}
}

/**
* Set the user's preferred SMS number, creating a new internal mobile number if needed
* @param $number string The SMS-capable mobile phone number
*/
public function setSmsNumber($number)
{
$currentNumber = $this->getSmsNumber();
if ($number === $currentNumber) {
return;
}
$this->unsetSmsNumber();
$updated = false;
if ($this->data->contact_info->phone) {
foreach ($this->data->contact_info->phone as $phone) {
if ($phone->phone_number === $number) {
$phone->preferred_sms = true;
}
}
}
if (!$updated) {
$this->addSmsNumber($number);
}
}

/**
* Add the user's preferred SMS number as a new internal mobile number.
* @param $number string The SMS-capable mobile phone number
*/
public function addSmsNumber($number)
{
$currentNumber = $this->getSmsNumber();
if ($currentNumber) {
$this->unsetSmsNumber();
}
$phones = json_decode(json_encode($this->data->contact_info->phone), true);
$phones[] = json_decode('{"phone_number":'.json_encode($number).',"preferred":false,"preferred_sms":true,"segment_type":"Internal","phone_type":[{"value":"mobile","desc":"Mobile"}]}', true);
$this->data->contact_info->phone = json_decode(json_encode($phones));
}

/**
* Save the user
*
* @return string The API response body
*/
public function save()
{
$this->init();
return $this->client->put($this->url(), json_encode($this->jsonSerialize()));
}

/**
* Check if we have the full representation of our data object.
*
Expand Down