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