Overview

Namespaces

  • DocPlanner
    • SDK
      • Base
        • ServiceDescription
      • Core
      • Model
        • Doctor
        • Geo
        • User
        • Visit
  • PHP

Classes

  • BaseSDK
  • CoreBase
  • Parameter
  • Result

Exceptions

  • DPException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Author: Ɓukasz Barulski
  4:  * Date: 22.04.13 15:13
  5:  */
  6: namespace DocPlanner\SDK\Base;
  7: 
  8: use DocPlanner\SDK\Base\ServiceDescription\DocPlanner;
  9: use DocPlanner\SDK\Base\Parameter;
 10: use Guzzle\Http\Exception\ClientErrorResponseException;
 11: use Guzzle\Http\Message\Response;
 12: use Guzzle\Service\Client;
 13: use Guzzle\Plugin\Oauth\OauthPlugin;
 14: use Guzzle\Service\Description\ServiceDescription;
 15: 
 16: /**
 17:  * Class BaseSDK
 18:  * @package DocPlanner\SDK\Base
 19:  */
 20: class BaseSDK
 21: {
 22:     const TOKEN = 'token';
 23:     const TOKEN_SECRET = 'token_secret';
 24:     const RESULT_TYPE_ARRAY = 'array';
 25:     const RESULT_TYPE_RESULT_OBJECT = 'result';
 26: 
 27:     /**
 28:      * @var string
 29:      */
 30:     protected $consumerKey;
 31: 
 32:     /**
 33:      * @var string
 34:      */
 35:     protected $consumerSecret;
 36: 
 37:     /**
 38:      * @var string|null
 39:      */
 40:     protected $token;
 41: 
 42:     /**
 43:      * @var string|null
 44:      */
 45:     protected $tokenSecret;
 46: 
 47:     /**
 48:      * @var string
 49:      */
 50:     protected $resultType;
 51: 
 52:     /**
 53:      * @var \Guzzle\Service\Client
 54:      */
 55:     protected $client;
 56: 
 57:     /**
 58:      * @param string $consumerKey
 59:      * @param string $consumerSecret
 60:      * @param string $resultType
 61:      */
 62:     public function __construct($consumerKey, $consumerSecret, $resultType = self::RESULT_TYPE_RESULT_OBJECT)
 63:     {
 64:         $this->client = new Client();
 65:         $this->consumerKey = $consumerKey;
 66:         $this->consumerSecret = $consumerSecret;
 67:         $this->client->addSubscriber($this->_getOauthPlugin());
 68:         $this->client->setDescription($this->_getServiceDescription());
 69:         $this->resultType = $resultType;
 70:     }
 71: 
 72:     /**
 73:      * @param string|null $token
 74:      * @param string|null $tokenSecret
 75:      *
 76:      * @return void
 77:      */
 78:     public function setToken($token, $tokenSecret)
 79:     {
 80:         $this->token = $token;
 81:         $this->tokenSecret = $tokenSecret;
 82:         $this->client->addSubscriber($this->_getOauthPlugin());
 83:     }
 84: 
 85:     /**
 86:      * @return array
 87:      */
 88:     public function getToken()
 89:     {
 90:         return [self::TOKEN => $this->token, self::TOKEN_SECRET => $this->tokenSecret];
 91:     }
 92: 
 93:     /**
 94:      * @return bool
 95:      */
 96:     public function tokenIsSet()
 97:     {
 98:         return ($this->token && $this->tokenSecret);
 99:     }
100: 
101:     /**
102:      * @param                 $method
103:      * @param Parameter|null  $parameters
104:      *
105:      * @throws \Exception|\Guzzle\Http\Exception\ClientErrorResponseException
106:      * @throws DPException
107:      * @return array|mixed
108:      */
109:     public function execute($method, Parameter $parameters = null)
110:     {
111:         $params = $parameters ? $parameters->all() : [];
112:         if ($parameters instanceof Parameter)
113:         {
114:             $parameters->clear();
115:         }
116:         $command = $this->client->getCommand($method, $params);
117:         $result = null;
118:         try
119:         {
120:             $result = $this->client->execute($command);
121:         }
122:         catch (ClientErrorResponseException $e)
123:         {
124:             /**
125:              * @var Response $response
126:              */
127:             $response = $e->getResponse();
128:             if (400 === $response->getStatusCode() && strstr($response->getMessage(), 'oauth_parameters_absent=oauth_token'))
129:             {
130:                 throw new DPException('You have to be logged in! Use "User::requestAccess" or "DocPlannerSDK::setToken" methods!');
131:             }
132:             throw $e;
133:         }
134: 
135:         if ($result['status'] != 'ok' && $result !== true)
136:         {
137:             throw new DPException('Unknown error! More info: ' . var_export($result, true));
138:         }
139: 
140:         $items = $result['items'];
141: 
142:         switch ($this->resultType)
143:         {
144:             case self::RESULT_TYPE_ARRAY:
145:                 return $items;
146: 
147:             case self::RESULT_TYPE_RESULT_OBJECT:
148:                 list($group, $class) = explode('.', $method);
149:                 $modelClass = '\DocPlanner\SDK\Model\\' . ucfirst($group) . '\\' . ucfirst($class);
150:                 if (array_keys($items) === range(0, count($items) - 1))
151:                 {
152:                     $result = [];
153:                     foreach ($items as $key => $value)
154:                     {
155:                         $result[$key] = new $modelClass($value);
156:                     }
157:                     return new Result($result);
158:                 }
159:                 return new $modelClass($items);
160: 
161:             default:
162:                 throw new DPException('Unknown result type!');
163:         }
164:     }
165: 
166:     /**
167:      * @return OauthPlugin
168:      */
169:     private function _getOauthPlugin()
170:     {
171:         return new OauthPlugin([
172:             'consumer_key'    => $this->consumerKey,
173:             'consumer_secret' => $this->consumerSecret,
174:             'token'           => $this->token,
175:             'token_secret'    => $this->tokenSecret,
176:         ]);
177:     }
178: 
179:     /**
180:      * @return ServiceDescription
181:      */
182:     private function _getServiceDescription()
183:     {
184:         return DocPlanner::factory();
185:     }
186: }
187: 
Docplanner API documentation generated by ApiGen 2.8.0