AbstractRESTService.php 7.72 KB
Newer Older
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
20 21 22
 */

abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebService_SIGB_AbstractService {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  protected $_server_root;
  protected $_web_client;

  /**
   * @param string $server_root
   * @return Class_WebService_SIGB_AbstractService
   */
  public static function getService($server_root) {
    return static::newInstance()->setServerRoot($server_root);
  }


  /**
   * @param string $server_root
   * @return Class_WebService_SIGB_AbstractRESTService
   */
  public function setServerRoot($server_root) {
40
    $this->_server_root = $this->_withProtocol($server_root);
41 42 43
    return $this;
  }

44 45 46 47 48 49 50 51

  protected function _withProtocol($url) {
    return ('http://' == substr($url, 0, 7) || 'https://' == substr($url, 0, 8)
            ? '' : 'http://')
      . $url;
  }


52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  /**
   * @return string
   */
  public function getServerRoot() {
    return $this->_server_root;

  }

  /**
   * @return Class_WebService_SimpleWebClient
   */
  public function getWebClient() {
    if (!isset($this->_web_client))
      $this->_web_client = new Class_WebService_SimpleWebClient();
    return $this->_web_client;
  }

  /**
   * @param Class_WebService_SimpleWebClient $web_client
   * @return Class_WebService_SIGB_AbstractRESTService
   */
  public function setWebClient($web_client) {
    $this->_web_client = $web_client;
    return $this;
  }

  /**
   * @param array $options
   * @return string
   */
  public function buildQueryURL($options) {
    return sprintf('%s?%s', $this->getServerRoot(), http_build_query($options));
  }

  /**
   * @param array $options
   * @return string
   */
  public function httpGet($options) {
    $closure = function($client, $url) {
      return $client->open_url($url);
    };

    return $this->_withClientDo($options, $closure);
  }


  /**
   * @param $options array url query part
   * @param $datas array posted datas
   * @return string response body
   */
  public function httpPost($options, $datas) {
    $closure = function($client, $url) use ($datas) {
      return $client->postData($url, $datas);
    };

    return $this->_withClientDo($options, $closure);
  }


Ghislain Loas's avatar
Ghislain Loas committed
113 114 115 116 117 118 119 120 121
  public function httpPut($options, $datas) {
    $closure = function($client, $url) use ($datas) {
      return $client->putData($url, $datas);
    };

    return $this->_withClientDo($options, $closure);
  }


122 123 124 125 126 127 128 129 130
  /**
   * @param $options array url query part
   * @param $closure
   * @return response body
   */
  protected function _withClientDo($options, $closure) {
    $url = $this->buildQueryURL($options);
    try {
      $response = $closure($this->getWebClient(), $url);
131
      $this->log($this);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      return $response;
    } catch (Exception $e) {
      $this->logError($url, $e->getMessage());
      return '';
    }
  }


  /**
   * @param string $xml
   * @param string $tag
   * @return string
   */
  protected function _getTagData($xml, $tag) {
    $matches = array();
147 148
    $string_to_search = sprintf('/%s>([^<]*)<\/%s/', $tag, $tag);
    if (preg_match($string_to_search,
149
                   $xml,
150
                   $matches)) {
151
      return $matches[1];
152
    }
153 154 155 156
    return '';
  }


157 158 159 160 161
  protected function _findErrorTagInXml($xml, $tag) {
    return $this->_getTagData($xml,$tag);
  }


162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  public function httpGetNotice($params, $reader) {
    $xml = $this->httpGet($params);

    if ($notice = $reader->getNoticeFromXML($xml))
      $this->cacheNotice($notice);

    return $notice;
  }


  /**
   * @param int $id
   * @param Class_WeClass_WebService_SIGB*Reader $reader
   * @return string Class_WebService_SIGB_Notice
   */
  public function ilsdiGetRecords($id, $reader) {
    return $this->httpGetNotice(['service' => 'GetRecords', 'id' => $id],
                                $reader);
  }


  /**
   * @param array $params
   * @param Class_WebService_SIGB_AbstractILSDIPatronInfoReader $reader
   * @return Class_WebService_SIGB_Emprunteur
   */
  public function ilsdiGetPatronInfo($params, $reader, $error_tag='error') {
    $emprunteur = Class_WebService_SIGB_Emprunteur::newInstance()->setService($this);
    $params = array_merge(array('service' => 'GetPatronInfo'), $params);
    $xml = $this->httpGet($params);

    if (0 === strpos($xml, '<html>'))
      return $emprunteur;

    if ($this->_getTagData($xml, $error_tag))
      return $emprunteur;

    return $reader
      ->setEmprunteur($emprunteur)
      ->parseXML($xml)
      ->getEmprunteur();
  }


  /**
   * @param array $params
   * @return array
   */
  public function ilsdiHoldTitle($params, $error_tag = 'error') {
    return $this->ilsdiAction('HoldTitle', $params, $error_tag,
                              $this->_('Réservation impossible'));
  }


  /**
   * @param array $params
   * @return array
   */
  public function ilsdiHoldItem($params, $error_tag = 'error') {
    return $this->ilsdiAction('HoldItem', $params, $error_tag,
                              $this->_('Réservation impossible'));
  }


  /**
   * @param array $params
   */
  public function ilsdiCancelHold($params, $error_tag='error') {
    return $this->ilsdiAction('CancelHold', $params, $error_tag,
                              $this->_('Annulation impossible'));
  }
233 234


235 236 237 238 239 240 241
  /**
   * @param array $params
   */
  public function ilsdiRenewLoan($params, $error_tag='error') {
    return $this->ilsdiAction('RenewLoan', $params, $error_tag,
                              $this->_('Prolongation impossible'));
  }
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256

  /**
   * @param string $xml
   */
  public function ilsdiUpdatePatronInfo($xml, $error_tag='error'){
    return $this->ilsdiCheckXml($xml, $error_tag,
                                $this->_('Mise à jour impossible'));
  }


  public function ilsdiAction($name, $params, $error_tag, $error_message) {
    $params = array_merge(['service' => $name], $params);
    return $this->ilsdiCheckXml($this->httpGet($params), $error_tag, $error_message);
  }
257

258

259
  public function ilsdiCheckXml($xml, $error_tag, $error_message) {
260 261 262 263 264 265 266
    return ($error = $this->_getErrorMessage($xml, $error_tag, $error_message))
      ? $this->_error($error)
      : $this->_success();
  }


  protected function _getErrorMessage($xml, $tag, $message) {
267
    if (!$xml)
268
      return $this->_getNetworkErrorLabel();
269

270 271
    if (0 === strpos($xml, '<html>'))
      return $this->_getNetworkErrorLabel();
272

273 274 275
    if ($error = $this->_xmlError($xml))
      return $error;

276 277 278
    return ($error = $this->_findErrorTagInXml($xml, $tag))
      ? $this->_getErrorFromCode($error, $message)
      : null;
279
  }
280

281

282 283 284 285 286
  protected function _xmlError($xml) {
    return '';
  }


287 288 289 290
  /**
   * @return array
   */
  protected function _getNetworkError() {
291 292 293 294 295 296 297 298 299 300 301 302
    return $this->_error($this->_getNetworkErrorLabel());
  }


  protected function _getNetworkErrorLabel() {
    return $this->_('Service indisponible');
  }


  protected function _isNetworkError($result) {
    return (false === $result['statut'])
      && ($this->_getNetworkErrorLabel() == $result['erreur']);
303
  }
304 305 306


  protected function _getErrorFromCode($error_code, $default_message) {
307
    return $default_message;
308
  }
309
}