PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Service/LiveDocx.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 415 lines | 151 code | 33 blank | 231 comment | 10 complexity | cb74e80b638ab1226ebb5492845c57b6 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage LiveDocx
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: LiveDocx.php 25024 2012-07-30 15:08:15Z rob $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage LiveDocx
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. * @since LiveDocx 1.0
  29. */
  30. class Zend_Service_LiveDocx
  31. {
  32. /**
  33. * LiveDocx service version
  34. * @since LiveDocx 1.0
  35. */
  36. const VERSION = '2.0';
  37. /**
  38. * SOAP client used to connect to LiveDocx service
  39. * @var Zend_Soap_Client
  40. * @since LiveDocx 1.0
  41. */
  42. protected $_soapClient;
  43. /**
  44. * WSDL of LiveDocx web service
  45. * @var string
  46. * @since LiveDocx 1.0
  47. */
  48. protected $_wsdl;
  49. /**
  50. * Array of credentials (username and password) to log into backend server
  51. * @var array
  52. * @since LiveDocx 1.2
  53. */
  54. protected $_credentials;
  55. /**
  56. * Set to true, when session is logged into backend server
  57. * @var boolean
  58. * @since LiveDocx 1.2
  59. */
  60. protected $_loggedIn;
  61. /**
  62. * Constructor
  63. *
  64. * Optionally, pass an array of options (or Zend_Config object).
  65. *
  66. * If an option with the key 'soapClient' is provided, that value will be
  67. * used to set the internal SOAP client used to connect to the LiveDocx
  68. * service.
  69. *
  70. * Use 'soapClient' in the case that you have a dedicated or (locally
  71. * installed) licensed LiveDocx server. For example:
  72. *
  73. * {code}
  74. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  75. * array (
  76. * 'username' => 'myUsername',
  77. * 'password' => 'myPassword',
  78. * 'soapClient' => new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
  79. * )
  80. * );
  81. * {code}
  82. *
  83. * Replace the URI of the WSDL in the constructor of Zend_Soap_Client with
  84. * that of your dedicated or licensed LiveDocx server.
  85. *
  86. * If you are using the public LiveDocx server, simply pass 'username' and
  87. * 'password'. For example:
  88. *
  89. * {code}
  90. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  91. * array (
  92. * 'username' => 'myUsername',
  93. * 'password' => 'myPassword'
  94. * )
  95. * );
  96. * {code}
  97. *
  98. * If you prefer to not pass the username and password through the
  99. * constructor, you can also call the following methods:
  100. *
  101. * {code}
  102. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  103. *
  104. * $phpLiveDocx->setUsername('myUsername')
  105. * ->setPassword('myPassword');
  106. * {/code}
  107. *
  108. * Or, if you want to specify your own SoapClient:
  109. *
  110. * {code}
  111. * $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  112. *
  113. * $phpLiveDocx->setUsername('myUsername')
  114. * ->setPassword('myPassword');
  115. *
  116. * $phpLiveDocx->setSoapClient(
  117. * new Zend_Soap_Client('https://api.example.com/path/mailmerge.asmx?WSDL')
  118. * );
  119. * {/code}
  120. *
  121. * @param array|Zend_Config $options
  122. * @return void
  123. * @throws Zend_Service_LiveDocx_Exception
  124. * @since LiveDocx 1.0
  125. */
  126. public function __construct($options = null)
  127. {
  128. $this->_credentials = array();
  129. $this->_loggedIn = false;
  130. if ($options instanceof Zend_Config) {
  131. $options = $options->toArray();
  132. }
  133. if (is_array($options)) {
  134. $this->setOptions($options);
  135. }
  136. }
  137. /**
  138. * Set options
  139. * One or more of username, password, soapClient
  140. *
  141. * @param array $options
  142. * @return Zend_Service_LiveDocx
  143. * @since LiveDocx 1.2
  144. */
  145. public function setOptions(array $options)
  146. {
  147. foreach ($options as $key => $value) {
  148. $method = 'set' . $key;
  149. if (method_exists($this, $method)) {
  150. $this->$method($value);
  151. }
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Clean up and log out of LiveDocx service
  157. *
  158. * @return boolean
  159. * @since LiveDocx 1.0
  160. */
  161. public function __destruct()
  162. {
  163. return $this->logOut();
  164. }
  165. /**
  166. * Init Soap client - connect to SOAP service
  167. *
  168. * @param string $endpoint
  169. * @throws Zend_Service_LiveDocx_Exception
  170. * @return void
  171. * @since LiveDocx 1.2
  172. */
  173. protected function _initSoapClient($endpoint)
  174. {
  175. try {
  176. require_once 'Zend/Soap/Client.php';
  177. $this->_soapClient = new Zend_Soap_Client();
  178. $this->_soapClient->setWsdl($endpoint);
  179. } catch (Zend_Soap_Client_Exception $e) {
  180. require_once 'Zend/Service/LiveDocx/Exception.php';
  181. throw new Zend_Service_LiveDocx_Exception('Cannot connect to LiveDocx service at ' . $endpoint, 0, $e);
  182. }
  183. }
  184. /**
  185. * Get SOAP client
  186. *
  187. * @return Zend_Soap_Client
  188. * @since LiveDocx 1.2
  189. */
  190. public function getSoapClient()
  191. {
  192. return $this->_soapClient;
  193. }
  194. /**
  195. * Set SOAP client
  196. *
  197. * @param Zend_Soap_Client $soapClient
  198. * @return Zend_Service_LiveDocx
  199. * @since LiveDocx 1.2
  200. */
  201. public function setSoapClient(Zend_Soap_Client $soapClient)
  202. {
  203. $this->_soapClient = $soapClient;
  204. return $this;
  205. }
  206. /**
  207. * Log in to LiveDocx service
  208. *
  209. * @param string $username
  210. * @param string $password
  211. *
  212. * @throws Zend_Service_LiveDocx_Exception
  213. * @return boolean
  214. * @since LiveDocx 1.2
  215. */
  216. public function logIn()
  217. {
  218. if (!$this->isLoggedIn()) {
  219. if (null === $this->getUsername()) {
  220. require_once 'Zend/Service/LiveDocx/Exception.php';
  221. throw new Zend_Service_LiveDocx_Exception(
  222. 'Username has not been set. To set username specify the options array in the constructor or call setUsername($username) after instantiation'
  223. );
  224. }
  225. if (null === $this->getPassword()) {
  226. require_once 'Zend/Service/LiveDocx/Exception.php';
  227. throw new Zend_Service_LiveDocx_Exception(
  228. 'Password has not been set. To set password specify the options array in the constructor or call setPassword($password) after instantiation'
  229. );
  230. }
  231. if (null === $this->getSoapClient()) {
  232. $this->_initSoapClient($this->_wsdl);
  233. }
  234. try {
  235. $this->getSoapClient()->LogIn(array(
  236. 'username' => $this->getUsername(),
  237. 'password' => $this->getPassword(),
  238. ));
  239. $this->_loggedIn = true;
  240. } catch (Exception $e) {
  241. require_once 'Zend/Service/LiveDocx/Exception.php';
  242. throw new Zend_Service_LiveDocx_Exception(
  243. 'Cannot login into LiveDocx service - username and/or password are invalid', 0, $e
  244. );
  245. }
  246. }
  247. return $this->_loggedIn;
  248. }
  249. /**
  250. * Log out of the LiveDocx service
  251. *
  252. * @throws Zend_Service_LiveDocx_Exception
  253. * @return boolean
  254. * @since LiveDocx 1.2
  255. */
  256. public function logOut()
  257. {
  258. if ($this->isLoggedIn()) {
  259. try {
  260. $this->getSoapClient()->LogOut();
  261. $this->_loggedIn = false;
  262. } catch (Exception $e) {
  263. require_once 'Zend/Service/LiveDocx/Exception.php';
  264. throw new Zend_Service_LiveDocx_Exception(
  265. 'Cannot log out of LiveDocx service', 0, $e
  266. );
  267. }
  268. }
  269. return $this->_loggedIn;
  270. }
  271. /**
  272. * Return true, if session is currently logged into the backend server
  273. *
  274. * @return boolean
  275. * @since LiveDocx 1.2
  276. */
  277. public function isLoggedIn()
  278. {
  279. return $this->_loggedIn;
  280. }
  281. /**
  282. * Set username
  283. *
  284. * @return Zend_Service_LiveDocx
  285. * @since LiveDocx 1.0
  286. */
  287. public function setUsername($username)
  288. {
  289. $this->_credentials['username'] = $username;
  290. return $this;
  291. }
  292. /**
  293. * Set password
  294. *
  295. * @return Zend_Service_LiveDocx
  296. * @since LiveDocx 1.0
  297. */
  298. public function setPassword($password)
  299. {
  300. $this->_credentials['password'] = $password;
  301. return $this;
  302. }
  303. /**
  304. * Set WSDL of LiveDocx web service
  305. *
  306. * @return Zend_Service_LiveDocx
  307. * @since LiveDocx 1.0
  308. */
  309. public function setWsdl($wsdl)
  310. {
  311. $this->_wsdl = $wsdl;
  312. return $this;
  313. }
  314. /**
  315. * Return current username
  316. *
  317. * @return string|null
  318. * @since LiveDocx 1.0
  319. */
  320. public function getUsername()
  321. {
  322. if (isset($this->_credentials['username'])) {
  323. return $this->_credentials['username'];
  324. }
  325. return null;
  326. }
  327. /**
  328. * Return current password
  329. *
  330. * @return string|null
  331. * @since LiveDocx 1.0
  332. */
  333. public function getPassword()
  334. {
  335. if (isset($this->_credentials['password'])) {
  336. return $this->_credentials['password'];
  337. }
  338. return null;
  339. }
  340. /**
  341. * Return WSDL of LiveDocx web service
  342. *
  343. * @return Zend_Service_LiveDocx
  344. * @since LiveDocx 1.0
  345. */
  346. public function getWsdl()
  347. {
  348. return $this->_wsdl;
  349. }
  350. /**
  351. * Return the document format (extension) of a filename
  352. *
  353. * @param string $filename
  354. * @return string
  355. * @since LiveDocx 1.0
  356. */
  357. public function getFormat($filename)
  358. {
  359. return strtolower(substr(strrchr($filename, '.'), 1));
  360. }
  361. /**
  362. * Return the current API version
  363. *
  364. * @return string
  365. * @since LiveDocx 1.0
  366. */
  367. public function getVersion()
  368. {
  369. return self::VERSION;
  370. }
  371. /**
  372. * Compare the current API version with another version
  373. *
  374. * @param string $version (STRING NOT FLOAT)
  375. * @return int -1 (version is less than API version), 0 (versions are equal), or 1 (version is greater than API version)
  376. * @since LiveDocx 1.0
  377. */
  378. public function compareVersion($version)
  379. {
  380. return version_compare($version, $this->getVersion());
  381. }
  382. }