/Zend/Service/LiveDocx/LiveDocx.php

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