3 namespace Org\Mxchange\City\Database\Frontend\RegionInformation;
5 // Import application-specific stuff
6 use Org\Mxchange\City\Factory\Manager\ManagerFactory;
8 // Import framework stuff
9 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend;
10 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Registry\Registerable;
16 use \InvalidArgumentException;
18 use \BadMethodCallException;
21 * A database frontend for region informations
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2015 - 2023 City Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements RegionInformationFrontend, Registerable {
43 // Constants for database table names
44 const DB_TABLE_REGION_INFORMATION = 'region_data';
46 // Constants for database column names
47 const DB_COLUMN_REGION_ID = 'region_id';
48 const DB_COLUMN_REGION_NAME = 'region_name';
49 const DB_COLUMN_REGION_USER_ID = 'region_user_id';
52 * Protected constructor
56 protected function __construct () {
57 // Call parent constructor
58 parent::__construct(__CLASS__);
62 * Creates an instance of this database frontend by a provided user class
64 * @return $frontendInstance An instance of the created frontend class
66 public static final function createRegionInformationDatabaseFrontend () {
68 $frontendInstance = new RegionInformationDatabaseFrontend();
70 // Set (primary!) table name
71 $frontendInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
73 // Return the instance
74 return $frontendInstance;
78 * Removes non-public data from given array.
80 * @param $data An array with possible non-public data that needs to be removed.
81 * @return $data A cleaned up array with only public data.
83 public function removeNonPublicDataFromArray(array $data) {
84 // Currently call only inner method
85 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('REGION-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
86 $data = parent::removeNonPublicDataFromArray($data);
88 // Return cleaned data
93 * Checks whether the user has already founded a region
95 * @return $hasFounded Whether the user has already founded a region
96 * @throws LogicException If no manager instance was created
98 public function ifUserHasCreatedRegion () {
100 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('REGION-INFORMATION-DATABASE-WRAPPER: CALLED!');
101 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
103 // Now get a search criteria instance
104 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
106 // Search for user's cities
107 $searchInstance->addCriteria(RegionInformationDatabaseFrontend::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
110 $resultInstance = $this->doSelectByCriteria($searchInstance);
112 // Get region manager instance
113 $managerInstance = ManagerFactory::createManagerByType('region');
115 // Make sure the instance is valid
116 if (!($managerInstance instanceof ManageableRegion)) {
117 // Not a valid instance
118 throw new LogicException('No manager instance created for regions', FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
121 // Set result instance
122 $managerInstance->setResultInstance($resultInstance);
124 // Has it been founded?
125 $hasFounded = $resultInstance->valid();
128 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: hasFounded=%d - EXIT!', intval($hasFounded)));
133 * Checks whether the given region name is taken
135 * @param $regionName Name of region
136 * @return $isTaken Whether the given region name is taken
137 * @throws InvalidArgumentException If a parameter has an invalid value
138 * @throws LogicException If no manager instance was created
140 public function ifRegionExists (string $regionName) {
142 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: regionName=%s - CALLED!', $regionName));
143 if (empty($regionName)) {
145 throw new InvalidArgumentException('Parameter "regionName" is empty', FramworkInterface::EXCEPTION_INVALID_ARGUMENT);
148 // Now get a search criteria instance
149 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
151 // Search for the region number one which is hard-coded the default
152 $searchInstance->addCriteria(RegionInformationDatabaseFrontend::DB_COLUMN_REGION_NAME, $regionName);
153 $searchInstance->setLimit(1);
156 $resultInstance = $this->doSelectByCriteria($searchInstance);
159 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: resultInstance[]=%s', gettype($resultInstance)));
160 $isTaken = $resultInstance->next();
162 // Get manger instance
163 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: isTaken=%d', intval($isTaken)));
164 $managerInstance = ManagerFactory::createManagerByType('region');
166 // Make sure the instance is valid
167 if (!($managerInstance instanceof ManageableRegion)) {
168 // Not a valid instance
169 throw new LogicException(sprintf('No manager instance created for regions, regionName=%s', $regionName), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
172 // Set result instance
173 $managerInstance->setResultInstance($resultInstance);
176 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: isTaken=%d - EXIT!', intval($isTaken)));
181 * Creates a region by given name
183 * @param $regionName Name of region
185 * @throws InvalidArgumentException If a parameter has an invalid value
186 * @throws BadMethodCallException If this method was invoked but a region with that name exists (user-based)
188 public function createRegionByName (string $regionName) {
190 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: regionName=%s - CALLED!', $regionName));
191 if (empty($regionName)) {
193 throw new InvalidArgumentException('Parameter "regionName" is empty', FramworkInterface::EXCEPTION_INVALID_ARGUMENT);
194 } elseif ($this->ifRegionExists($regionName)) {
196 throw new BadMethodCallException(sprintf('regionName=%s already exist', $regionName));
200 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
202 // Get a dataset instance
203 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', [self::DB_TABLE_REGION_INFORMATION]);
205 // Set the primary key
206 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
208 // Add region name and assign user id
209 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID , ($this->countTotalRows() + 1));
210 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME , $regionName);
211 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
213 // "Insert" this dataset instance completely into the database
214 $this->queryInsertDataSet($dataSetInstance);
217 assert($this->ifRegionExists($regionName));
220 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('REGION-INFORMATION-DATABASE-WRAPPER: EXIT!');