1 |
<?php |
2 |
|
3 |
|
4 |
require_once('vendor/autoload.php'); |
5 |
use PhpAmqpLib\Connection\AMQPConnection; |
6 |
use PhpAmqpLib\Message\AMQPMessage; |
7 |
|
8 |
(defined("DEBUG") ? "" : define("DEBUG", EXTRA_VERBOSE_MODE)); |
9 |
|
10 |
// pour des besoins de logging il faut setter $_SERVER["REQUEST_URI"] |
11 |
if (!isset($_SERVER["REQUEST_URI"])) { |
12 |
$_SERVER["REQUEST_URI"] = __FILE__; |
13 |
} |
14 |
|
15 |
// Si le AMQP_DEBUG est defini a true, les messages DEBUG sont envoyes aux |
16 |
// stdout ce qui peut etre bien pour deboguer |
17 |
if (DEBUG > PRODUCTION_MODE) { |
18 |
define('AMQP_DEBUG', true); |
19 |
} |
20 |
|
21 |
|
22 |
class MessageEnqueuer { |
23 |
|
24 |
var $ERP_DEMANDE_COMPLETUDE_PC = 'erpDemandeCompletudePc'; |
25 |
var $ERP_QUALIFIE = 'erpQualifie'; |
26 |
var $ERP_DEMANDE_QUALIFICATION_PC = 'erpDemandeQualificationPc'; |
27 |
var $ERP_DEMANDE_INSTRUCTION_PC = 'erpDemandeInstructionPc'; |
28 |
var $ERP_CONSULTATION_CONFORMITE = 'erpConsultationConformite'; |
29 |
var $ERP_DEMANDE_OUVERTURE_PC = 'erpDemandeOuverturePc'; |
30 |
var $ERP_DEPOT_DOSSIER_DAT = 'erpDepotDossierDat'; |
31 |
var $ERP_ANNULATION_DEMANDE = 'erpAnnulationDemande'; |
32 |
var $ERP_DEMANDE_OUVERTURE_DAT = 'erpDemandeOuvertureDat'; |
33 |
var $ERP_DECISION_CONFORMITE_EFFECTUE = 'erpDecisionConformiteEffectue'; |
34 |
var $ERP_ARRETE_PC_EFFECTUE = 'erpArretePcEffectue'; |
35 |
|
36 |
var $BAD_DATA = -2; |
37 |
var $OK = 0; |
38 |
var $KO = -1; |
39 |
|
40 |
|
41 |
public function __construct() { |
42 |
include_once('../dyn/services.inc.php'); |
43 |
$this->ret_array = array(); |
44 |
$this->ret_array['date'] = date('d/m/Y H:i'); |
45 |
$this->ret_array['emetteur'] = $_SESSION['login']; |
46 |
$this->dossier_instruction = null; |
47 |
$this->competence = null; |
48 |
$this->contrainte_plu = null; |
49 |
$this->consultation = null; |
50 |
$this->decision = null; |
51 |
$this->reference_cadastrale = null; |
52 |
|
53 |
// les donnees de la connexion |
54 |
$this->queue = $ERP_QUEUE; |
55 |
$this->host = $ERP_CONNECTION_HOST; |
56 |
$this->port = $ERP_CONNECTIOn_PORT; |
57 |
$this->user = $ERP_CONNECTION_USER; |
58 |
$this->password = $ERP_CONNECTION_PASSWORD; |
59 |
$this->vhost = $ERP_CONNECTION_VHOST; |
60 |
} |
61 |
|
62 |
|
63 |
/** |
64 |
* Precondition: $msg_type est egal a valeur d'une des $ERP_* |
65 |
*/ |
66 |
public function enqueueMessage($method) { |
67 |
if (method_exists($this, $method)) { |
68 |
return $this->$method(); |
69 |
} |
70 |
return $this->KO; |
71 |
} |
72 |
|
73 |
|
74 |
private function enqueueRequest() { |
75 |
$msg_str = json_encode($this->ret_array); |
76 |
|
77 |
if ($msg_str === false) { |
78 |
return $this->KO; |
79 |
} |
80 |
|
81 |
$exchange = 'router'; |
82 |
$queue = $this->queue; |
83 |
|
84 |
$conn = new AMQPConnection($this->host, $this->port, $this->user, |
85 |
$this->password, $this->vhost); |
86 |
$ch = $conn->channel(); |
87 |
if (is_null($ch)) { |
88 |
return -1; |
89 |
} |
90 |
|
91 |
$ch->queue_declare($queue, false, true, false, false); |
92 |
|
93 |
$ch->exchange_declare($exchange, 'direct', false, true, false); |
94 |
|
95 |
$ch->queue_bind($queue, $exchange); |
96 |
|
97 |
$msg = new AMQPMessage($msg_str, |
98 |
array('content_type' => 'text/plain', 'delivery_mode' => 2)); |
99 |
$ch->basic_publish($msg, $exchange); |
100 |
|
101 |
$ch->close(); |
102 |
$conn->close(); |
103 |
|
104 |
// log le message qui etait mis sur la pile dans RabbitMQ |
105 |
logger::instance()->log("Ajout du message sur pile: ". |
106 |
json_encode($this->ret_array), EXTRA_VERBOSE_MODE); |
107 |
|
108 |
return 0; |
109 |
} |
110 |
|
111 |
|
112 |
public function setDossierInstructionIdentifier($id) { |
113 |
$this->dossier_instruction = $id; |
114 |
} |
115 |
|
116 |
|
117 |
public function setCompetence($competence) { |
118 |
$this->competence = $competence; |
119 |
if (is_null($this->competence)) { |
120 |
$this->competence = ""; |
121 |
} |
122 |
} |
123 |
|
124 |
|
125 |
public function setContraintePlu($contrainte_plu) { |
126 |
$this->contrainte_plu = $contrainte_plu; |
127 |
if (is_null($this->contrainte_plu)) { |
128 |
$this->contrainte_plu = ""; |
129 |
} |
130 |
} |
131 |
|
132 |
|
133 |
public function setConsultationIdentifier($id) { |
134 |
$this->consultation = $id; |
135 |
} |
136 |
|
137 |
|
138 |
public function setDecision($decision) { |
139 |
$this->decision = $decision; |
140 |
if (is_null($this->decision)) { |
141 |
$this->decision = ""; |
142 |
} |
143 |
} |
144 |
|
145 |
|
146 |
public function setReferenceCadastrale($ref_cad) { |
147 |
$this->reference_cadastrale = $ref_cad; |
148 |
if (is_null($this->reference_cadastrale)) { |
149 |
$this->reference_cadastrale = ""; |
150 |
} |
151 |
} |
152 |
|
153 |
|
154 |
private function generic($msg_type, $contenu_keys = null) { |
155 |
$this->ret_array["type"] = $msg_type; |
156 |
if (is_null($this->dossier_instruction)) { |
157 |
// manque des donnees |
158 |
return $this->BAD_DATA; |
159 |
} |
160 |
$this->ret_array['date'] = date('d/m/Y H:i'); |
161 |
$this->ret_array['emetteur'] = $_SESSION['login']; |
162 |
$this->ret_array['dossier_instruction'] = $this->dossier_instruction; |
163 |
|
164 |
if (!is_null($contenu_keys)) { |
165 |
$contenu = array(); |
166 |
foreach ($contenu_keys as $attrib) { |
167 |
if (is_null($this->$attrib)) { |
168 |
return $this->BAD_DATA; |
169 |
} |
170 |
$contenu[$attrib] = $this->$attrib; |
171 |
} |
172 |
$this->ret_array['contenu'] = $contenu; |
173 |
} |
174 |
return $this->enqueueRequest(); |
175 |
} |
176 |
|
177 |
|
178 |
private function erpQualifie() { |
179 |
$contenu_keys = array('competence', |
180 |
'contrainte_plu', |
181 |
'reference_cadastrale'); |
182 |
return $this->generic('ERP Qualifié', |
183 |
$contenu_keys); |
184 |
} |
185 |
|
186 |
|
187 |
private function erpDemandeCompletudePc() { |
188 |
return $this->generic('Demande de complétude dossier ERP'); |
189 |
} |
190 |
|
191 |
|
192 |
private function erpDemandeQualificationPc() { |
193 |
return $this->generic('Demande de qualification ERP'); |
194 |
} |
195 |
|
196 |
|
197 |
private function erpDemandeInstructionPc() { |
198 |
if (is_null($this->consultation)) { |
199 |
return $this->BAD_DATA; |
200 |
} |
201 |
//$this->ret_array['type'] = 'Demande d\'avis de dossier PC pour un ERP'; |
202 |
$this->ret_array['consultation'] = $this->consultation; |
203 |
return $this->generic('Demande d\'avis de dossier PC pour un ERP'); |
204 |
} |
205 |
|
206 |
|
207 |
private function erpArretePcEffectue() { |
208 |
if (is_null($this->dossier_instruction) |
209 |
|| is_null($this->decision)) { |
210 |
// manque des donnees |
211 |
return $this->BAD_DATA; |
212 |
} |
213 |
return $this->generic('Arrêté PC effectué', |
214 |
array('decision')); |
215 |
} |
216 |
|
217 |
|
218 |
private function erpConsultationConformite() { |
219 |
if (is_null($this->consultation)) { |
220 |
return $this->BAD_DATA; |
221 |
} |
222 |
$this->ret_array['consultation'] = $this->consultation; |
223 |
return $this->generic('Consultation ERP pour conformité'); |
224 |
} |
225 |
|
226 |
|
227 |
private function erpDemandeOuverturePc() { |
228 |
return $this->generic('Demande d\'ouverture ERP PC'); |
229 |
} |
230 |
|
231 |
|
232 |
private function erpDepotDossierDat() { |
233 |
return $this->generic('Dépôt de dossier DAT'); |
234 |
} |
235 |
|
236 |
|
237 |
public function erpAnnulationDemande() { |
238 |
return $this->generic('Annulation de la demande'); |
239 |
} |
240 |
|
241 |
|
242 |
public function erpDemandeOuvertureDat() { |
243 |
return $this->generic('Demande d\'ouverture ERP DAT'); |
244 |
} |
245 |
|
246 |
|
247 |
public function erpDecisionConformiteEffectue() { |
248 |
return $this->generic('Décision de conformité effectuée'); |
249 |
} |
250 |
} |
251 |
|
252 |
?> |