| 139 | 
           jpm | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           class Utilisateurs {
  | 
        
        
            | 
            | 
           3 | 
           	const TPL_URL_WS_ANNUAIRE = 'http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/%s';
  | 
        
        
            | 
            | 
           4 | 
              | 
        
        
            | 
            | 
           5 | 
           	private $courriels = array();
  | 
        
        
           | 387 | 
           jpm | 
           6 | 
           	private $identites = array();
  | 
        
        
           | 139 | 
           jpm | 
           7 | 
           	private $clientRest = null;
  | 
        
        
            | 
            | 
           8 | 
              | 
        
        
            | 
            | 
           9 | 
           	/**
  | 
        
        
            | 
            | 
           10 | 
           	* Prend en paramêtre un tableau de courriels.
  | 
        
        
            | 
            | 
           11 | 
           	*
  | 
        
        
            | 
            | 
           12 | 
           	* @param array $courriels un tableau de courriels pour lesquels il faut rechercher les infos d'identité
  | 
        
        
            | 
            | 
           13 | 
           	*/
  | 
        
        
            | 
            | 
           14 | 
           	public function __construct(Array $courriels = array(), RestClient $clientRest = null) {
  | 
        
        
            | 
            | 
           15 | 
           		$this->courriels = $courriels;
  | 
        
        
            | 
            | 
           16 | 
           		$this->clientRest = is_null($clientRest) ? new RestClient() : $clientRest;
  | 
        
        
            | 
            | 
           17 | 
           	}
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           	public function setCourriels($courriels) {
  | 
        
        
            | 
            | 
           20 | 
           		$this->courriels = $courriels;
  | 
        
        
            | 
            | 
           21 | 
           	}
  | 
        
        
           | 387 | 
           jpm | 
           22 | 
           	/**
  | 
        
        
            | 
            | 
           23 | 
           	 * Lance l'interrogation du service de l'annuaire.
  | 
        
        
            | 
            | 
           24 | 
           	 *
  | 
        
        
            | 
            | 
           25 | 
           	 * @return null.
  | 
        
        
            | 
            | 
           26 | 
           	 */
  | 
        
        
            | 
            | 
           27 | 
           	public function chargerIdentites() {
  | 
        
        
            | 
            | 
           28 | 
           		$this->identites = $this->getIdentites();
  | 
        
        
            | 
            | 
           29 | 
           	}
  | 
        
        
           | 139 | 
           jpm | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           	/**
  | 
        
        
           | 387 | 
           jpm | 
           32 | 
           	* Retourne après avoir interrogé un service de l'annuaire, les intitulés correspondant aux
  | 
        
        
           | 139 | 
           jpm | 
           33 | 
           	* courriels des utilisateurs.
  | 
        
        
            | 
            | 
           34 | 
           	*
  | 
        
        
            | 
            | 
           35 | 
           	* @return mixed tableau avec en clé le courriel et en valeur l'intitulé de la personne à
  | 
        
        
            | 
            | 
           36 | 
           	* afficher, false en cas d'erreur ou de résultat vide.
  | 
        
        
            | 
            | 
           37 | 
           	*/
  | 
        
        
            | 
            | 
           38 | 
           	public function getIntitules() {
  | 
        
        
            | 
            | 
           39 | 
           		$intitules = false;
  | 
        
        
           | 387 | 
           jpm | 
           40 | 
           		$this->chargerIdentites();
  | 
        
        
            | 
            | 
           41 | 
           		if ($this->identites) {
  | 
        
        
           | 139 | 
           jpm | 
           42 | 
           			$intitules = array();
  | 
        
        
           | 387 | 
           jpm | 
           43 | 
           			foreach ($this->identites as $courriel => $infos) {
  | 
        
        
           | 139 | 
           jpm | 
           44 | 
           				$intitules[$courriel] = $infos['intitule'];
  | 
        
        
            | 
            | 
           45 | 
           			}
  | 
        
        
            | 
            | 
           46 | 
           		}
  | 
        
        
            | 
            | 
           47 | 
           		return $intitules;
  | 
        
        
            | 
            | 
           48 | 
           	}
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
           | 387 | 
           jpm | 
           50 | 
           	/**
  | 
        
        
            | 
            | 
           51 | 
           	 * Retourne un intitulé en fonction d'un courriel.
  | 
        
        
            | 
            | 
           52 | 
           	 *
  | 
        
        
            | 
            | 
           53 | 
           	 * @return String l'intitulé de l'utilisateur ou une chaine vide en cas de problème.
  | 
        
        
            | 
            | 
           54 | 
           	 */
  | 
        
        
            | 
            | 
           55 | 
           	public function getIntitule($courriel) {
  | 
        
        
            | 
            | 
           56 | 
           		$intitule = '';
  | 
        
        
            | 
            | 
           57 | 
           		if ($this->contenirCourriel($courriel)) {
  | 
        
        
            | 
            | 
           58 | 
           			$intitule = $this->identites[$courriel]['intitule'];
  | 
        
        
            | 
            | 
           59 | 
           		}
  | 
        
        
            | 
            | 
           60 | 
           		return $intitule;
  | 
        
        
            | 
            | 
           61 | 
           	}
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
           	/**
  | 
        
        
            | 
            | 
           64 | 
           	 * Retourne l'identifiant de l'utilisateur en fonction d'un courriel.
  | 
        
        
            | 
            | 
           65 | 
           	 *
  | 
        
        
            | 
            | 
           66 | 
           	 * @return String l'id de l'utilisateur ou une chaine vide en cas de problème.
  | 
        
        
            | 
            | 
           67 | 
           	 */
  | 
        
        
            | 
            | 
           68 | 
           	public function getId($courriel) {
  | 
        
        
            | 
            | 
           69 | 
           		$id = '';
  | 
        
        
            | 
            | 
           70 | 
           		if ($this->contenirCourriel($courriel)) {
  | 
        
        
            | 
            | 
           71 | 
           			$id = $this->identites[$courriel]['id'];
  | 
        
        
            | 
            | 
           72 | 
           		}
  | 
        
        
            | 
            | 
           73 | 
           		return $id;
  | 
        
        
            | 
            | 
           74 | 
           	}
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
           	private function contenirCourriel($courriel) {
  | 
        
        
            | 
            | 
           77 | 
           		$ok = ($this->identites && isset($this->identites[$courriel])) ? true : false;
  | 
        
        
            | 
            | 
           78 | 
           		return $ok;
  | 
        
        
            | 
            | 
           79 | 
           	}
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
           | 139 | 
           jpm | 
           81 | 
           	private function getIdentites() {
  | 
        
        
            | 
            | 
           82 | 
           		$utilisateursInfos = $this->consulterServiceAnnuaire();
  | 
        
        
            | 
            | 
           83 | 
           		$identites = $this->extraireIdentites($utilisateursInfos);
  | 
        
        
            | 
            | 
           84 | 
           		return $identites;
  | 
        
        
            | 
            | 
           85 | 
           	}
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           	private function consulterServiceAnnuaire() {
  | 
        
        
            | 
            | 
           88 | 
           		$url = sprintf(self::TPL_URL_WS_ANNUAIRE, implode(',', $this->courriels));
  | 
        
        
            | 
            | 
           89 | 
           		$json = $this->clientRest->consulter($url);
  | 
        
        
            | 
            | 
           90 | 
           		$utilisateurs = json_decode($json, true);
  | 
        
        
            | 
            | 
           91 | 
           		return $utilisateurs;
  | 
        
        
            | 
            | 
           92 | 
           	}
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
           	private function extraireIdentites($utilisateursInfos) {
  | 
        
        
            | 
            | 
           95 | 
           		$identites = array();
  | 
        
        
            | 
            | 
           96 | 
           		foreach ($this->courriels as $courriel) {
  | 
        
        
            | 
            | 
           97 | 
           			$info = array('id' => null, 'intitule' => '');
  | 
        
        
            | 
            | 
           98 | 
           			if (isset($utilisateursInfos[$courriel])) {
  | 
        
        
            | 
            | 
           99 | 
           				$info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
  | 
        
        
            | 
            | 
           100 | 
           				$info['id'] = $utilisateursInfos[$courriel]['id'];
  | 
        
        
            | 
            | 
           101 | 
           			} else {
  | 
        
        
            | 
            | 
           102 | 
           				$info['intitule'] = $this->tronquerCourriel($courriel);
  | 
        
        
            | 
            | 
           103 | 
           			}
  | 
        
        
            | 
            | 
           104 | 
           			$identites[$courriel] = $info;
  | 
        
        
            | 
            | 
           105 | 
           		}
  | 
        
        
            | 
            | 
           106 | 
           		return $identites;
  | 
        
        
            | 
            | 
           107 | 
           	}
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
           	private function tronquerCourriel($courriel) {
  | 
        
        
            | 
            | 
           110 | 
           		$courriel = str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
  | 
        
        
            | 
            | 
           111 | 
           		return $courriel;
  | 
        
        
            | 
            | 
           112 | 
           	}
  | 
        
        
            | 
            | 
           113 | 
           }
  | 
        
        
            | 
            | 
           114 | 
           ?>
  |