Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 845 | Go to most recent revision | 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($this->courriels);
        }

        /**
        * 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() {
                $this->chargerIdentites();
                if (! $this->identites) return false;

                $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) {
                if ($this->contenirCourriel($courriel)) {
                        return $this->identites[$courriel]['intitule'];
                }
                return '';
        }

        /**
         * 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) {
                if ($this->contenirCourriel($courriel)) {
                        return $this->identites[$courriel]['id'];
                }
                return '';
        }

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

        private function getIdentites($courriels) {
                // consulterServiceAnnuaire
                $courriels = array_unique($courriels);
                $utilisateursInfos = json_decode($this->clientRest->consulter(sprintf(self::TPL_URL_WS_ANNUAIRE,
                                                                                                                                                          implode(',', $courriels))),
                                                                                 true);
                return self::extraireIdentites($utilisateursInfos, $this->courriels);
        }

        static function extraireIdentites($utilisateursInfos, $courriels) {
                $identites = array();
                foreach ($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'] = self::tronquerCourriel($courriel);
                        }
                        $identites[$courriel] = $info;
                }
                return $identites;
        }

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