From: Roland Häder Date: Sat, 18 Feb 2023 00:52:20 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f96ae7c7a3da80ba67f66e04dbba908d0ec2de72;p=city.git Continued: - thrown exceptions instead of assertitions - added some more debug lines - updated 'core' framework --- diff --git a/application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php b/application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php index f5a4986..6990652 100644 --- a/application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php +++ b/application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php @@ -8,9 +8,15 @@ use Org\Mxchange\City\Factory\Manager\ManagerFactory; // Import framework stuff use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend; use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory; +use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Registry\Registerable; +// Import SPL stuff +use \InvalidArgumentException; +use \LogicException; +use \BadMethodCallException; + /** * A database frontend for region informations * @@ -87,9 +93,11 @@ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements * Checks whether the user has already founded a region * * @return $hasFounded Whether the user has already founded a region + * @throws LogicException If no manager instance was created */ public function ifUserHasCreatedRegion () { // Get user instance + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGION-INFORMATION-DATABASE-WRAPPER: CALLED!'); $userInstance = GenericRegistry::getRegistry()->getInstance('user'); // Now get a search criteria instance @@ -105,7 +113,10 @@ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements $managerInstance = ManagerFactory::createManagerByType('region'); // Make sure the instance is valid - assert($managerInstance instanceof ManageableRegion); + if (!($managerInstance instanceof ManageableRegion)) { + // Not a valid instance + throw new LogicException('No manager instance created for regions', FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION); + } // Set result instance $managerInstance->setResultInstance($resultInstance); @@ -114,6 +125,7 @@ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements $hasFounded = $resultInstance->valid(); // Return result + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: hasFounded=%d - EXIT!', intval($hasFounded))); return $hasFounded; } @@ -122,8 +134,17 @@ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements * * @param $regionName Name of region * @return $isTaken Whether the given region name is taken + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws LogicException If no manager instance was created */ - public function ifRegionExists ($regionName) { + public function ifRegionExists (string $regionName) { + // Pre-check name + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: regionName=%s - CALLED!', $regionName)); + if (empty($regionName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "regionName" is empty', FramworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Now get a search criteria instance $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); @@ -135,19 +156,24 @@ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements $resultInstance = $this->doSelectByCriteria($searchInstance); // Check it + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: resultInstance[]=%s', gettype($resultInstance))); $isTaken = $resultInstance->next(); - //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken)); // Get manger instance + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: isTaken=%d', intval($isTaken))); $managerInstance = ManagerFactory::createManagerByType('region'); // Make sure the instance is valid - assert($managerInstance instanceof ManageableRegion); + if (!($managerInstance instanceof ManageableRegion)) { + // Not a valid instance + throw new LogicException(sprintf('No manager instance created for regions, regionName=%s', $regionName), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION); + } // Set result instance $managerInstance->setResultInstance($resultInstance); // Return result + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: isTaken=%d - EXIT!', intval($isTaken))); return $isTaken; } @@ -156,16 +182,25 @@ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements * * @param $regionName Name of region * @return void + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws BadMethodCallException If this method was invoked but a region with that name exists (user-based) */ - public function createRegionByName ($regionName) { + public function createRegionByName (string $regionName) { // Pre-check name - assert(!$this->ifRegionExists($regionName)); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: regionName=%s - CALLED!', $regionName)); + if (empty($regionName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "regionName" is empty', FramworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif ($this->ifRegionExists($regionName)) { + // Throw BMCE + throw new BadMethodCallException(sprintf('regionName=%s already exist', $regionName)); + } // Get user instance $userInstance = GenericRegistry::getRegistry()->getInstance('user'); // Get a dataset instance - $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION)); + $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', [self::DB_TABLE_REGION_INFORMATION]); // Set the primary key $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID); @@ -180,5 +215,8 @@ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements // Post-check name assert($this->ifRegionExists($regionName)); + + // Trace message + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGION-INFORMATION-DATABASE-WRAPPER: EXIT!'); } } diff --git a/core b/core index 262f905..c20af2b 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 262f905be8d6f9dc9fd02acc9505cb45ef2a53f6 +Subproject commit c20af2b1b05f92040a8cfabfb1d0c5cd2771f7c5