PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Services/Capsule/Common.php

http://github.com/davidcoallier/Services_Capsule
PHP | 296 lines | 110 code | 31 blank | 155 comment | 13 complexity | 4bbd020d31e08ab9a1ecd37ce91380c0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * +-----------------------------------------------------------------------+
  4. * | Copyright (c) 2010, David Coallier & echolibre ltd |
  5. * | All rights reserved. |
  6. * | |
  7. * | Redistribution and use in source and binary forms, with or without |
  8. * | modification, are permitted provided that the following conditions |
  9. * | are met: |
  10. * | |
  11. * | o Redistributions of source code must retain the above copyright |
  12. * | notice, this list of conditions and the following disclaimer. |
  13. * | o Redistributions in binary form must reproduce the above copyright |
  14. * | notice, this list of conditions and the following disclaimer in the |
  15. * | documentation and/or other materials provided with the distribution.|
  16. * | o The names of the authors may not be used to endorse or promote |
  17. * | products derived from this software without specific prior written |
  18. * | permission. |
  19. * | |
  20. * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  21. * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  22. * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  23. * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
  24. * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  25. * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
  26. * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  27. * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  28. * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
  29. * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  30. * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
  31. * | |
  32. * +-----------------------------------------------------------------------+
  33. * | Author: David Coallier <david@echolibre.com> |
  34. * +-----------------------------------------------------------------------+
  35. *
  36. * PHP version 5
  37. *
  38. * @category Services
  39. * @package Services_Capsule
  40. * @author David Coallier <david@echolibre.com>
  41. * @copyright echolibre ltd. 2009-2010
  42. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  43. * @link http://github.com/davidcoallier/Services_Capsule
  44. * @version GIT: $Id$
  45. */
  46. /**
  47. * Services_Capsule
  48. *
  49. * @category Services
  50. * @package Services_Capsule
  51. * @author David Coallier <david@echolibre.com>
  52. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  53. * @link http://github.com/davidcoallier/Services_Capsule
  54. * @version Release: @package_version@
  55. */
  56. abstract class Services_Capsule_Common
  57. {
  58. /**
  59. * Sub section. History of an Opportunity for example
  60. * is Services_Capsule_Opportunity_History
  61. *
  62. * @var string The sub section mame
  63. */
  64. protected $subSections = array();
  65. /**
  66. * The name of the module to call
  67. *
  68. * @var string The module name to call
  69. */
  70. protected $moduleName;
  71. /**
  72. * The identification token
  73. *
  74. * @link https://sample.capsulecrm.com/user/api
  75. * @var string The web service identification token
  76. */
  77. protected $token;
  78. /**
  79. * The application name
  80. *
  81. * @var string The actual app name of your company
  82. */
  83. protected $appName;
  84. /**
  85. * An object of the HTTP_Request2 Client
  86. *
  87. * @var HTTP_Request2 An HTTP_Request2 instance.
  88. */
  89. protected $client;
  90. /**
  91. * The capsule webservice endpoint in a sprintf configurable url.
  92. *
  93. * @see $this->appName
  94. * @var string The web service endpoint.
  95. */
  96. protected $endpoint = 'https://%s.capsulecrm.com/api/%s';
  97. /**
  98. * Magical Getter
  99. *
  100. * @throws Services_Capsule_RuntimeException
  101. *
  102. * @param string $sub Items, Meetings, Notes, Projects or User.
  103. *
  104. * @return mixed Services_Capsule_*
  105. */
  106. public function __get($section)
  107. {
  108. $section = ucwords(strtolower($section));
  109. switch ($section) {
  110. case 'History':
  111. case 'Tag':
  112. case 'Party':
  113. case 'Opportunity':
  114. case 'Customfield':
  115. case 'Milestone':
  116. case 'Cases':
  117. case 'Task':
  118. $currentModule = ucfirst(strtolower($this->moduleName));
  119. if (!isset($this->subSections[$section])) {
  120. $classname = 'Services_Capsule_'. $currentModule . '_' .$section;
  121. if (!class_exists($classname)) {
  122. $filename = str_replace('_', '/', $classname) . '.php';
  123. if (!(include $filename)) {
  124. throw new Services_Capsule_RuntimeException(
  125. 'File ' . $filename . ' does not exist.'
  126. );
  127. }
  128. }
  129. $this->subSections[$section] = new $classname;
  130. }
  131. $this->subSections[$section]
  132. ->setToken($this->token)
  133. ->setAppName($this->appName)
  134. ->setModuleName(strtolower($currentModule));
  135. return $this->subSections[$section];
  136. break;
  137. default:
  138. throw new Services_Capsule_RuntimeException(
  139. 'Section '. $section .' is not a valid API call. If you believe this ' .
  140. 'is wrong please report a bug on http://pear.php.net/Services_Capsule'
  141. );
  142. }
  143. }
  144. /**
  145. * Set the identification token
  146. *
  147. * This method is used to set the identification token
  148. * that you can gather from the capsule website.
  149. *
  150. * @link https://sampl.capsulecrm.com/user/api
  151. *
  152. * @param string $token The web service token.
  153. * @return object $this
  154. */
  155. public function setToken($token)
  156. {
  157. $this->token = $token;
  158. return $this;
  159. }
  160. /**
  161. * Set the application name
  162. *
  163. * This method is used to set the first part of the
  164. * $this->endpoint variable (%s).
  165. *
  166. * @param string $appName The name of your application (company name).
  167. * @return object $this
  168. */
  169. public function setAppName($appName)
  170. {
  171. $this->appName = $appName;
  172. return $this;
  173. }
  174. /**
  175. * Set the module naem
  176. *
  177. * This method is used to set the module name that the
  178. * child will be invoking.
  179. *
  180. * @param string $moduleName The module name to use.
  181. * @return object $this
  182. */
  183. public function setModuleName($moduleName)
  184. {
  185. $this->moduleName = $moduleName;
  186. return $this;
  187. }
  188. /**
  189. * Send the request to the capsule web service
  190. *
  191. * This method is used to send all the requests to the capsule web service.
  192. *
  193. * @throws Services_Capsule_UnexpectedValueException
  194. * @throws Services_Capsule_RuntimeException
  195. *
  196. * @uses HTTP_Request2
  197. *
  198. * @param string $url The URL to request.
  199. * @param string $method The HTTP Method (Use HTTP_Request2::METHOD_*)
  200. * Default is HTTP_Request2::METHOD_GET (GET).
  201. * @param string $data The data to pass to the body. Null by default.
  202. */
  203. protected function sendRequest($url, $method = HTTP_Request2::METHOD_GET, $data = null)
  204. {
  205. if (!isset($this->appName)) {
  206. throw new Services_Capsule_UnexpectedValueException(
  207. 'Please set an app name.'
  208. );
  209. }
  210. if (!isset($this->client) || !($this->client instanceof HTTP_Request2)) {
  211. $this->client = new HTTP_Request2();
  212. $this->client->setAdapter('curl');
  213. // Modify the security parameters for this connection because for some odd reason
  214. // it did not function in the previous case.
  215. $z = $this->client->getConfig();
  216. $z['ssl_verify_peer'] = false;
  217. $z['ssl_verify_host'] = false;
  218. $this->client->setConfig($z);
  219. }
  220. $finalUrl = sprintf($this->endpoint, $this->appName, $this->moduleName);
  221. $finalUrl = $finalUrl . $url;
  222. $this->client
  223. ->setHeader('Content-Type: application/json')
  224. ->setHeader('Accept: application/json')
  225. ->setAuth($this->token, 'x')
  226. ->setMethod($method)
  227. ->setUrl($finalUrl);
  228. if (!is_null($data)) {
  229. $this->client->setBody($data);
  230. }
  231. try {
  232. $response = $this->client->send();
  233. } catch (HTTP_Request2_Exception $e) {
  234. throw new Services_Capsule_RuntimeException($e);
  235. }
  236. return $response;
  237. }
  238. /**
  239. * Parse the response
  240. *
  241. * This method is used to parse the response that is returned from
  242. * the request that was made in $this->sendRequest().
  243. *
  244. * @throws Services_Capsule_RuntimeException
  245. *
  246. * @param HTTP_Request2_Response $response The response from the webservice.
  247. * @return mixed stdClass|bool A stdClass object of the
  248. * json-decode'ed body or true if
  249. * the code is 201 (created)
  250. */
  251. protected function parseResponse(HTTP_Request2_Response $response)
  252. {
  253. $body = $response->getBody();
  254. $return = json_decode($body);
  255. if (!($return instanceof stdClass)) {
  256. if ($response->getStatus() == 201 || $response->getStatus() == 200) {
  257. return true;
  258. }
  259. throw new Services_Capsule_RuntimeException(
  260. 'Invalid response with no valid json body'
  261. );
  262. }
  263. return $return;
  264. }
  265. }