// 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
*
* 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
$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);
$hasFounded = $resultInstance->valid();
// Return result
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: hasFounded=%d - EXIT!', intval($hasFounded)));
return $hasFounded;
}
*
* @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');
$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;
}
*
* @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);
// Post-check name
assert($this->ifRegionExists($regionName));
+
+ // Trace message
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGION-INFORMATION-DATABASE-WRAPPER: EXIT!');
}
}