Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 843 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
class Utilisateurs {
        const TPL_URL_WS_ANNUAIRE = 'http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/%s';

        private $courriels = array();
        private $identites = array();
        private $clientRest = null;

        /**
        * Prend en paramêtre un tableau de courriels.
        *
        * @param array $courriels un tableau de courriels pour lesquels il faut rechercher les infos d'identité
        */
        public function __construct(Array $courriels = array(), RestClient $clientRest = null) {
                $this->courriels = $courriels;
                $this->clientRest = is_null($clientRest) ? new RestClient() : $clientRest;
        }

        public function setCourriels($courriels) {
                $this->courriels = $courriels;
        }
        /**
         * Lance l'interrogation du service de l'annuaire.
         *
         * @return null.
         */
        public function chargerIdentites() {
                $this->identites = $this->getIdentites();
        }

        /**
        * Retourne après avoir interrogé un service de l'annuaire, les intitulés correspondant aux
        * courriels des utilisateurs.
        *
        * @return mixed tableau avec en clé le courriel et en valeur l'intitulé de la personne à
        * afficher, false en cas d'erreur ou de résultat vide.
        */
        public function getIntitules() {
                $intitules = false;
                $this->chargerIdentites();
                if ($this->identites) {
                        $intitules = array();
                        foreach ($this->identites as $courriel => $infos) {
                                $intitules[$courriel] = $infos['intitule'];
                        }
                }
                return $intitules;
        }

        /**
         * Retourne un intitulé en fonction d'un courriel.
         *
         * @return String l'intitulé de l'utilisateur ou une chaine vide en cas de problème.
         */
        public function getIntitule($courriel) {
                $intitule = '';
                if ($this->contenirCourriel($courriel)) {
                        $intitule = $this->identites[$courriel]['intitule'];
                }
                return $intitule;
        }

        /**
         * Retourne l'identifiant de l'utilisateur en fonction d'un courriel.
         *
         * @return String l'id de l'utilisateur ou une chaine vide en cas de problème.
         */
        public function getId($courriel) {
                $id = '';
                if ($this->contenirCourriel($courriel)) {
                        $id = $this->identites[$courriel]['id'];
                }
                return $id;
        }

        private function contenirCourriel($courriel) {
                $ok = ($this->identites && isset($this->identites[$courriel])) ? true : false;
                return $ok;
        }

        private function getIdentites() {
                $utilisateursInfos = $this->consulterServiceAnnuaire();
                $identites = $this->extraireIdentites($utilisateursInfos);
                return $identites;
        }

        private function consulterServiceAnnuaire() {
                $url = sprintf(self::TPL_URL_WS_ANNUAIRE, implode(',', $this->courriels));
                $json = $this->clientRest->consulter($url);
                $utilisateurs = json_decode($json, true);
                return $utilisateurs;
        }

        private function extraireIdentites($utilisateursInfos) {
                $identites = array();
                foreach ($this->courriels as $courriel) {
                        $info = array('id' => null, 'intitule' => '');
                        if (isset($utilisateursInfos[$courriel])) {
                                $info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
                                $info['id'] = $utilisateursInfos[$courriel]['id'];
                        } else {
                                $info['intitule'] = $this->tronquerCourriel($courriel);
                        }
                        $identites[$courriel] = $info;
                }
                return $identites;
        }

        private function tronquerCourriel($courriel) {
                $courriel = str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
                return $courriel;
        }
}
?>