From 19dc5fa4345254447a1012812d8055d4711e1ca7 Mon Sep 17 00:00:00 2001 From: insolita Date: Sun, 4 Jun 2017 02:58:39 +0800 Subject: [PATCH] add partnerId in AuthInterface --- Api.php | 144 +++++++++++++++---------------- Auth/AbstractAuth.php | 7 +- Auth/ApiIdAuth.php | 25 ++++-- Auth/AuthInterface.php | 5 ++ Auth/LoginPasswordAuth.php | 25 ++++-- Auth/LoginPasswordSecureAuth.php | 17 +++- 6 files changed, 137 insertions(+), 86 deletions(-) diff --git a/Api.php b/Api.php index f72c42d..862b466 100644 --- a/Api.php +++ b/Api.php @@ -21,50 +21,37 @@ use Zelenin\SmsRu\Response\StoplistDelResponse; use Zelenin\SmsRu\Response\StoplistGetResponse; -/** - * Class Api - * - * @package Zelenin\SmsRu - */ class Api { /** * @var AuthInterface */ private $auth; - + /** * @var ClientInterface */ private $client; - /** - * @var string - */ - private $partner_id; - /** * @param AuthInterface $auth - * @param string|null $partner_id */ - public function __construct(AuthInterface $auth, $partner_id = null) + public function __construct(AuthInterface $auth) { $this->auth = $auth; $this->auth->setContext($this); - $this->partner_id = $partner_id; } - + /** * @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; @@ -75,34 +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->partner_id){ - $params['partner_id'] = $this->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)) { @@ -114,10 +101,10 @@ public function smsSend(AbstractSms $sms) // } } } - + return $smsResponse; } - + /** * @param string $id * @@ -125,15 +112,18 @@ public function smsSend(AbstractSms $sms) */ 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 * @@ -142,20 +132,20 @@ 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 */ @@ -163,13 +153,13 @@ 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 */ @@ -177,14 +167,14 @@ 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 */ @@ -192,18 +182,18 @@ 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 */ @@ -211,10 +201,10 @@ public function authCheck() { $response = $this->request('auth/check'); $response = explode("\n", $response); - + return new AuthCheckResponse(array_shift($response)); } - + /** * @param string $stoplistPhone * @param string $stoplistText @@ -223,16 +213,19 @@ 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 * @@ -240,15 +233,18 @@ public function stoplistAdd($stoplistPhone, $stoplistText) */ 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 */ @@ -256,20 +252,20 @@ 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 */ @@ -277,7 +273,7 @@ public function request($method, $params = []) { return $this->getClient()->request($method, array_merge($params, $this->getAuth()->getAuthParams())); } - + /** * @return ClientInterface */ @@ -286,10 +282,10 @@ public function getClient() if ($this->client === null) { $this->client = new Client(); } - + return $this->client; } - + /** * @param ClientInterface $client */ @@ -297,7 +293,7 @@ public function setClient(ClientInterface $client) { $this->client = $client; } - + /** * @return AuthInterface */ diff --git a/Auth/AbstractAuth.php b/Auth/AbstractAuth.php index ab07b20..c8b24c4 100644 --- a/Auth/AbstractAuth.php +++ b/Auth/AbstractAuth.php @@ -16,7 +16,12 @@ abstract class AbstractAuth implements AuthInterface * @return array */ abstract public function getAuthParams(); - + + /** + * @return null|string + */ + abstract public function getPartnerId(); + /** * @return Api */ diff --git a/Auth/ApiIdAuth.php b/Auth/ApiIdAuth.php index 65163db..0886895 100644 --- a/Auth/ApiIdAuth.php +++ b/Auth/ApiIdAuth.php @@ -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 */ @@ -27,7 +34,7 @@ public function getAuthParams() 'api_id' => $this->apiId, ]; } - + /** * @return string */ @@ -35,4 +42,12 @@ public function getApiId() { return $this->apiId; } + + /** + * @return null|string + */ + public function getPartnerId() + { + return $this->partnerId; + } } diff --git a/Auth/AuthInterface.php b/Auth/AuthInterface.php index d291a23..2a6454e 100644 --- a/Auth/AuthInterface.php +++ b/Auth/AuthInterface.php @@ -10,6 +10,11 @@ interface AuthInterface * @return array */ public function getAuthParams(); + + /** + * @return null|string + */ + public function getPartnerId(); /** * @return Api diff --git a/Auth/LoginPasswordAuth.php b/Auth/LoginPasswordAuth.php index 25c3f7a..fd50419 100644 --- a/Auth/LoginPasswordAuth.php +++ b/Auth/LoginPasswordAuth.php @@ -8,30 +8,45 @@ class LoginPasswordAuth extends AbstractAuth * @var string */ private $login; - + /** * @var string */ private $password; - + + /** + * @var null|string + */ + private $partnerId; + /** * @param string $login * @param string $password + * @param null|string $partnerId */ - public function __construct($login, $password) + public function __construct($login, $password, $partnerId = null) { $this->login = $login; $this->password = $password; + $this->partnerId = $partnerId; } - + /** * @return array */ public function getAuthParams() { return [ - 'login' => $this->login, + 'login' => $this->login, 'password' => $this->password, ]; } + + /** + * @return null|string + */ + public function getPartnerId() + { + return $this->partnerId; + } } diff --git a/Auth/LoginPasswordSecureAuth.php b/Auth/LoginPasswordSecureAuth.php index d10b0f4..05c2b91 100644 --- a/Auth/LoginPasswordSecureAuth.php +++ b/Auth/LoginPasswordSecureAuth.php @@ -22,6 +22,11 @@ class LoginPasswordSecureAuth extends AbstractAuth * @var null|string */ private $apiId; + + /** + * @var null|string + */ + private $partnerId; /** * @var CacheInterface @@ -38,13 +43,15 @@ class LoginPasswordSecureAuth extends AbstractAuth * @param string $password * @param null|string $apiId * @param CacheInterface|null $cache + * @param null|string $partnerId */ - public function __construct($login, $password, $apiId = null, CacheInterface $cache = null) + public function __construct($login, $password, $apiId = null, CacheInterface $cache = null, $partnerId=null) { $this->login = $login; $this->password = $password; $this->apiId = $apiId; $this->cache = $cache === null ? new DummyCache() : $cache; + $this->partnerId = $partnerId; } /** @@ -70,6 +77,14 @@ public function getApiId() { return $this->apiId; } + + /** + * @return null|string + */ + public function getPartnerId() + { + return $this->partnerId; + } /** * @return string