X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fcity%2Fclasses%2Fdatabase%2Ffrontend%2Fregion%2Fclass_RegionInformationDatabaseWrapper.php;fp=application%2Fcity%2Fclasses%2Fdatabase%2Ffrontend%2Fregion%2Fclass_RegionInformationDatabaseWrapper.php;h=6990652795a89f00a1a718ec2cff5c85b54eee91;hb=f96ae7c7a3da80ba67f66e04dbba908d0ec2de72;hp=f5a4986bb2c2ec23945ddba8661521079e1642f5;hpb=ef3ba784ac193d3d760a1f4fec156895f57f9e3a;p=city.git 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!'); } }