* @version 0.0.0 * @copyright Copyright (c) 2015 - 2023 City Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements RegionInformationFrontend, Registerable { // Constants for database table names const DB_TABLE_REGION_INFORMATION = 'region_data'; // Constants for database column names const DB_COLUMN_REGION_ID = 'region_id'; const DB_COLUMN_REGION_NAME = 'region_name'; const DB_COLUMN_REGION_USER_ID = 'region_user_id'; /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this database frontend by a provided user class * * @return $frontendInstance An instance of the created frontend class */ public static final function createRegionInformationDatabaseFrontend () { // Get a new instance $frontendInstance = new RegionInformationDatabaseFrontend(); // Set (primary!) table name $frontendInstance->setTableName(self::DB_TABLE_REGION_INFORMATION); // Return the instance return $frontendInstance; } /** * Removes non-public data from given array. * * @param $data An array with possible non-public data that needs to be removed. * @return $data A cleaned up array with only public data. */ public function removeNonPublicDataFromArray(array $data) { // Currently call only inner method /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGION-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...'); $data = parent::removeNonPublicDataFromArray($data); // Return cleaned data return $data; } /** * 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 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Search for user's cities $searchInstance->addCriteria(RegionInformationDatabaseFrontend::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId()); // Get a result back $resultInstance = $this->doSelectByCriteria($searchInstance); // Get region manager instance $managerInstance = ManagerFactory::createManagerByType('region'); // Make sure the instance is valid 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); // Has it been founded? $hasFounded = $resultInstance->valid(); // Return result /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: hasFounded=%d - EXIT!', intval($hasFounded))); return $hasFounded; } /** * Checks whether the given region name is taken * * @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 (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'); // Search for the region number one which is hard-coded the default $searchInstance->addCriteria(RegionInformationDatabaseFrontend::DB_COLUMN_REGION_NAME, $regionName); $searchInstance->setLimit(1); // Get a result back $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(); // 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 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; } /** * Creates a region by given name * * @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 (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); } 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', [self::DB_TABLE_REGION_INFORMATION]); // Set the primary key $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID); // Add region name and assign user id $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID , ($this->countTotalRows() + 1)); $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME , $regionName); $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId()); // "Insert" this dataset instance completely into the database $this->queryInsertDataSet($dataSetInstance); // Post-check name assert($this->ifRegionExists($regionName)); // Trace message /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGION-INFORMATION-DATABASE-WRAPPER: EXIT!'); } }