From 97a063219a469910824c055cbffbef69a2127ede Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Tue, 21 Aug 2012 09:55:34 +0000
Subject: [PATCH] CommSigb: refacto avec Users::getSigbComm + closures

---
 library/Class/CommSigb.php                    | 87 +++++++------------
 library/Class/IntBib.php                      | 32 +++++++
 library/Class/Users.php                       | 13 ++-
 .../Class/WebService/SIGB/AbstractService.php |  4 +
 .../Class/WebService/SIGB/Pergame/Service.php |  4 +
 5 files changed, 81 insertions(+), 59 deletions(-)

diff --git a/library/Class/CommSigb.php b/library/Class/CommSigb.php
index 01bf7adcfb1..45e4e2f6aaa 100644
--- a/library/Class/CommSigb.php
+++ b/library/Class/CommSigb.php
@@ -29,7 +29,7 @@ class Class_CommSigb {
 	const COM_MICROBIB = 9;
 	const COM_BIBLIXNET = 10;
 
-	protected static $_instance;
+
 
 	private $COM_CLASSES = array(self::COM_PERGAME => 'Class_WebService_SIGB_Pergame',
 															 self::COM_OPSYS => 'Class_WebService_SIGB_Opsys',
@@ -41,6 +41,8 @@ class Class_CommSigb {
 															 self::COM_MICROBIB => 'Class_WebService_SIGB_Microbib',
 															 self::COM_BIBLIXNET => 'Class_WebService_SIGB_BiblixNet');
 
+	protected static $_instance;
+
 	private $mode_comm;								// memo de modes de comm pour les bibs
 	private $msg_erreur_comm;					// Message d'erreur pour la connexion au service de communication
 	private $_translate;
@@ -197,37 +199,14 @@ class Class_CommSigb {
 	 * @return array
 	 */
 	public function supprimerReservation($std_user, $id_reservation) {
-		$user = Class_Users::getLoader()->find($std_user->ID_USER);
-		Class_WebService_SIGB_EmprunteurCache::newInstance()->remove($user);
+		$supprimer = function ($user, $sigb) use ($id_reservation) {
+			              if ($sigb->isPergame())
+											return (new Class_Systeme_PergameService($user))->supprimerReservation($id_reservation);
 
-		$mode_comm = $this->getModeComm($user->getIdSite());
-		$ret = array();
-		switch ($mode_comm["type"]) {
-			// Pergame
-			case self::COM_PERGAME:
-				$pergame = new Class_Systeme_PergameService($user);
-				$ret = $pergame->supprimerReservation($id_reservation);
-				break;
+										return $sigb->supprimerReservation($user, $id_reservation);			
+		};
 
-			// Autres
-		  case self::COM_OPSYS:
-		  case self::COM_VSMART:
-  		case self::COM_KOHA:
-	    case self::COM_CARTHAME:
-			case self::COM_NANOOK:
-			case self::COM_ORPHEE:
-			case self::COM_MICROBIB:
-		  case self::COM_BIBLIXNET:
-				$user = Class_Users::getLoader()->find($user->ID_USER);
-				$sigb = $this->getSIGBComm($mode_comm);
-				if ($sigb == false) {
-					$ret['erreur'] = $this->msg_erreur_comm;
-				} else {
-					$ret = $sigb->supprimerReservation($user, $id_reservation);
-				}
-				break;
-		}
-		return $ret;
+		return $this->withUserAndSIGBDo($std_user, $supprimer);
 	}
 
 
@@ -237,26 +216,28 @@ class Class_CommSigb {
 	 * @return array
 	 */
 	public function prolongerPret($std_user, $id_pret) {
+		$prolonger = function($user, $sigb) use ($std_user, $id_pret) {
+			              if ($sigb->isPergame())
+											return (new Class_Systeme_PergameService($std_user))->prolongerPret($id_pret);
+	
+										return $sigb->prolongerPret($user, $id_pret);
+		};
+
+		return $this->withUserAndSIGBDo($std_user, $prolonger);
+	}
+
+
+	public function withUserAndSIGBDo($std_user, $closure) {
 		$user = Class_Users::getLoader()->find($std_user->ID_USER);
 		Class_WebService_SIGB_EmprunteurCache::newInstance()->remove($user);
 
-		$mode_comm = $this->getModeComm($user->getIdSite());
-		if (!$mode_comm['type'])
-			return array();
-
-		if (false == $sigb = $this->getSIGBComm($mode_comm))
-			return array('erreur' => $this->msg_erreur_comm);
+		if (null == $sigb = $user->getSIGBComm())
+			return [];
 		
-		switch ($mode_comm["type"])
-			{
-				// Pergame
-				case self::COM_PERGAME:
-					$pergame = new Class_Systeme_PergameService($std_user);
-					$ret = $pergame->prolongerPret($id_pret);
-					return $ret;
-				default:
-					return $sigb->prolongerPret($user, $id_pret);
-			}
+		if (!$sigb->isConnected())
+			return array('erreur' => $this->msg_erreur_comm);
+
+		return $closure($user, $sigb);
 	}
 
 
@@ -279,16 +260,8 @@ class Class_CommSigb {
 	 * @return Class_WebService_SIGB_AbstractService
 	 */
 	private function getSIGBComm($mode_comm) {
-		if (!array_key_exists($mode_comm['type'], $this->COM_CLASSES))
-			return null;
-
-		$service_class = $this->COM_CLASSES[$mode_comm['type']];
-		$sigb_com = call_user_func(array($service_class, 'getService'),
-															 $mode_comm);
-
-		if(!$sigb_com->isConnected())
-			return false;
-
-		return $sigb_com;
+		if ($bib = Class_IntBib::find($mode_comm['id_bib']))
+			return $bib->getSIGBComm();
+		return false;
 	}
 }
\ No newline at end of file
diff --git a/library/Class/IntBib.php b/library/Class/IntBib.php
index 5f1da873e3c..8b6df0ef7af 100644
--- a/library/Class/IntBib.php
+++ b/library/Class/IntBib.php
@@ -24,6 +24,28 @@
  */
 
 class Class_IntBib extends Storm_Model_Abstract {
+	const COM_PERGAME = 1;
+	const COM_OPSYS = 2;
+	const COM_VSMART = 4;
+	const COM_KOHA = 5;
+	const COM_CARTHAME = 6;
+	const COM_NANOOK = 7;
+	const COM_ORPHEE = 8;
+	const COM_MICROBIB = 9;
+	const COM_BIBLIXNET = 10;
+
+
+
+	private $COM_CLASSES = array(self::COM_PERGAME => 'Class_WebService_SIGB_Pergame',
+															 self::COM_OPSYS => 'Class_WebService_SIGB_Opsys',
+															 self::COM_VSMART => 'Class_WebService_SIGB_VSmart',
+															 self::COM_KOHA => 'Class_WebService_SIGB_Koha',
+															 self::COM_CARTHAME => 'Class_WebService_SIGB_Carthame',
+															 self::COM_NANOOK => 'Class_WebService_SIGB_Nanook',
+															 self::COM_ORPHEE => 'Class_WebService_SIGB_Orphee',
+															 self::COM_MICROBIB => 'Class_WebService_SIGB_Microbib',
+															 self::COM_BIBLIXNET => 'Class_WebService_SIGB_BiblixNet');
+
 	protected $_table_name = 'int_bib';
 	protected $_table_primary = 'id_bib';
 
@@ -52,6 +74,16 @@ class Class_IntBib extends Storm_Model_Abstract {
 												'type' => $this->getCommSigb()]);
 	}
 
+
+	public function getSIGBComm() {
+		$type_comm = $this->getCommSigb();
+		if (!isset($this->COM_CLASSES[$type_comm]))
+			return null;
+
+		return call_user_func([$this->COM_CLASSES[$type_comm], 'getService'], 
+													$this->getModeComm());
+
+	}
 }
 
 ?>
\ No newline at end of file
diff --git a/library/Class/Users.php b/library/Class/Users.php
index f726817976d..b8d7ddfb7b3 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -178,8 +178,10 @@ class Class_Users extends Storm_Model_Abstract {
 
 
 	protected $_belongs_to = array('bib' => array('model' => 'Class_Bib',
-																								 'referenced_in' => 'id_site'),
-																  'zone' => array('through' => 'bib'));
+																								'referenced_in' => 'id_site'),
+																 'int_bib' => array('model' => 'Class_IntBib',
+																										'referenced_in' => 'id_site'),
+																 'zone' => array('through' => 'bib'));
 
 	protected $_default_attribute_values = array('id_site' => 0,
 																							 'role_level' => 0,
@@ -827,6 +829,13 @@ class Class_Users extends Storm_Model_Abstract {
 	}
 
 
+	public function getSIGBComm() {
+		if (null == $int_bib = $this->getIntBib())
+			return null;
+		return $int_bib->getSIGBComm();
+	}
+
+
 	public function updateSIGBOnSave() {
 		$this->getFicheSigb();
 		return $this;
diff --git a/library/Class/WebService/SIGB/AbstractService.php b/library/Class/WebService/SIGB/AbstractService.php
index 6efae1afee4..1844e9dbcb1 100644
--- a/library/Class/WebService/SIGB/AbstractService.php
+++ b/library/Class/WebService/SIGB/AbstractService.php
@@ -87,6 +87,10 @@ abstract class Class_WebService_SIGB_AbstractService {
 		return array('statut' => false,
 								  'erreur' => $message);
 	}
+
+	public function isPergame() {
+		return false;
+	}
 }
 
 ?>
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Pergame/Service.php b/library/Class/WebService/SIGB/Pergame/Service.php
index 9e9a6a959b0..2d655601c89 100644
--- a/library/Class/WebService/SIGB/Pergame/Service.php
+++ b/library/Class/WebService/SIGB/Pergame/Service.php
@@ -31,6 +31,10 @@ class Class_WebService_SIGB_Pergame_Service extends Class_WebService_SIGB_Abstra
 		return self::newInstance()->setIdBib($id_bib);
 	}
 
+	public function isPergame() {
+		return true;
+	}
+
 	public function setIdBib($id_bib) {
 		$this->_id_bib = $id_bib;
 		return $this;
-- 
GitLab