Skip to content

Commit

Permalink
Merge pull request #26 from Insolita/feature/global_partner
Browse files Browse the repository at this point in the history
ability to global partner id setup
  • Loading branch information
zelenin authored Jun 3, 2017
2 parents 47205f6 + 19dc5fa commit 71f179d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 72 deletions.
130 changes: 70 additions & 60 deletions Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class Api
* @var AuthInterface
*/
private $auth;

/**
* @var ClientInterface
*/
private $client;

/**
* @param AuthInterface $auth
*/
Expand All @@ -41,18 +41,17 @@ public function __construct(AuthInterface $auth)
$this->auth = $auth;
$this->auth->setContext($this);
}

/**
* @param AbstractSms $sms
*
* @return SmsResponse
*
* @throws Exception
*/
public function smsSend(AbstractSms $sms)
{
$params = [];

if ($sms instanceof Sms) {
$params['to'] = $sms->to;
$params['text'] = $sms->text;
Expand All @@ -63,32 +62,34 @@ public function smsSend(AbstractSms $sms)
} else {
throw new Exception('Only Sms or SmsPool instances');
}

if ($sms->from) {
$params['from'] = $sms->from;
}

if ($sms->time && $sms->time < (time() + 7 * 24 * 60 * 60)) {
$params['time'] = $sms->time;
}

if ($sms->translit) {
$params['translit'] = 1;
}

if ($sms->test) {
$params['test'] = 1;
}

if ($sms->partner_id) {
$params['partner_id'] = $sms->partner_id;
} elseif ($this->getAuth()->getPartnerId()) {
$params['partner_id'] = $this->getAuth()->getPartnerId();
}

$response = $this->request('sms/send', $params);
$response = explode("\n", $response);

$smsResponse = new SmsResponse(array_shift($response));

if ($smsResponse->code == 100) {
foreach ($response as $id) {
if (!preg_match('/=/', $id)) {
Expand All @@ -100,26 +101,29 @@ public function smsSend(AbstractSms $sms)
// }
}
}

return $smsResponse;
}

/**
* @param string $id
*
* @return SmsStatusResponse
*/
public function smsStatus($id)
{
$response = $this->request('sms/status', [
'id' => $id,
]);

$response = $this->request(
'sms/status',
[
'id' => $id,
]
);

$response = explode("\n", $response);

return new SmsStatusResponse(array_shift($response));
}

/**
* @param Sms $sms
*
Expand All @@ -128,79 +132,79 @@ public function smsStatus($id)
public function smsCost(Sms $sms)
{
$params = [
'to' => $sms->to,
'to' => $sms->to,
'text' => $sms->text,
];

$response = $this->request('sms/cost', $params);
$response = explode("\n", $response);

$smsCostResponse = new SmsCostResponse(array_shift($response));
$smsCostResponse->price = (float)$response[0];
$smsCostResponse->length = (int)$response[1];

return $smsCostResponse;
}

/**
* @return MyBalanceResponse
*/
public function myBalance()
{
$response = $this->request('my/balance');
$response = explode("\n", $response);

$myBalanceResponse = new MyBalanceResponse(array_shift($response));
$myBalanceResponse->balance = (float)$response[0];

return $myBalanceResponse;
}

/**
* @return MyLimitResponse
*/
public function myLimit()
{
$response = $this->request('my/limit');
$response = explode("\n", $response);

$myLimitResponse = new MyLimitResponse(array_shift($response));
$myLimitResponse->limit = (int)$response[0];
$myLimitResponse->current = (int)$response[1];

return $myLimitResponse;
}

/**
* @return MySendersResponse
*/
public function mySenders()
{
$response = $this->request('my/senders');
$response = explode("\n", $response);

$mySendersResponse = new MySendersResponse(array_shift($response));

foreach ($response as $phone) {
if ($phone) {
$mySendersResponse->phones[] = $phone;
}
}

return $mySendersResponse;
}

/**
* @return AuthCheckResponse
*/
public function authCheck()
{
$response = $this->request('auth/check');
$response = explode("\n", $response);

return new AuthCheckResponse(array_shift($response));
}

/**
* @param string $stoplistPhone
* @param string $stoplistText
Expand All @@ -209,61 +213,67 @@ public function authCheck()
*/
public function stoplistAdd($stoplistPhone, $stoplistText)
{
$response = $this->request('stoplist/add', [
'stoplist_phone' => $stoplistPhone,
'stoplist_text' => $stoplistText,
]);

$response = $this->request(
'stoplist/add',
[
'stoplist_phone' => $stoplistPhone,
'stoplist_text' => $stoplistText,
]
);

$response = explode("\n", $response);

return new StoplistAddResponse(array_shift($response));
}

/**
* @param string $stoplistPhone
*
* @return StoplistDelResponse
*/
public function stoplistDel($stoplistPhone)
{
$response = $this->request('stoplist/del', [
'stoplist_phone' => $stoplistPhone,
]);

$response = $this->request(
'stoplist/del',
[
'stoplist_phone' => $stoplistPhone,
]
);

$response = explode("\n", $response);

return new StoplistDelResponse(array_shift($response));
}

/**
* @return StoplistGetResponse
*/
public function stoplistGet()
{
$response = $this->request('stoplist/get');
$response = explode("\n", $response);

$stoplistGetResponse = new StoplistGetResponse(array_shift($response));

foreach ($response as $phone) {
$phone = explode(';', $phone);
$stoplistGetResponse->phones[] = new StoplistPhone($phone[0], $phone[1]);
}

return $stoplistGetResponse;
}

/**
* @param string $method
* @param array $params
* @param array $params
*
* @return string
*/
public function request($method, $params = [])
{
return $this->getClient()->request($method, array_merge($params, $this->getAuth()->getAuthParams()));
}

/**
* @return ClientInterface
*/
Expand All @@ -272,18 +282,18 @@ public function getClient()
if ($this->client === null) {
$this->client = new Client();
}

return $this->client;
}

/**
* @param ClientInterface $client
*/
public function setClient(ClientInterface $client)
{
$this->client = $client;
}

/**
* @return AuthInterface
*/
Expand Down
7 changes: 6 additions & 1 deletion Auth/AbstractAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ abstract class AbstractAuth implements AuthInterface
* @return array
*/
abstract public function getAuthParams();


/**
* @return null|string
*/
abstract public function getPartnerId();

/**
* @return Api
*/
Expand Down
25 changes: 20 additions & 5 deletions Auth/ApiIdAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@

class ApiIdAuth extends AbstractAuth
{

/**
* @var string
*/
private $apiId;


/**
* @var null|string
*/
private $partnerId;

/**
* @param string $apiId
* @param null|string $partnerId
*/
public function __construct($apiId)
public function __construct($apiId, $partnerId = null)
{
$this->apiId = $apiId;
$this->partnerId = $partnerId;
}

/**
* @return array
*/
Expand All @@ -27,12 +34,20 @@ public function getAuthParams()
'api_id' => $this->apiId,
];
}

/**
* @return string
*/
public function getApiId()
{
return $this->apiId;
}

/**
* @return null|string
*/
public function getPartnerId()
{
return $this->partnerId;
}
}
Loading

0 comments on commit 71f179d

Please sign in to comment.