44use webfiori \database \ConnectionInfo ;
55use webfiori \email \SMTPAccount ;
66use webfiori \file \File ;
7+ use webfiori \framework \exceptions \InitializationException ;
78use webfiori \http \Uri ;
89use webfiori \json \Json ;
910
@@ -134,8 +135,8 @@ public function addOrUpdateDBConnection(ConnectionInfo $dbConnectionsInfo) {
134135 'username ' => $ dbConnectionsInfo ->getUsername (),
135136 'database ' => $ dbConnectionsInfo ->getDBName (),
136137 'password ' => $ dbConnectionsInfo ->getPassword (),
137- 'extars ' => $ dbConnectionsInfo ->getExtars (),
138138 ]);
139+ $ connectionJAsJson ->addArray ('extras ' , $ dbConnectionsInfo ->getExtars (), true );
139140 $ this ->json ->get ('database-connections ' )->add ($ dbConnectionsInfo ->getName (), $ connectionJAsJson );
140141 $ this ->writeJson ();
141142 }
@@ -207,33 +208,66 @@ public function getDBConnection(string $conName) {
207208 $ jsonObj = $ this ->json ->get ('database-connections ' )->get ($ conName );
208209
209210 if ($ jsonObj !== null ) {
211+ $ extras = $ jsonObj ->get ('extras ' );
212+ $ extrasArr = [];
213+ if ($ extras instanceof Json) {
214+ foreach ($ extras ->getProperties () as $ prop ) {
215+ $ extrasArr [$ prop ->getName ()] = $ prop ->getValue ();
216+ }
217+ }
210218 return new ConnectionInfo (
211219 $ jsonObj ->get ('type ' ),
212220 $ jsonObj ->get ('username ' ),
213221 $ jsonObj ->get ('password ' ),
214222 $ jsonObj ->get ('database ' ),
215223 $ jsonObj ->get ('host ' ),
216224 $ jsonObj ->get ('port ' ),
217- $ jsonObj -> get ( ' extras ' ) !== null ? $ jsonObj -> get ( ' extras ' ) : [] );
225+ $ extrasArr );
218226 }
219227 }
220-
228+ /**
229+ * Returns an associative array that contain the information of database connections.
230+ *
231+ * @return array An associative array. The indices are connections names and
232+ * values are objects of type 'ConnectionInfo'.
233+ */
221234 public function getDBConnections (): array {
222235 $ accountsInfo = $ this ->json ->get ('database-connections ' );
223236 $ retVal = [];
224237
225238 foreach ($ accountsInfo ->getProperties () as $ propObj ) {
239+ $ name = $ propObj ->getName ();
226240 $ jsonObj = $ propObj ->getValue ();
227- $ acc = new ConnectionInfo ($ jsonObj ->get ('type ' ), $ jsonObj ->get ('username ' ), $ jsonObj ->get ('password ' ), $ jsonObj ->get ('database ' ));
228- $ acc ->setExtras ($ jsonObj ->get ('extras ' ) !== null ? $ jsonObj ->get ('extras ' ) : []);
229- $ acc ->setHost ($ jsonObj ->get ('host ' ));
241+ $ acc = new ConnectionInfo (
242+ $ this ->getProp ($ jsonObj , 'type ' , $ name ),
243+ $ this ->getProp ($ jsonObj , 'username ' , $ name ),
244+ $ this ->getProp ($ jsonObj , 'password ' , $ name ),
245+ $ this ->getProp ($ jsonObj , 'database ' , $ name ));
246+ $ extrasObj = $ jsonObj ->get ('extras ' );
247+
248+ if ($ extrasObj !== null && $ extrasObj instanceof Json) {
249+ $ extrasArr = [];
250+
251+ foreach ($ extrasObj ->getProperties () as $ prop ) {
252+ $ extrasArr [$ prop ->getName ()] = $ prop ->getValue ();
253+ }
254+ $ acc ->setExtras ($ extrasArr );
255+ }
256+ $ acc ->setHost ($ this ->getProp ($ jsonObj , 'host ' , $ name ));
230257 $ acc ->setName ($ propObj ->getName ());
231- $ acc ->setPort ($ jsonObj -> get ( 'port ' ));
258+ $ acc ->setPort ($ this -> getProp ( $ jsonObj , 'port ' , $ name ));
232259 $ retVal [$ propObj ->getName ()] = $ acc ;
233260 }
234261
235262 return $ retVal ;
236263 }
264+ private function getProp (Json $ j , $ name , string $ connName ) {
265+ $ val = $ j ->get ($ name );
266+ if ($ val === null ) {
267+ throw new InitializationException ('The property " ' .$ name .'" of the connection " ' .$ connName .'" is missing. ' );
268+ }
269+ return $ val ;
270+ }
237271
238272 public function getDescription (string $ langCode ) {
239273 return $ this ->json ->get ('app-descriptions ' )->get (strtoupper (trim ($ langCode )));
@@ -321,17 +355,21 @@ public function getSMTPConnection(string $name) {
321355
322356 if ($ jsonObj !== null ) {
323357 return new SMTPAccount ([
324- 'sender-address ' => $ jsonObj -> get ( 'address ' ),
325- 'pass ' => $ jsonObj -> get ( 'password ' ),
326- 'port ' => $ jsonObj -> get ( 'port ' ),
327- 'sender-name ' => $ jsonObj -> get ( 'sender-name ' ),
328- 'server-address ' => $ jsonObj -> get ( 'host ' ),
329- 'user ' => $ jsonObj -> get ( 'username ' ),
358+ 'sender-address ' => $ this -> getProp ( $ jsonObj , 'address ' , $ name ),
359+ 'pass ' => $ this -> getProp ( $ jsonObj , 'password ' , $ name ),
360+ 'port ' => $ this -> getProp ( $ jsonObj , 'port ' , $ name ),
361+ 'sender-name ' => $ this -> getProp ( $ jsonObj , 'sender-name ' , $ name ),
362+ 'server-address ' => $ this -> getProp ( $ jsonObj , 'host ' , $ name ),
363+ 'user ' => $ this -> getProp ( $ jsonObj , 'username ' , $ name ),
330364 'account-name ' => $ name
331365 ]);
332366 }
333367 }
334-
368+ /**
369+ * Returns an array that contains all added SMTP accounts.
370+ *
371+ * @return array An array that contains all added SMTP accounts.
372+ */
335373 public function getSMTPConnections (): array {
336374 $ accountsInfo = $ this ->json ->get ('smtp-connections ' );
337375 $ retVal = [];
@@ -340,13 +378,13 @@ public function getSMTPConnections(): array {
340378 $ jsonObj = $ prop ->getValue ();
341379 $ acc = new SMTPAccount ();
342380 $ acc ->setAccountName ($ name );
343- $ acc ->setAddress ($ jsonObj -> get ( 'address ' ));
344- $ acc ->setPassword ($ jsonObj -> get ( 'password ' ));
345- $ acc ->setPort ($ jsonObj -> get ( 'port ' ));
346- $ acc ->setSenderName ($ jsonObj -> get ( 'sender-name ' ));
347- $ acc ->setServerAddress ($ jsonObj -> get ( 'host ' ));
348- $ acc ->setUsername ($ jsonObj -> get ( 'username ' ));
349- $ retVal [] = $ acc ;
381+ $ acc ->setAddress ($ this -> getProp ( $ jsonObj , 'address ' , $ name ));
382+ $ acc ->setPassword ($ this -> getProp ( $ jsonObj , 'password ' , $ name ));
383+ $ acc ->setPort ($ this -> getProp ( $ jsonObj , 'port ' , $ name ));
384+ $ acc ->setSenderName ($ this -> getProp ( $ jsonObj , 'sender-name ' , $ name ));
385+ $ acc ->setServerAddress ($ this -> getProp ( $ jsonObj , 'host ' , $ name ));
386+ $ acc ->setUsername ($ this -> getProp ( $ jsonObj , 'username ' , $ name ));
387+ $ retVal [$ name ] = $ acc ;
350388 }
351389
352390 return $ retVal ;
0 commit comments