Subversion Repositories eFlore/Applications.del

Rev

Rev 1815 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
935 gduche 1
<?php
1795 jpm 2
// declare(encoding='UTF-8');
935 gduche 3
/**
1707 jpm 4
 * Classe principale de chargement des sous-services d'accès aux infos sur les communes.
5
 *
1795 jpm 6
 * @category   DEL
7
 * @package    Services
8
 * @subpackage Communes
9
 * @version    0.1
10
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
11
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
13
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
1707 jpm 16
 */
935 gduche 17
class Communes extends RestService {
18
 
19
	private $parametres = array();
20
	private $ressources = array();
21
	private $methode = null;
1707 jpm 22
	private $serviceNom = 'communes';
23
	private $sousServiceNom = null;
935 gduche 24
	private $cheminCourant = null;
25
 
26
	private $conteneur;
1700 jpm 27
 
935 gduche 28
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
29
	protected $utilisationParametresBruts = true;
30
 
31
	public function __construct() {
32
		$this->cheminCourant = dirname(__FILE__).DS;
33
	}
34
 
35
	public function consulter($ressources, $parametres) {
36
		$this->methode = 'consulter';
1707 jpm 37
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
38
		return $this->executerService();
39
	}
40
 
41
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
42
		$this->ressources = $ressources;
43
		$this->parametres = $parametres;
44
	}
45
 
46
	private function executerService() {
935 gduche 47
		$reponseHttp = new ReponseHttp();
48
		try {
49
			$this->conteneur = new Conteneur($this->parametres);
50
			$resultat = $this->traiterRessources();
51
			$reponseHttp->setResultatService($resultat);
52
		} catch (Exception $e) {
53
			$reponseHttp->ajouterErreur($e);
54
		}
55
		$reponseHttp->emettreLesEntetes();
56
		$corps = $reponseHttp->getCorps();
57
		return $corps;
58
	}
1700 jpm 59
 
935 gduche 60
	private function traiterRessources() {
1707 jpm 61
		$this->analyserRessources();
935 gduche 62
		$retour = $this->initialiserService();
63
		return $retour;
64
	}
65
 
1707 jpm 66
	private function analyserRessources() {
67
		if ($this->methode == 'consulter') {
1806 jpm 68
			if (count($this->ressources) == 0 && $this->verifierPresenceParametre('masque.nom')) {
69
				$this->sousServiceNom = 'liste-communes';
1707 jpm 70
			}
71
		}
72
		if ($this->sousServiceNom == null) {
73
			$this->lancerMessageErreurRessource();
74
		}
935 gduche 75
	}
76
 
1707 jpm 77
	private function verifierPresenceParametre($cle) {
1806 jpm 78
		if (isset($this->parametres[$cle]) && trim($this->parametres[$cle]) == '') {
79
			$message = "Le service demandé '{$this->serviceNom}' ".
80
				"nécessite l'utilisation d'un paramètre (non vide) : masque.nom \n";
81
			throw new Exception($message, RestServeur::HTTP_CODE_ECHEC_CONDITION);
1707 jpm 82
		}
1806 jpm 83
		return true;
1707 jpm 84
	}
85
 
86
	private function lancerMessageErreurRessource() {
87
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
88
		$message = "La ressource demandée '$ressource' ".
89
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
90
			"Les URLs disponibles sont : \n".
91
			" - en GET : communes (paramètres : masque.nom) \n";
92
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
93
		throw new Exception($message, $code);
94
	}
95
 
935 gduche 96
	private function initialiserService() {
1707 jpm 97
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
935 gduche 98
		$chemins = array();
1707 jpm 99
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
935 gduche 100
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
101
		$retour = '';
102
		$service = null;
1707 jpm 103
 
935 gduche 104
		foreach ($chemins as $chemin) {
105
			if (file_exists($chemin)) {
106
				require_once $chemin;
107
				$service = new $classe($this->conteneur);
108
				if ($this->methode == 'consulter') {
1809 jpm 109
					$retour = $service->consulter();
935 gduche 110
				} else {
1707 jpm 111
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
112
						"ne possède pas de méthode '{$this->methode}' !";
113
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
114
					throw new Exception($message, $code);
935 gduche 115
				}
116
			}
117
		}
1700 jpm 118
 
935 gduche 119
		if (is_null($service)) {
1707 jpm 120
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
121
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
122
				"est introuvable par le service '{$this->serviceNom}' !";
935 gduche 123
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
124
			throw new Exception($message, $code);
125
		}
126
		return $retour;
127
	}
1700 jpm 128
 
935 gduche 129
	private function obtenirNomClasseService($mot) {
130
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
131
		return $classeNom;
132
	}
1707 jpm 133
}