Skip to content

Commit

Permalink
Merge pull request #1 from lsnepomuceno/dev
Browse files Browse the repository at this point in the history
feat: including perform get and get balance methods
  • Loading branch information
lsnepomuceno authored Jul 12, 2021
2 parents 8c5c8c4 + 13e4815 commit 644e492
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/SMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ public function __construct(string $login, string $pass)
$this->client = self::withOptions(['verify' => false])->baseUrl(self::URL);
}

/**
* cleanNumbers - Format numbers to Brazilian format without country code
*
* @param array $numbers
* @return array
*/
private function cleanNumbers(array $numbers): array
{
return array_map(function ($number) {
$number = preg_replace("/\D/", '', $number);
return substr($number, -11);
}, $numbers);
}

/**
* performGet - Prepare get
*
* @throws \Exception
*
* @return \Illuminate\Http\Client\Response
*/
private function performGet(): Response
{
$response = $this->client->get('', $this->params);

if (!$response->body()['status']) {
throw new \Exception($response->body()['msg']);
}

return $response;
}

/**
* send - Send sms mesages from defined numbers
*
Expand All @@ -57,20 +89,22 @@ public function send(array $numbers, string $message): Response
'numbers' => join(',', $this->cleanNumbers($numbers))
];

return $this->client->get('', $this->params);
return $this->performGet();
}

/**
* cleanNumbers - Format numbers to Brazilian format without country code
* getBalance - Receive sms balance for account
*
* @param array $numbers
* @return array
* @return \Illuminate\Http\Client\Response
*/
private function cleanNumbers(array $numbers): array
public function getBalance(): Response
{
return array_map(function ($number) {
$number = preg_replace("/\D/", '', $number);
return substr($number, -11);
}, $numbers);
$this->params = [
'action' => 'getbalance',
'lgn' => $this->login,
'pwd' => $this->pass
];

return $this->performGet();
}
}

0 comments on commit 644e492

Please sign in to comment.