Skip to content

Commit

Permalink
Enhanced How Missing Connection Properties are Shown
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Oct 30, 2023
1 parent a6c9709 commit 29d16f9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
11 changes: 10 additions & 1 deletion tests/webfiori/framework/test/config/JsonDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,14 @@ public function testDatabaseConnections01() {
$this->assertEquals('root', $account->getUsername());

}

/**
* @test
*/
public function testAppWithError00() {
$this->expectExceptionMessage('The property "username" of the connection "New_Connection" is missing.');
JsonDriver::setConfigFileName('config-with-err-00');
$driver = new JsonDriver();
$driver->initialize();
$driver->getDBConnections();
}
}
80 changes: 59 additions & 21 deletions webfiori/framework/config/JsonDriver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace webfiori\framework\config;

use Exception;
use webfiori\database\ConnectionInfo;
use webfiori\email\SMTPAccount;
use webfiori\file\File;
Expand Down Expand Up @@ -134,8 +135,8 @@ public function addOrUpdateDBConnection(ConnectionInfo $dbConnectionsInfo) {
'username' => $dbConnectionsInfo->getUsername(),
'database' => $dbConnectionsInfo->getDBName(),
'password' => $dbConnectionsInfo->getPassword(),
'extars' => $dbConnectionsInfo->getExtars(),
]);
$connectionJAsJson->addArray('extras', $dbConnectionsInfo->getExtars(), true);
$this->json->get('database-connections')->add($dbConnectionsInfo->getName(), $connectionJAsJson);
$this->writeJson();
}
Expand Down Expand Up @@ -207,33 +208,66 @@ public function getDBConnection(string $conName) {
$jsonObj = $this->json->get('database-connections')->get($conName);

if ($jsonObj !== null) {
$extras = $jsonObj->get('extras');
$extrasArr = [];
if ($extras instanceof Json) {
foreach ($extras->getProperties() as $prop) {
$extrasArr[$prop->getName()] = $prop->getValue();
}
}
return new ConnectionInfo(
$jsonObj->get('type'),
$jsonObj->get('username'),
$jsonObj->get('password'),
$jsonObj->get('database'),
$jsonObj->get('host'),
$jsonObj->get('port'),
$jsonObj->get('extras') !== null ? $jsonObj->get('extras') : []);
$extrasArr);
}
}

/**
* Returns an associative array that contain the information of database connections.
*
* @return array An associative array. The indices are connections names and
* values are objects of type 'ConnectionInfo'.
*/
public function getDBConnections(): array {
$accountsInfo = $this->json->get('database-connections');
$retVal = [];

foreach ($accountsInfo->getProperties() as $propObj) {
$name = $propObj->getName();
$jsonObj = $propObj->getValue();
$acc = new ConnectionInfo($jsonObj->get('type'), $jsonObj->get('username'), $jsonObj->get('password'), $jsonObj->get('database'));
$acc->setExtras($jsonObj->get('extras') !== null ? $jsonObj->get('extras') : []);
$acc->setHost($jsonObj->get('host'));
$acc = new ConnectionInfo(
$this->getProp($jsonObj, 'type', $name),
$this->getProp($jsonObj, 'username', $name),
$this->getProp($jsonObj, 'password', $name),
$this->getProp($jsonObj, 'database', $name));
$extrasObj = $jsonObj->get('extras');

if ($extrasObj !== null && $extrasObj instanceof Json) {
$extrasArr = [];

foreach ($extrasObj->getProperties() as $prop) {
$extrasArr[$prop->getName()] = $prop->getValue();
}
$acc->setExtras($extrasArr);
}
$acc->setHost($this->getProp($jsonObj, 'host', $name));
$acc->setName($propObj->getName());
$acc->setPort($jsonObj->get('port'));
$acc->setPort($this->getProp($jsonObj, 'port', $name));
$retVal[$propObj->getName()] = $acc;
}

return $retVal;
}
private function getProp(Json $j, $name, string $connName) {
$val = $j->get($name);
if ($val === null) {
throw new Exception('The property "'.$name.'" of the connection "'.$connName.'" is missing.');
}
return $val;
}

public function getDescription(string $langCode) {
return $this->json->get('app-descriptions')->get(strtoupper(trim($langCode)));
Expand Down Expand Up @@ -321,17 +355,21 @@ public function getSMTPConnection(string $name) {

if ($jsonObj !== null) {
return new SMTPAccount([
'sender-address' => $jsonObj->get('address'),
'pass' => $jsonObj->get('password'),
'port' => $jsonObj->get('port'),
'sender-name' => $jsonObj->get('sender-name'),
'server-address' => $jsonObj->get('host'),
'user' => $jsonObj->get('username'),
'sender-address' => $this->getProp($jsonObj, 'address', $name),
'pass' => $this->getProp($jsonObj, 'password', $name),
'port' => $this->getProp($jsonObj, 'port', $name),
'sender-name' => $this->getProp($jsonObj, 'sender-name', $name),
'server-address' => $this->getProp($jsonObj, 'host', $name),
'user' => $this->getProp($jsonObj, 'username', $name),
'account-name' => $name
]);
}
}

/**
* Returns an array that contains all added SMTP accounts.
*
* @return array An array that contains all added SMTP accounts.
*/
public function getSMTPConnections(): array {
$accountsInfo = $this->json->get('smtp-connections');
$retVal = [];
Expand All @@ -340,13 +378,13 @@ public function getSMTPConnections(): array {
$jsonObj = $prop->getValue();
$acc = new SMTPAccount();
$acc->setAccountName($name);
$acc->setAddress($jsonObj->get('address'));
$acc->setPassword($jsonObj->get('password'));
$acc->setPort($jsonObj->get('port'));
$acc->setSenderName($jsonObj->get('sender-name'));
$acc->setServerAddress($jsonObj->get('host'));
$acc->setUsername($jsonObj->get('username'));
$retVal[] = $acc;
$acc->setAddress($this->getProp($jsonObj, 'address', $name));
$acc->setPassword($this->getProp($jsonObj, 'password', $name));
$acc->setPort($this->getProp($jsonObj, 'port', $name));
$acc->setSenderName($this->getProp($jsonObj, 'sender-name', $name));
$acc->setServerAddress($this->getProp($jsonObj, 'host', $name));
$acc->setUsername($this->getProp($jsonObj, 'username', $name));
$retVal[$name] = $acc;
}

return $retVal;
Expand Down

0 comments on commit 29d16f9

Please sign in to comment.