1+ <?php
2+ /**
3+ * Copyright (C) 2013-2024 Combodo SAS
4+ *
5+ * This file is part of iTop.
6+ *
7+ * iTop is free software; you can redistribute it and/or modify
8+ * it under the terms of the GNU Affero General Public License as published by
9+ * the Free Software Foundation, either version 3 of the License, or
10+ * (at your option) any later version.
11+ *
12+ * iTop is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ * GNU Affero General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Affero General Public License
18+ */
19+
20+ // Important: Unfortunately, for now AsyncTask classes CANNOT have a namespace, it will crash the OQL parser.
21+
22+ use Combodo \iTop \Core \WebRequest ;
23+ use Combodo \iTop \Service \WebRequestSender ;
24+ // If we ever move this class back to a namespace, mind putting the following uses back
25+ //use AsyncTask;
26+ //use AttributeString;
27+ //use AttributeLongText;
28+ //use MetaModel;
29+
30+ /**
31+ * Class SendWebRequest
32+ *
33+ * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
34+ */
35+ class SendWebRequest extends AsyncTask
36+ {
37+ /**
38+ * @inheritDoc
39+ * @throws \CoreException
40+ * @throws \Exception
41+ */
42+ public static function Init ()
43+ {
44+ $ aParams = array
45+ (
46+ "category " => "core/cmdb " ,
47+ "key_type " => "autoincrement " ,
48+ "name_attcode " => "created " ,
49+ "state_attcode " => "" ,
50+ "reconc_keys " => array (),
51+ "db_table " => "priv_async_send_web_request " ,
52+ "db_key_field " => "id " ,
53+ "db_finalclass_field " => "" ,
54+ "display_template " => "" ,
55+ );
56+ MetaModel::Init_Params ($ aParams );
57+ MetaModel::Init_InheritAttributes ();
58+
59+ MetaModel::Init_AddAttribute (new AttributeLongText ("request " , array ("allowed_values " =>null , "sql " =>"request " , "default_value " =>null , "is_null_allowed " =>false , "depends_on " =>array ())));
60+ }
61+
62+ /**
63+ * Add the $oWebRequest to the queue to be send later (background task for example)
64+ *
65+ * @param \Combodo\iTop\Core\WebRequest $oWebRequest
66+ * @param \EventNotification|null $oLog
67+ *
68+ * @throws \ArchivedObjectException
69+ * @throws \CoreCannotSaveObjectException
70+ * @throws \CoreException
71+ * @throws \CoreUnexpectedValue
72+ * @throws \CoreWarning
73+ * @throws \MySQLException
74+ * @throws \OQLException
75+ *
76+ * @return void
77+ */
78+ public static function AddToQueue (WebRequest $ oWebRequest , $ oLog = null )
79+ {
80+ $ oNew = new static ();
81+ if ($ oLog )
82+ {
83+ $ oNew ->Set ('event_id ' , $ oLog ->GetKey ());
84+ }
85+
86+ $ oNew ->Set ('request ' , serialize ($ oWebRequest ));
87+ $ oNew ->DBInsert ();
88+ }
89+
90+ /**
91+ * @inheritDoc
92+ */
93+ public function DoProcess ()
94+ {
95+ $ aIssues = array ();
96+ $ oWebRequest = unserialize ($ this ->Get ('request ' ));
97+
98+ // Retrieve log event
99+ /** @var \AttributeExternalKey $oAttDef */
100+ $ oAttDef = MetaModel::GetAttributeDef (get_class ($ this ), 'event_id ' );
101+ $ oLog = MetaModel::GetObject ($ oAttDef ->GetTargetClass (), $ this ->Get ('event_id ' ), false , true );
102+
103+ $ oSenderService = WebRequestSender::GetInstance ();
104+ $ aResult = $ oSenderService ->Send ($ oWebRequest , $ aIssues , $ oLog , WebRequestSender::ENUM_SEND_MODE_SYNC );
105+ switch ($ aResult ['sender_status ' ])
106+ {
107+ case WebRequestSender::ENUM_SEND_STATE_OK :
108+ return 'Sent ' ;
109+
110+ case WebRequestSender::ENUM_SEND_STATE_PENDING :
111+ return 'Whoops! Seems like a bug occurred, the request should be sent in synchronous mode ' ;
112+
113+ case WebRequestSender::ENUM_SEND_STATE_ERROR :
114+ return 'Failed: ' .implode (', ' , $ aIssues );
115+ }
116+ }
117+ }
0 commit comments