1: <?php
2: 3: 4: 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: 18: 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: 29:
30: protected $consumerKey;
31:
32: 33: 34:
35: protected $consumerSecret;
36:
37: 38: 39:
40: protected $token;
41:
42: 43: 44:
45: protected $tokenSecret;
46:
47: 48: 49:
50: protected $resultType;
51:
52: 53: 54:
55: protected $client;
56:
57: 58: 59: 60: 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: 74: 75: 76: 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: 87:
88: public function getToken()
89: {
90: return [self::TOKEN => $this->token, self::TOKEN_SECRET => $this->tokenSecret];
91: }
92:
93: 94: 95:
96: public function tokenIsSet()
97: {
98: return ($this->token && $this->tokenSecret);
99: }
100:
101: 102: 103: 104: 105: 106: 107: 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: 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: 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: 181:
182: private function _getServiceDescription()
183: {
184: return DocPlanner::factory();
185: }
186: }
187: