/library/Zend/Service/LiveDocx/AbstractLiveDocx.php

https://github.com/bruisedlee/zf2 · PHP · 398 lines · 161 code · 48 blank · 189 comment · 13 complexity · 17093ba6404ba56af0a3bae3eb93b4d4 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Service\LiveDocx;
  25. use Zend\Config\Config,
  26. Zend\Soap\Client as SoapClient;
  27. /**
  28. * @category Zend
  29. * @package Zend_Service
  30. * @subpackage LiveDocx
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class AbstractLiveDocx
  35. {
  36. /**
  37. * LiveDocx service version.
  38. * @since LiveDocx 1.0
  39. */
  40. const VERSION = '2.0';
  41. /**
  42. * SOAP client used to connect to LiveDocx service.
  43. * @var \Zend\Soap\Client
  44. * @since LiveDocx 1.0
  45. */
  46. protected $soapClient = null;
  47. /**
  48. * WSDL of LiveDocx service.
  49. * @var string
  50. * @since LiveDocx 1.0
  51. */
  52. protected $wsdl = null;
  53. /**
  54. * Array of credentials (username and password) to log into LiveDocx service.
  55. * @var array
  56. * @since LiveDocx 1.2
  57. */
  58. protected $credentials = array();
  59. /**
  60. * Status of connection to LiveDocx service.
  61. * When set to true, session is logged into LiveDocx service.
  62. * When set to false, session is not logged into LiveDocx service.
  63. * @var boolean
  64. * @since LiveDocx 1.2
  65. */
  66. protected $isLoggedIn = null;
  67. /**
  68. * Constructor.
  69. *
  70. * Optionally, pass an array of options (or \Zend\Config\Config object).
  71. *
  72. * @param array|Config $options
  73. * @return void
  74. * @since LiveDocx 1.0
  75. */
  76. public function __construct($options = null)
  77. {
  78. $this->setIsLoggedIn(false);
  79. if ($options instanceof Config) {
  80. $options = $options->toArray();
  81. }
  82. if (is_array($options)) {
  83. $this->setOptions($options);
  84. }
  85. }
  86. /**
  87. * Clean up and log out of LiveDocx service.
  88. *
  89. * @return boolean
  90. * @since LiveDocx 1.0
  91. */
  92. public function __destruct()
  93. {
  94. return $this->logOut();
  95. }
  96. /**
  97. * Set options. Valid options are username, password and soapClient.
  98. *
  99. * @param $options
  100. * @throws Exception\InvalidArgumentException
  101. * @return AbstractLiveDocx
  102. * @since LiveDocx 1.2
  103. */
  104. public function setOptions(array $options)
  105. {
  106. foreach ($options as $key => $value) {
  107. $method = 'set' . $key;
  108. if (!method_exists($this, $method)) {
  109. throw new Exception\InvalidArgumentException(sprintf(
  110. 'Invalid option specified - "%s"', $key
  111. ));
  112. }
  113. $this->$method($value);
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Set SOAP client.
  119. *
  120. * @param \Zend\Soap\Client $soapClient
  121. * @return AbstractLiveDocx
  122. * @since LiveDocx 1.2
  123. */
  124. public function setSoapClient($soapClient)
  125. {
  126. $this->soapClient = $soapClient;
  127. return $this;
  128. }
  129. /**
  130. * Get SOAP client.
  131. *
  132. * @return \Zend\Soap\Client
  133. * @since LiveDocx 1.2
  134. */
  135. public function getSoapClient()
  136. {
  137. return $this->soapClient;
  138. }
  139. /**
  140. * Instantiate SOAP client.
  141. *
  142. * @param string $endpoint
  143. * @return void
  144. * @since LiveDocx 1.2
  145. */
  146. protected function initSoapClient($endpoint)
  147. {
  148. $this->soapClient = new SoapClient();
  149. $this->soapClient->setWsdl($endpoint);
  150. }
  151. /**
  152. * Set username.
  153. *
  154. * @return AbstractLiveDocx
  155. * @since LiveDocx 1.0
  156. */
  157. public function setUsername($username)
  158. {
  159. $this->credentials['username'] = $username;
  160. return $this;
  161. }
  162. /**
  163. * Return username.
  164. *
  165. * @return string|null
  166. * @since LiveDocx 1.0
  167. */
  168. public function getUsername()
  169. {
  170. if (isset($this->credentials['username'])) {
  171. return $this->credentials['username'];
  172. }
  173. return null;
  174. }
  175. /**
  176. * Set password.
  177. *
  178. * @return AbstractLiveDocx
  179. * @since LiveDocx 1.0
  180. */
  181. public function setPassword($password)
  182. {
  183. $this->credentials['password'] = $password;
  184. return $this;
  185. }
  186. /**
  187. * Return password.
  188. *
  189. * @return string|null
  190. * @since LiveDocx 1.0
  191. */
  192. public function getPassword()
  193. {
  194. if (isset($this->credentials['password'])) {
  195. return $this->credentials['password'];
  196. }
  197. return null;
  198. }
  199. /**
  200. * Set WSDL of LiveDocx service.
  201. *
  202. * @return AbstractLiveDocx
  203. * @since LiveDocx 1.0
  204. */
  205. public function setWsdl($wsdl)
  206. {
  207. $this->wsdl = $wsdl;
  208. return $this;
  209. }
  210. /**
  211. * Return WSDL of LiveDocx service.
  212. *
  213. * @return AbstractLiveDocx
  214. * @since LiveDocx 1.0
  215. */
  216. public function getWsdl()
  217. {
  218. if (null !== $this->getSoapClient()) {
  219. return $this->getSoapClient()->getWsdl();
  220. } else {
  221. return $this->wsdl;
  222. }
  223. }
  224. /**
  225. * Return the document format (extension) of a filename.
  226. *
  227. * @param string $filename
  228. * @return string
  229. * @since LiveDocx 1.0
  230. */
  231. public function getFormat($filename)
  232. {
  233. return strtolower(substr(strrchr($filename, '.'), 1));
  234. }
  235. /**
  236. * Return the current API version.
  237. *
  238. * @return string
  239. * @since LiveDocx 1.0
  240. */
  241. public function getVersion()
  242. {
  243. return self::VERSION;
  244. }
  245. /**
  246. * Compare the current API version with another version.
  247. *
  248. * @param string $version (STRING NOT FLOAT).
  249. * @return int -1 (version is less than API version), 0 (versions are equal), or 1 (version is greater than API version).
  250. * @since LiveDocx 1.0
  251. */
  252. public function compareVersion($version)
  253. {
  254. return version_compare($version, $this->getVersion());
  255. }
  256. // -------------------------------------------------------------------------
  257. /**
  258. * Return logged into LiveDocx service status.
  259. * (true = logged in, false = not logged in).
  260. *
  261. * @return boolean
  262. * @since LiveDocx 1.2
  263. */
  264. protected function getIsLoggedIn()
  265. {
  266. return $this->isLoggedIn;
  267. }
  268. /**
  269. * Set logged into LiveDocx service status.
  270. * (true = logged in, false = not logged in).
  271. *
  272. * @throws Exception\InvalidArgumentException
  273. * @return boolean
  274. * @since LiveDocx 1.2
  275. */
  276. protected function setIsLoggedIn($state)
  277. {
  278. if (!is_bool($state)) {
  279. throw new Exception\InvalidArgumentException(
  280. 'Logged in status must be boolean.'
  281. );
  282. }
  283. $this->isLoggedIn = $state;
  284. }
  285. // -------------------------------------------------------------------------
  286. /**
  287. * Log in to LiveDocx service.
  288. *
  289. * @param string $username
  290. * @param string $password
  291. * @throws Exception\InvalidArgumentException
  292. * @throws Exception\RuntimeException
  293. * @return boolean
  294. * @since LiveDocx 1.2
  295. */
  296. protected function logIn()
  297. {
  298. if (false === $this->getIsLoggedIn()) {
  299. if (null === $this->getUsername()) {
  300. throw new Exception\InvalidArgumentException(
  301. 'Username has not been set. To set username specify the options array '
  302. . 'in the constructor or call setUsername($username) after instantiation.'
  303. );
  304. }
  305. if (null === $this->getPassword()) {
  306. throw new Exception\InvalidArgumentException(
  307. 'Password has not been set. To set password specify the options array '
  308. . 'in the constructor or call setPassword($password) after instantiation.'
  309. );
  310. }
  311. if (null === $this->getSoapClient()) {
  312. $this->initSoapClient($this->getWsdl());
  313. }
  314. try {
  315. @$this->getSoapClient()->LogIn(array(
  316. 'username' => $this->getUsername(),
  317. 'password' => $this->getPassword(),
  318. ));
  319. $this->setIsLoggedIn(true);
  320. } catch (\Exception $e) {
  321. throw new Exception\RuntimeException(
  322. $e->getMessage()
  323. );
  324. }
  325. }
  326. return $this->isLoggedIn;
  327. }
  328. /**
  329. * Log out of the LiveDocx service.
  330. *
  331. * @throws Exception\RuntimeException
  332. * @return boolean
  333. * @since LiveDocx 1.2
  334. */
  335. protected function logOut()
  336. {
  337. if ($this->getIsLoggedIn()) {
  338. try {
  339. $this->getSoapClient()->LogOut();
  340. $this->setIsLoggedIn(false);
  341. } catch (\Exception $e) {
  342. throw new Exception\RuntimeException(
  343. $e->getMessage()
  344. );
  345. }
  346. }
  347. return $this->isLoggedIn;
  348. }
  349. // -------------------------------------------------------------------------
  350. }