/[openfoncier]/trunk/services/messagesender.php
ViewVC logotype

Contents of /trunk/services/messagesender.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 813 - (show annotations)
Fri Nov 23 10:07:15 2012 UTC (12 years, 2 months ago) by mlimic
File size: 6650 byte(s)
Suppersion des lignes non-importantes dans services/messagesender.php

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26